ryuta46

imp: add zoomScaleState value notifier.

... ... @@ -138,6 +138,7 @@ class MobileScanner(
torch: Boolean,
detectionSpeed: DetectionSpeed,
torchStateCallback: TorchStateCallback,
zoomScaleStateCallback: ZoomScaleStateCallback,
mobileScannerStartedCallback: MobileScannerStartedCallback,
detectionTimeout: Long
) {
... ... @@ -201,6 +202,11 @@ class MobileScanner(
torchStateCallback(state)
}
// Register the zoom scale listener
camera!!.cameraInfo.zoomState.observe(activity) { state ->
zoomScaleStateCallback(state.linearZoom.toDouble())
}
// Enable torch if provided
camera!!.cameraControl.enableTorch(torch)
... ...
... ... @@ -6,4 +6,5 @@ typealias MobileScannerCallback = (barcodes: List<Map<String, Any?>>, image: Byt
typealias AnalyzerCallback = (barcodes: List<Map<String, Any?>>?) -> Unit
typealias MobileScannerErrorCallback = (error: String) -> Unit
typealias TorchStateCallback = (state: Int) -> Unit
typealias ZoomScaleStateCallback = (zoomScale: Double) -> Unit
typealias MobileScannerStartedCallback = (parameters: MobileScannerStartParameters) -> Unit
\ No newline at end of file
... ...
... ... @@ -70,6 +70,11 @@ class MobileScannerHandler(
barcodeHandler.publishEvent(mapOf("name" to "torchState", "data" to state))
}
private val zoomScaleStateCallback: ZoomScaleStateCallback = {zoomScale: Double ->
barcodeHandler.publishEvent(mapOf("name" to "zoomScaleState", "data" to zoomScale))
}
init {
methodChannel = MethodChannel(binaryMessenger,
"dev.steenbakker.mobile_scanner/scanner/method")
... ... @@ -152,7 +157,7 @@ class MobileScannerHandler(
val detectionSpeed: DetectionSpeed = DetectionSpeed.values().first { it.intValue == speed}
try {
mobileScanner!!.start(barcodeScannerOptions, returnImage, position, torch, detectionSpeed, torchStateCallback, mobileScannerStartedCallback = {
mobileScanner!!.start(barcodeScannerOptions, returnImage, position, torch, detectionSpeed, torchStateCallback, zoomScaleStateCallback, mobileScannerStartedCallback = {
result.success(mapOf(
"textureId" to it.id,
"size" to mapOf("width" to it.width, "height" to it.height),
... ...
... ... @@ -13,6 +13,7 @@ import MLKitBarcodeScanning
typealias MobileScannerCallback = ((Array<Barcode>?, Error?, UIImage) -> ())
typealias TorchModeChangeCallback = ((Int?) -> ())
typealias ZoomScaleChangeCallback = ((Double?) -> ())
public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, FlutterTexture {
/// Capture session of the camera
... ... @@ -36,6 +37,10 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
/// When torch mode is changes, this callback will be called
let torchModeChangeCallback: TorchModeChangeCallback
/// When zoom scale is changes, this callback will be called
let zoomScaleChangeCallback: ZoomScaleChangeCallback
/// If provided, the Flutter registry will be used to send the output of the CaptureOutput to a Flutter texture.
private let registry: FlutterTextureRegistry?
... ... @@ -47,10 +52,11 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates
init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback) {
init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback, zoomScaleChangeCallback: @escaping ZoomScaleChangeCallback) {
self.registry = registry
self.mobileScannerCallback = mobileScannerCallback
self.torchModeChangeCallback = torchModeChangeCallback
self.zoomScaleChangeCallback = zoomScaleChangeCallback
super.init()
}
... ... @@ -133,6 +139,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
device.addObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode), options: .new, context: nil)
device.addObserver(self, forKeyPath: #keyPath(AVCaptureDevice.videoZoomFactor), options: .new, context: nil)
do {
try device.lockForConfiguration()
if device.isFocusModeSupported(.continuousAutoFocus) {
... ... @@ -199,6 +206,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
captureSession.removeOutput(output)
}
device.removeObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode))
device.removeObserver(self, forKeyPath: #keyPath(AVCaptureDevice.videoZoomFactor))
registry?.unregisterTexture(textureId)
textureId = nil
captureSession = nil
... ... @@ -228,6 +236,10 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
// off = 0; on = 1; auto = 2;
let state = change?[.newKey] as? Int
torchModeChangeCallback(state)
case "videoZoomFactor":
let zoomFactor = change?[.newKey] as? CGFloat ?? 1
let zoomScale = (zoomFactor - 1) / 4
zoomScaleChangeCallback(Double(zoomScale))
default:
break
}
... ...
... ... @@ -57,6 +57,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
}
}, torchModeChangeCallback: { torchState in
barcodeHandler.publishEvent(["name": "torchState", "data": torchState])
}, zoomScaleChangeCallback: { zoomScale in
barcodeHandler.publishEvent(["name": "zoomScaleState", "data": zoomScale])
})
self.barcodeHandler = barcodeHandler
super.init()
... ...
... ... @@ -85,6 +85,10 @@ class MobileScannerController {
late final ValueNotifier<CameraFacing> cameraFacingState =
ValueNotifier(facing);
/// A notifier that provides zoomScale.
final ValueNotifier<double> zoomScaleState = ValueNotifier(0.0);
bool isStarting = false;
/// A notifier that provides availability of the Torch (Flash)
... ... @@ -337,6 +341,9 @@ class MobileScannerController {
final state = TorchState.values[data as int? ?? 0];
torchState.value = state;
break;
case 'zoomScaleState':
zoomScaleState.value = data as double? ?? 0.0;
break;
case 'barcode':
if (data == null) return;
final parsed = (data as List)
... ...