Skip to content

Commit

Permalink
relaxed kotlin version + fix torch
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoogstraat committed Jun 1, 2021
1 parent ef9b51c commit 09a5750
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 38 deletions.
6 changes: 6 additions & 0 deletions fast_barcode_scanner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
48 changes: 30 additions & 18 deletions fast_barcode_scanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions fast_barcode_scanner/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,7 +17,7 @@ buildscript {
rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
})

Expand All @@ -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() }
Expand Down
6 changes: 3 additions & 3 deletions fast_barcode_scanner/example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
buildscript {
ext.kotlin_version = '1.5.0'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
Expand All @@ -14,7 +14,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
10 changes: 4 additions & 6 deletions fast_barcode_scanner/example/lib/scanner_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ class _ScannerScreenState extends State<ScannerScreen> {
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(
Expand Down
4 changes: 3 additions & 1 deletion fast_barcode_scanner/lib/src/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum CameraEvent { uninitialized, init, paused, resumed, codeFound, error }

class CameraState {
PreviewConfiguration? _previewConfig;
bool _torchState = false;
bool _togglingTorch = false;
Object? _error;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion fast_barcode_scanner/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 09a5750

Please sign in to comment.