Skip to content

Commit

Permalink
Add documentation and some small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoogstraat committed May 16, 2021
1 parent 455a72a commit ef9b51c
Show file tree
Hide file tree
Showing 29 changed files with 525 additions and 1,217 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Contributing to fast_barcode_scanner

Contributions are welcome by submitting a PR for to be reviewed.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Flutter Barcode Scanner plugin

This repository consists hosts the following packages

fast_barcode_scanner: code for the cross-platform facing plugin used to display a camera view within Flutter applications
fast_barcode_scanner_platform_interface: the code for the common platform interface
These can be found in the corresponding directories within the same name. Most developers are likely here as they are looking to use the fast_barcode_scanner plugin. There is a readme file within each directory with more information.

Issues

If you run into bugs, please raise them on the GitHub repository. Please do not email them to me as GitHub is the appropriate place for them and allows for members of the community to answer questions, particularly if I miss the email. It would also be much appreciated if they could be limited to actual bugs or feature requests. If you're looking at how you could use the plugin to do a particular kind of notification, check the example app provides detailed code samples for each supported feature. Also try to check the README first in case you have missed something e.g. platform-specific setup.

Contributions

The guidelines around submitting pull requests can be found here
10 changes: 9 additions & 1 deletion fast_barcode_scanner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.0.1
## 1.0.2

* Even more documentation.

## 1.0.1

* Updated documentation.

## 1.0.0

* Initial pre-release.
107 changes: 95 additions & 12 deletions fast_barcode_scanner/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
# fast_barcode_scanner

A fast barcode scanner using ML Kit (and CameraX) on Android and AVFoundation on iOS.

Android implementation is still missing.

Documentation coming soon.

## Installation

### iOS
Add a description under the NSCameraUsageDescription key to your Info.plist.
# fast_barcode_scanner

