ryuta46

imp: add resetZoomScale

... ... @@ -289,4 +289,13 @@ class MobileScanner(
camera!!.cameraControl.setLinearZoom(scale.toFloat())
}
/**
* Reset the zoom rate of the camera.
*/
fun resetScale() {
if (camera == null) throw ZoomWhenStopped()
camera!!.cameraControl.setZoomRatio(1f)
}
}
... ...
... ... @@ -120,6 +120,7 @@ class MobileScannerHandler(
"stop" -> stop(result)
"analyzeImage" -> analyzeImage(call, result)
"setScale" -> setScale(call, result)
"resetScale" -> resetScale(call, result)
"updateScanWindow" -> updateScanWindow(call)
else -> result.notImplemented()
}
... ... @@ -234,6 +235,15 @@ class MobileScannerHandler(
}
}
private fun resetScale(call: MethodCall, result: MethodChannel.Result) {
try {
mobileScanner!!.resetScale()
result.success(null)
} catch (e: ZoomWhenStopped) {
result.error("MobileScanner", "Called setScale() while stopped!", null)
}
}
private fun updateScanWindow(call: MethodCall) {
mobileScanner!!.scanWindow = call.argument<List<Float>?>("rect")
}
... ...
... ... @@ -52,6 +52,8 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates
var standardZoomFactor: CGFloat = 1
init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback, zoomScaleChangeCallback: @escaping ZoomScaleChangeCallback) {
self.registry = registry
self.mobileScannerCallback = mobileScannerCallback
... ... @@ -140,6 +142,20 @@ 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)
// Check the zoom factor at switching from ultra wide camera to wide camera.
standardZoomFactor = 1
if #available(iOS 13.0, *) {
for (index, actualDevice) in device.constituentDevices.enumerated() {
if (actualDevice.deviceType != .builtInUltraWideCamera) {
if index > 0 && index <= device.virtualDeviceSwitchOverVideoZoomFactors.count {
standardZoomFactor = CGFloat(truncating: device.virtualDeviceSwitchOverVideoZoomFactors[index - 1])
}
break
}
}
}
do {
try device.lockForConfiguration()
if device.isFocusModeSupported(.continuousAutoFocus) {
... ... @@ -272,6 +288,22 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
/// Set the zoom factor of the camera
func resetScale() throws {
if (device == nil) {
throw MobileScannerError.torchWhenStopped
}
do {
try device.lockForConfiguration()
device.videoZoomFactor = standardZoomFactor
device.unlockForConfiguration()
} catch {
throw MobileScannerError.zoomError(error)
}
}
/// Analyze a single image
func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, callback: @escaping BarcodeScanningCallback) {
let image = VisionImage(image: image)
... ...
... ... @@ -87,6 +87,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
analyzeImage(call, result)
case "setScale":
setScale(call, result)
case "resetScale":
resetScale(call, result)
case "updateScanWindow":
updateScanWindow(call, result)
default:
... ... @@ -189,7 +191,28 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
}
result(nil)
}
/// Reset the zoomScale
private func resetScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
do {
try mobileScanner.resetScale()
} catch MobileScannerError.zoomWhenStopped {
result(FlutterError(code: "MobileScanner",
message: "Called resetScale() while stopped!",
details: nil))
} catch MobileScannerError.zoomError(let error) {
result(FlutterError(code: "MobileScanner",
message: "Error while zooming.",
details: error))
} catch {
result(FlutterError(code: "MobileScanner",
message: "Error while zooming.",
details: nil))
}
result(nil)
}
/// Toggles the torch
func updateScanWindow(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let scanWindowData: Array? = (call.arguments as? [String: Any])?["rect"] as? [CGFloat]
... ...
... ... @@ -322,6 +322,12 @@ class MobileScannerController {
await _methodChannel.invokeMethod('setScale', zoomScale);
}
/// Reset the zoomScale of the camera to use standard scale 1x.
Future<void> resetZoomScale() async {
await _methodChannel.invokeMethod('resetScale');
}
/// Disposes the MobileScannerController and closes all listeners.
///
/// If you call this, you cannot use this controller object anymore.
... ...