react-native-vision-camera/package/example/ios/Frame Processor Plugins/Example Swift Plugin/ExampleSwiftFrameProcessor.swift
Mateusz Mędrek a291642c53
feat: Reintroduce Macros for Frame Processor Plugin registration (#2027)
in VisionCamera v1 & v2 there were two ObjC macros that were helping
in creation/registration of Frame Processors, but these were removed with
v3

This PR reintroduces such macros, which will not only make FP development
easier, but also it will also fix issues people had with registration of
Swift Frame Processors (+load vs +initialize issues)

Docs were also updated to reflect that the macros should be used to
correctly initialize and register ObjC/Swift Frame Processors
2023-10-19 10:35:14 +02:00

49 lines
1.4 KiB
Swift

//
// ExampleSwiftFrameProcessor.swift
// VisionCameraExample
//
// Created by Mateusz Medrek on 02/10/2023.
//
#if VISION_CAMERA_ENABLE_FRAME_PROCESSORS
import VisionCamera
// Example for a Swift Frame Processor plugin
@objc(ExampleSwiftFrameProcessorPlugin)
public class ExampleSwiftFrameProcessorPlugin: FrameProcessorPlugin {
public override init(options: [AnyHashable: Any]! = [:]) {
super.init(options: options)
print("ExampleSwiftPlugin - options: \(String(describing: options))")
}
public override func callback(_ frame: Frame, withArguments arguments: [AnyHashable: Any]?) -> Any? {
let imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer)
if let arguments, let imageBuffer {
let width = CVPixelBufferGetWidth(imageBuffer)
let height = CVPixelBufferGetHeight(imageBuffer)
let count = arguments.count
print(
"ExampleSwiftPlugin: \(width) x \(height) Image. Logging \(count) parameters:"
)
for key in arguments.keys {
let value = arguments[key]
let valueString = String(describing: value)
let valueClassString = String(describing: value.self)
print("ExampleSwiftPlugin: -> \(valueString) (\(valueClassString))")
}
}
return [
"example_str": "SwiftTest",
"example_bool": false,
"example_double": 6.7,
"example_array": ["Good bye", false, 21.37]
]
}
}
#endif