Navaron Bracke

reimplement toggle torch on Android

... ... @@ -19,6 +19,7 @@ import androidx.camera.core.ExperimentalGetImage
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import androidx.camera.core.Preview
import androidx.camera.core.TorchState
import androidx.camera.core.resolutionselector.AspectRatioStrategy
import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.core.resolutionselector.ResolutionStrategy
... ... @@ -422,13 +423,16 @@ class MobileScanner(
/**
* Toggles the flash light on or off.
*/
fun toggleTorch(enableTorch: Boolean) {
if (camera == null) {
return
}
fun toggleTorch() {
camera?.let {
if (!it.cameraInfo.hasFlashUnit()) {
return@let
}
if (camera?.cameraInfo?.hasFlashUnit() == true) {
camera?.cameraControl?.enableTorch(enableTorch)
when(it.cameraInfo.torchState.value) {
TorchState.OFF -> it.cameraControl.enableTorch(true)
TorchState.ON -> it.cameraControl.enableTorch(false)
}
}
}
... ...
... ... @@ -122,8 +122,8 @@ class MobileScannerHandler(
}
})
"start" -> start(call, result)
"torch" -> toggleTorch(call, result)
"stop" -> stop(result)
"toggleTorch" -> toggleTorch(result)
"analyzeImage" -> analyzeImage(call, result)
"setScale" -> setScale(call, result)
"resetScale" -> resetScale(result)
... ... @@ -168,7 +168,7 @@ class MobileScannerHandler(
val position =
if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA
val detectionSpeed: DetectionSpeed = DetectionSpeed.values().first { it.intValue == speed}
val detectionSpeed: DetectionSpeed = DetectionSpeed.entries.first { it.intValue == speed}
mobileScanner!!.start(
barcodeScannerOptions,
... ... @@ -244,8 +244,8 @@ class MobileScannerHandler(
mobileScanner!!.analyzeImage(uri, analyzeImageSuccessCallback, analyzeImageErrorCallback)
}
private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) {
mobileScanner!!.toggleTorch(call.arguments == 1)
private fun toggleTorch(result: MethodChannel.Result) {
mobileScanner?.toggleTorch()
result.success(null)
}
... ...