DrafaKiller / CodeScanner-flutter

A flexible code scanner for QR codes, barcodes and many others. Using Google's ML Kit. Use it as a Widget with a camera or use the methods provided, with a camera controller.
MIT License
11 stars 4 forks source link
flutter package widgets

Pub.dev package GitHub repository

Code Scanner

A flexible code scanner for QR codes, barcodes and many others. Using Google's ML Kit. Use it as a Widget with a camera or use the methods provided, with a camera controller.

Features

Scannable Formats

Getting started

Install it using pub:

flutter pub add code_scan

And import the package:

import 'package:code_scan/code_scan.dart';

Usage

CodeScanner(
    onScan: (code, details, controller) => setState(() => this.code = code),
    onScanAll: (codes, controller) => print('Codes: ' + codes.map((code) => code.rawValue).toString()),
    formats: [ BarcodeFormat.qrCode ],
    once: true,
)

Add callbacks for events:

CodeScanner(
    onScan: (code, details, controller) => ...,
    onScanAll: (codes, controller) => ...,
    onCreated: (controller) => ...,
)

Set loading and overlay widgets, although you can use the default overlay:

CodeScanner(
    loading: Center(child: CircularProgressIndicator()),
    overlay: Center(child: Text('Scanning...')),
)

Choose how the widget should react:

CodeScanner(
    direction: CameraLensDirection.back,
    resolution: ResolutionPreset.medium,
    formats: const [ BarcodeFormat.all ],
    scanInterval: const Duration(seconds: 1),
    aspectRatio: 480 / 720, // height / width
    once: false,
)

The default behavior of the widget is to fill the screen or parent widget, but you can choose the aspect ratio.

You can use the camera view with your camera controller, without having the scanning widget:

final cameraController = CameraController();

CodeScannerCameraView(
    controller: cameraController,
)

Caution

If you use your camera controller, it's recommended that you be responsible to control camera resources.

Manage it yourself using the lifecycle or make sure it's already being managed by some other widget.

Methods provided

Create a camera listener and plug it with any camera controller, to scan for codes:

final cameraController = CameraController();

final listener = CodeScannerCameraListener(
    cameraController,

    formats: const [ BarcodeFormat.all ],
    interval: const Duration(milliseconds: 500),
    once: false,

    onScan: (code, details, controller) => ...,
    onScanAll: (codes, controller) => ...,
);

Stop the listener whenever you want:

listener.stop();

GitHub

The package code is available on Github: Flutter - CodeScanner