icapps_fast_barcode_scanner 1.2.0 copy "icapps_fast_barcode_scanner: ^1.2.0" to clipboard
icapps_fast_barcode_scanner: ^1.2.0 copied to clipboard

A fast barcode scanner using MLKit on Android and AVFoundation on iOS.

Notice #

This is a fork of the fast_barcode_scanner package. It adds camera switching which is something we need. If this gets merged with the source we will deprecated this package and forward to the original package.

icapps Fast Barcode Scanner Plugin #

pub package

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.

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

Installation #

Add the following line to your pubspec.yaml:

icapps_fast_barcode_scanner: latest_version

iOS #

Add the NSCameraUsageDescription key to your ios/Runner/Info.plist, like so:

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

import 'package:icapps_fast_barcode_scanner/icapps_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. 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
pause Actively pauses the scanner
resume Resumes the scanner from the paused state
toggleTorch toggles the torch on and off
dispose Stops and resets the camera on platform level
changeCamera Changes the camera to the other available camera
canChangeCamera Checks whether the camera can be changed

You do not have to call initialize yourself, if you use the BarcodeCamera widget.

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 between back and front camera (iOS)
onScan The callback when a barcode is scanned
children Widgets to display on top of the preview