diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt index 8a7aa37..d99ddfb 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -30,6 +30,7 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : private val activity = registrar.activity() var cameraPermissionContinuation: Runnable? = null var requestingPermission = false + private var isTorchOn: Boolean = false val channel: MethodChannel init { @@ -80,11 +81,12 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : barcodeView?.resume() } - fun flipFlash() { - barcodeView?.pause() - var settings = barcodeView?.cameraSettings - settings?.isAutoTorchEnabled = ! (settings?.isAutoTorchEnabled?:false) - barcodeView?.resume() + private fun toggleFlash() { + if (hasFlash()) { + barcodeView?.setTorch(!isTorchOn) + isTorchOn = !isTorchOn + } + } private fun pauseCamera() { @@ -99,6 +101,11 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : } } + private fun hasFlash(): Boolean { + return registrar.activeContext().packageManager + .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) + } + override fun getView(): View { return initBarCodeView()?.apply { resume() @@ -143,7 +150,7 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : private fun hasCameraPermission(): Boolean { return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || - activity.checkSelfPermission(Manifest.permission.CAMERA) === PackageManager.PERMISSION_GRANTED + activity.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED } @@ -155,8 +162,8 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : "flipCamera" -> { flipCamera() } - "flipFlash" -> { - flipFlash() + "toggleFlash" -> { + toggleFlash() } "pauseCamera" -> { pauseCamera() diff --git a/example/lib/main.dart b/example/lib/main.dart index 6f48ea3..cc85f8a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -50,7 +50,7 @@ class _QRViewExampleState extends State { child: RaisedButton( onPressed: () { if (controller != null) { - controller.flipFlash(); + controller.toggleFlash(); if (_isFlashOn(flashState)) setState(() { flashState = flash_off; diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 134db76..4a4ee34 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -48,8 +48,8 @@ public class QRView:NSObject,FlutterPlatformView { self?.setDimensions(width: arguments["width"] ?? 0,height: arguments["height"] ?? 0) case "flipCamera": self?.flipCamera() - case "flipFlash": - self?.flipFlash() + case "toggleFlash": + self?.toggleFlash() case "pauseCamera": self?.pauseCamera() case "resumeCamera": @@ -76,7 +76,7 @@ public class QRView:NSObject,FlutterPlatformView { } } - func flipFlash(){ + func toggleFlash(){ if let sc: MTBBarcodeScanner = scanner { if sc.hasTorch() { sc.toggleTorch() diff --git a/lib/qr_code_scanner.dart b/lib/qr_code_scanner.dart index 4325376..2f1cde2 100644 --- a/lib/qr_code_scanner.dart +++ b/lib/qr_code_scanner.dart @@ -87,8 +87,8 @@ class QRViewController { channel.invokeMethod("flipCamera"); } - void flipFlash() { - channel.invokeMethod("flipFlash"); + void toggleFlash() { + channel.invokeMethod("toggleFlash"); } void pauseCamera() {