Creating Frame Processor Plugins
Creating a Frame Processor Plugin for iOS​
The Frame Processor Plugin API is built to be as extensible as possible, which allows you to create custom Frame Processor Plugins. In this guide we will create a custom QR Code Scanner Plugin which can be used from JS.
iOS Frame Processor Plugins can be written in either Objective-C or Swift.
Automatic setup​
Run Vision Camera Plugin Builder CLI,
npx vision-camera-plugin-builder ios
The CLI will ask you for the path to project's .xcodeproj file, name of the plugin (e.g. QRCodeFrameProcessor
), name of the exposed method (e.g. scanQRCodes
) and language you want to use for plugin development (Objective-C, Objective-C++ or Swift).
For reference see the CLI's docs.
Manual setup​
- Objective-C
- Swift
- Open your Project in Xcode
- Create an Objective-C source file, for the QR Code Plugin this will be called
QRCodeFrameProcessorPlugin.m
. - Add the following code:
#import <VisionCamera/FrameProcessorPlugin.h>
#import <VisionCamera/Frame.h>
@interface QRCodeFrameProcessorPlugin : NSObject
@end
@implementation QRCodeFrameProcessorPlugin
static inline id scanQRCodes(Frame* frame, NSArray* args) {
CMSampleBufferRef buffer = frame.buffer;
UIImageOrientation orientation = frame.orientation;
// code goes here
return @[];
}
VISION_EXPORT_FRAME_PROCESSOR(scanQRCodes)
@end
The JS function name will be equal to the Objective-C function name you choose (with a __
prefix). Make sure it is unique across other Frame Processor Plugins.
- Implement your Frame Processing. See the Example Plugin (Objective-C) for reference.
- Open your Project in Xcode
- Create a Swift file, for the QR Code Plugin this will be
QRCodeFrameProcessorPlugin.swift
. If Xcode asks you to create a Bridging Header, press create.
- Inside the newly created Bridging Header, add the following code:
#import <VisionCamera/FrameProcessorPlugin.h>
#import <VisionCamera/Frame.h>
- Create an Objective-C source file with the same name as the Swift file, for the QR Code Plugin this will be
QRCodeFrameProcessorPlugin.m
. Add the following code:
#import <VisionCamera/FrameProcessorPlugin.h>
@interface VISION_EXPORT_SWIFT_FRAME_PROCESSOR(scanQRCodes, QRCodeFrameProcessorPlugin)
@end
The first parameter in the Macro specifies the JS function name. Make sure it is unique across other Frame Processors.
- In the Swift file, add the following code:
@objc(QRCodeFrameProcessorPlugin)
public class QRCodeFrameProcessorPlugin: NSObject, FrameProcessorPluginBase {
@objc
public static func callback(_ frame: Frame!, withArgs _: [Any]!) -> Any! {
let buffer = frame.buffer
let orientation = frame.orientation
// code goes here
return []
}
}
- Implement your frame processing. See Example Plugin (Swift) for reference.