diff --git a/fast_barcode_scanner/CHANGELOG.md b/fast_barcode_scanner/CHANGELOG.md index dc092b1d..8d96590c 100644 --- a/fast_barcode_scanner/CHANGELOG.md +++ b/fast_barcode_scanner/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.1.0 +* Relaxed kotlin version to 1.3.50 +* Updated MLKit version (16.1.2) +* The camera state now contains the torch state (on/off) +* Fixed toggling the torch in the example project + ## 1.0.2 * Even more documentation. diff --git a/fast_barcode_scanner/README.md b/fast_barcode_scanner/README.md index bea2f46a..76025330 100644 --- a/fast_barcode_scanner/README.md +++ b/fast_barcode_scanner/README.md @@ -11,7 +11,7 @@ A fast barcode scanner using **MLKit** (and **CameraX**) on Android and **AVFoun ## Installation Add the following line to your **pubspec.yaml**: ```yaml -fast_barcode_scanner: ^1.0.2 +fast_barcode_scanner: ^1.1.0 ``` ### iOS Add the `NSCameraUsageDescription` key to your `ios/Runner/Info.plist`, like so: @@ -68,28 +68,40 @@ As you can see, there are two overlays in the childrens list. These two are incl ### CameraController The `CameraController`-singleton manages the camera. It handles all the low level stuff like communicating with native code. It is implemented as a singleton to guarantee that there is always one and the same controller managing the camera. You can access the controller via the `CameraController.instance` attribute. These are the accessible methods: -|method |Description | -|----------------|-------------------------------------------------| -|`initialize` | Initialized the scanner with the provided config| -|`pauseDetector` | Actively pauses the scanner | -|`resumeDetector`| Resumes the scanner from the paused state | -|`toggleTorch` | toggles the torch on and off | -|`dispose` | Stops and resets the camera on platform level | +method |Description +----------------|------------------------------------------------- +`initialize` | Initialized the scanner with the provided config +`pauseDetector` | Actively pauses the scanner +`resumeDetector`| Resumes the scanner from the paused state +`toggleTorch` | toggles the torch on and off +`dispose` | Stops and resets the camera on platform level You do not have to call `initialize` yourself, if you use the `BarcodeCamera` widget. -If you want to use your own widget however, have a look at `CameraController.instance.state`, which contains a `PreviewConfiguration` after initialization. This class contains all necessary information to build a preview widget yourself. + +### CameraState +`CameraController.instance.state` contains the current state of the scanner. +You can use it to build your own overlay. The following information can be accessed: + +Attribute | Description +----------------|------------------------------------------------- +`isInitialized` | Indicated whether the camera is currently initialized +`previewConfig` | A `PreviewConfiguration` that is currently used +`eventNotifier` | A event notifier to react to init or detecting codes +`torchState` | The current state of the torch (on/off) +`hasError` | Indicates whether `error` is null or not +`error` | Access the error produced last ### BarcodeCamera The `BarcodeCamera` is a widget showing a preview of the camera feed. It calls the `CameraController` in the background for initialization and configuration of the barcode camera. An overview of all possible configurations (either passed to `BarcodeCamera` or `CameraController.initialize`): -|Attribute |Description | -|-------------|---------------------------------------------------------| -|`types` | See code types to scan (see `BarcodeType`) | -|`mode` | Whether to pause the camera on detection | -|`resolution` | The resolution of the camera feed | -|`framerate` | The framerate of the camera feed | -|`position` | Choose betreen back and front camera | -|`onScan` | The callback when a barcode is scanned | -|`children` | Child widgets to display on top (`BarcodeCamera` only) | +Attribute |Description +-------------|--------------------------------------------------------- +`types` | See code types to scan (see `BarcodeType`) +`mode` | Whether to pause the camera on detection +`resolution` | The resolution of the camera feed +`framerate` | The framerate of the camera feed +`position` | Choose between back and front camera (iOS) +`onScan` | The callback when a barcode is scanned +`children` | Widgets to display on top of the preview diff --git a/fast_barcode_scanner/android/build.gradle b/fast_barcode_scanner/android/build.gradle index 997ea2ce..a1cb124e 100644 --- a/fast_barcode_scanner/android/build.gradle +++ b/fast_barcode_scanner/android/build.gradle @@ -2,10 +2,10 @@ group 'com.jhoogstraat.fast_barcode_scanner' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.5.0' + ext.kotlin_version = '1.3.50' repositories { google() - jcenter() + mavenCentral() } dependencies { @@ -17,7 +17,7 @@ buildscript { rootProject.allprojects { repositories { google() - jcenter() + mavenCentral() } } @@ -54,7 +54,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" def camerax_version = "1.1.0-alpha04" - def mlkit_version = "16.1.1" + def mlkit_version = "16.1.2" implementation "androidx.camera:camera-camera2:$camerax_version" implementation "androidx.camera:camera-lifecycle:$camerax_version" diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt index 2eca01ac..69048efc 100644 --- a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt +++ b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt @@ -7,6 +7,9 @@ import android.view.Surface import androidx.camera.core.* import androidx.camera.lifecycle.ProcessCameraProvider import androidx.core.content.ContextCompat +import androidx.core.util.Consumer +import com.google.android.gms.tasks.OnFailureListener +import com.google.android.gms.tasks.OnSuccessListener import com.google.mlkit.vision.barcode.Barcode import com.google.mlkit.vision.barcode.BarcodeScannerOptions import io.flutter.embedding.android.FlutterActivity @@ -89,7 +92,7 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText fun toggleTorch(result: Result) { if (!isInitialized) return - camera.cameraControl.enableTorch(camera.cameraInfo.torchState.value != TorchState.ON).addListener({ + camera.cameraControl.enableTorch(camera.cameraInfo.torchState.value != TorchState.ON).addListener(Runnable { result.success(camera.cameraInfo.torchState.value == TorchState.ON) }, ContextCompat.getMainExecutor(activity)) } @@ -114,10 +117,10 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText .setBarcodeFormats(0, *cameraConfig.formats) .build() - barcodeDetector = MLKitBarcodeDetector(options, { codes -> + barcodeDetector = MLKitBarcodeDetector(options, OnSuccessListener { codes -> if (cameraConfig.mode.pause() && codes.isNotEmpty()) { stop() } listener(codes) - }, { + }, OnFailureListener { Log.e(TAG, "Error in MLKit", it) }) @@ -133,11 +136,11 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText cameraSurfaceProvider = Preview.SurfaceProvider { val surfaceTexture = flutterTextureEntry.surfaceTexture() surfaceTexture.setDefaultBufferSize(it.resolution.width, it.resolution.height) - it.provideSurface(Surface(surfaceTexture), cameraExecutor, {}) + it.provideSurface(Surface(surfaceTexture), cameraExecutor, Consumer {}) } val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!) - cameraProviderFuture.addListener({ + cameraProviderFuture.addListener(Runnable { cameraProvider = cameraProviderFuture.get() isInitialized = true try { bindCameraUseCases() } diff --git a/fast_barcode_scanner/example/android/build.gradle b/fast_barcode_scanner/example/android/build.gradle index a0398374..ed45c658 100644 --- a/fast_barcode_scanner/example/android/build.gradle +++ b/fast_barcode_scanner/example/android/build.gradle @@ -1,8 +1,8 @@ buildscript { - ext.kotlin_version = '1.5.0' + ext.kotlin_version = '1.3.50' repositories { google() - jcenter() + mavenCentral() } dependencies { @@ -14,7 +14,7 @@ buildscript { allprojects { repositories { google() - jcenter() + mavenCentral() } } diff --git a/fast_barcode_scanner/example/lib/scanner_screen.dart b/fast_barcode_scanner/example/lib/scanner_screen.dart index e33b5c10..5edf3d28 100644 --- a/fast_barcode_scanner/example/lib/scanner_screen.dart +++ b/fast_barcode_scanner/example/lib/scanner_screen.dart @@ -33,15 +33,13 @@ class _ScannerScreenState extends State { icon: state ? const Icon(Icons.flash_on) : const Icon(Icons.flash_off), - onPressed: () { - _torchIconState.value = !_torchIconState.value; + onPressed: () async { + await CameraController.instance.toggleTorch(); + _torchIconState.value = + CameraController.instance.state.torchState; }, ), ), - IconButton( - onPressed: () => print("switch camera in the future"), - icon: Icon(Icons.switch_camera), - ) ], ), body: BarcodeCamera( diff --git a/fast_barcode_scanner/lib/src/camera_controller.dart b/fast_barcode_scanner/lib/src/camera_controller.dart index 82507a47..c57361cd 100644 --- a/fast_barcode_scanner/lib/src/camera_controller.dart +++ b/fast_barcode_scanner/lib/src/camera_controller.dart @@ -34,6 +34,7 @@ enum CameraEvent { uninitialized, init, paused, resumed, codeFound, error } class CameraState { PreviewConfiguration? _previewConfig; + bool _torchState = false; bool _togglingTorch = false; Object? _error; @@ -42,6 +43,7 @@ class CameraState { final eventNotifier = ValueNotifier(CameraEvent.uninitialized); + bool get torchState => _torchState; bool get isInitialized => _previewConfig != null; bool get hasError => error != null; } @@ -152,7 +154,7 @@ class CameraController { state._togglingTorch = true; try { - await _platform.toggleTorch(); + state._torchState = await _platform.toggleTorch(); } catch (error, stack) { state._error = error; state.eventNotifier.value = CameraEvent.error; diff --git a/fast_barcode_scanner/pubspec.yaml b/fast_barcode_scanner/pubspec.yaml index f6cfe0c5..99acdba6 100644 --- a/fast_barcode_scanner/pubspec.yaml +++ b/fast_barcode_scanner/pubspec.yaml @@ -1,6 +1,6 @@ name: fast_barcode_scanner description: A fast barcode scanner using MLKit on Android and AVFoundation on iOS. -version: 1.0.2 +version: 1.1.0 homepage: https://github.com/jhoogstraat/fast_barcode_scanner repository: https://github.com/jhoogstraat/fast_barcode_scanner