[![pub package](https://img.shields.io/pub/v/fast_barcode_scanner)](https://pub.dev/packages/fast_barcode_scanner)

A fast barcode scanner using **MLKit** (and **CameraX**) on Android and **AVFoundation** on iOS. This package leaves the UI up to the user, but rather gives an access to a camera preview.

*Note*: This plugin is still under development, and some APIs might not be available yet. If you have any issues, ideas or recommendendations, don't hesitate to create an issue or pull request on github. I am using this plugin in production myself and will actively develop and maintain it going forward.

**This plugin required iOS 10.0 and Android sdk version 21 or higher.**

## Installation
Add the following line to your **pubspec.yaml**:
```yaml
fast_barcode_scanner: ^1.0.2
```
### iOS
Add the `NSCameraUsageDescription` key to your `ios/Runner/Info.plist`, like so:
```xml
<key>NSCameraUsageDescription</key>
<string>This app requires access to your phone’s camera solely for scanning barcodes</string>
```

### Android
Change the minimum Android sdk version to 21 (or higher) in your `android/app/build.gradle` file.
```
minSdkVersion 21
```
## Usage
The barcode scanner consists of two main classes `CameraController` and `BarcodeCamera`.
A full example looks like this:
```dart
import 'package:fast_barcode_scanner/fast_barcode_scanner.dart';
class MyScannerScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Barcode Scanner')),
body: BarcodeCamera(
types: const [
BarcodeType.ean8,
BarcodeType.ean13,
BarcodeType.code128
],
resolution: Resolution.hd720,
framerate: Framerate.fps30,
mode: DetectionMode.pauseVideo,
onScan: (code) => print(code),
children: [
MaterialPreviewOverlay(animateDetection: false),
BlurPreviewOverlay(),
Positioned(
child: ElevatedButton(
onPressed: () =>
CameraController.instance.resumeDetector(),
child: Text('Resume'),
),
)
],
)
)
}
}
```
As you can see, there are two overlays in the childrens list. These two are included in the package. `MaterialPreviewOverlay` mimics the official [material barcode scanning example](https://material.io/design/machine-learning/barcode-scanning.html#usage). `BlurPreviewOverlay` blurs the screen when a barcode is detected and unblurs it on resuming. These are normal widget, which are shown above the camera preview. Look at their source code to find out, how to react to events from the barcode scanner.

### 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 |

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.

### 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) |
123 changes: 62 additions & 61 deletions fast_barcode_scanner/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
group 'com.jhoogstraat.fast_barcode_scanner'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.5.0'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
minSdkVersion 23
}

lintOptions {
disable 'InvalidPackage'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

def camerax_version = "1.1.0-alpha04"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"

implementation 'com.google.mlkit:barcode-scanning:16.1.1'
}
group 'com.jhoogstraat.fast_barcode_scanner'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.5.0'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
minSdkVersion 21
}

lintOptions {
disable 'InvalidPackage'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

def camerax_version = "1.1.0-alpha04"
def mlkit_version = "16.1.1"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"

implementation "com.google.mlkit:barcode-scanning:$mlkit_version"
}
2 changes: 1 addition & 1 deletion fast_barcode_scanner/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {

defaultConfig {
applicationId "com.jhoogstraat.fast_barcode_scanner_example"
minSdkVersion 23
minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
12 changes: 6 additions & 6 deletions fast_barcode_scanner/example/lib/detections_counter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'package:fast_barcode_scanner/fast_barcode_scanner.dart';
import 'package:fast_barcode_scanner_example/scanner_screen.dart';
import 'package:flutter/material.dart';

class DetectionsCounter extends StatefulWidget {
Expand All @@ -11,11 +11,11 @@ class _DetectionsCounterState extends State<DetectionsCounter> {
@override
void initState() {
super.initState();
// _streamToken = CameraController.instance.state.codeStream.listen((event) {
// final count = detectionCount.update(event.value, (value) => value + 1,
// ifAbsent: () => 1);
// detectionInfo.value = "${count}x\n${event.value}";
// });
_streamToken = codeStream.stream.listen((event) {
final count = detectionCount.update(event.value, (value) => value + 1,
ifAbsent: () => 1);
detectionInfo.value = "${count}x\n${event.value}";
});
}

late StreamSubscription _streamToken;
Expand Down
16 changes: 1 addition & 15 deletions fast_barcode_scanner/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import 'package:fast_barcode_scanner/fast_barcode_scanner.dart';
import 'package:flutter/material.dart';

import 'scanner_screen.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(home: ScannerScreen());
}
runApp(MaterialApp(home: ScannerScreen()));
}
14 changes: 10 additions & 4 deletions fast_barcode_scanner/example/lib/scanner_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'dart:async';

import 'package:fast_barcode_scanner/fast_barcode_scanner.dart';
import 'package:flutter/material.dart';
import 'detections_counter.dart';

final codeStream = StreamController<Barcode>.broadcast();

class ScannerScreen extends StatefulWidget {
@override
_ScannerScreenState createState() => _ScannerScreenState();
Expand Down Expand Up @@ -35,9 +39,8 @@ class _ScannerScreenState extends State<ScannerScreen> {
),
),
IconButton(
onPressed: () => Navigator.pushReplacement(
context, MaterialPageRoute(builder: (ctx) => ScannerScreen())),
icon: Icon(Icons.navigate_next),
onPressed: () => print("switch camera in the future"),
icon: Icon(Icons.switch_camera),
)
],
),
Expand All @@ -51,12 +54,15 @@ class _ScannerScreenState extends State<ScannerScreen> {
resolution: Resolution.hd720,
framerate: Framerate.fps30,
mode: DetectionMode.pauseVideo,
position: CameraPosition.front,
position: CameraPosition.back,
onScan: (code) => codeStream.add(code),
children: [
MaterialPreviewOverlay(animateDetection: false),
BlurPreviewOverlay(),
Positioned(
bottom: 50,
left: 0,
right: 0,
child: Column(
children: [
ElevatedButton(
Expand Down
Loading

0 comments on commit ef9b51c

Please sign in to comment.