This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

This article translated from pub: barcode_scan2 | Flutter Package (Flutter – IO. Cn)

Translated version: barcode_scan2 4.2.0

rebirth

The original barcode_scan is no longer maintained, so barcode_scan2 is born and supports empty security.

barcode_scan2 

Flutter plugin for scanning 2D bar codes and QR codes.

The plugin provides wrappers for two commonly used iOS and Android libraries:

  • IOS: github.com/mikebuss/MT…
  • Android: github.com/dm77/barcod…

Features:

  • Scanning 2D barcode
  • Scan the QR code
  • Control flash during scanning
  • Permission to handle

start

Android

For Android, you need to do the following before using this plug-in:

  • Add camera permissions in androidmanifest.xml.

    <uses-permission android:name="android.permission.CAMERA" />

  • The plug-in is written using Kotlin. Therefore, you need to add Kotlin support to your project. View – Install the Kotlin plugin.

Edit the build.gradle file at the project level as follows:

buildscript {
    ext.kotlin_version = '1.3.61'
    // ...
    dependencies {
        // ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"}}// ...
Copy the code

Edit the build.gradle file at the application level as follows:

apply plugin: 'kotlin-android'
// ...
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // ...
}
Copy the code

You can now rely on barcode_scan2 in pubspec.yaml files:

dependencies:
    # ...
    barcode_scan2: any
Copy the code

Barcode_scan: any

Click “Package Get” in Android Studio or run flutter Package Get in the project directory.

iOS

To use it on iOS, add the camera’s usage description to info.plist.

<dict>
    <! -... -->
    <key>NSCameraUsageDescription</key>
    <string>Camera permission is required for barcode scanning.</string>
    <! -... -->
</dict>
Copy the code

use

import 'package:barcode_scan/barcode_scan.dart';

void main() async {
  var result = await BarcodeScanner.scan();

  print(result.type); // The result type (barcode, cancelled, failed)
  print(result.rawContent); // The barcode content
  print(result.format); // The barcode format (as enum)
  print(result.formatNote); // If a unknown format was scanned this field contains a note
}
Copy the code

Advanced usage

You can pass options to the scan method:

import 'package:barcode_scan/barcode_scan.dart';

void main() async {

  var options = ScanOptions(
    // set the options
  );

  var result = await BarcodeScanner.scan(options: options);

  // ...
}
Copy the code

Supported options

options type describe support
strings.cancel String Cancel button text on iOS Only the iOS
strings.flash_on String Flash open button text iOS + Android
strings.flash_off String Flash off button text iOS + Android
restrictFormat BarcodeFormat[] Qualify recognizable formats iOS + Android
useCamera int The subscript of the camera used for scanning (referenceBarcodeScanner.numberOfCameras) iOS + Android
autoEnableFlash bool Enable flash at start of scan iOS + Android
android.aspectTolerance double Autofocus is supported on Android Only the Android
android.useAutoFocus bool Sets the aspect ratio tolerance used to calculate the optimal camera preview size Only the Android

The development of installation

Install the protobuf

Mac:

$ brew install protobuf
$ brew install swift-protobuf
Copy the code

Windows/Linux: github.com/protocolbuf…

Activate the Protobuf Dart plugin:

$ flutter pub global activate protoc_plugin
Copy the code

Install Protobuf Support for IDEA/Android Studio or VS Code-Proto3 for VS Code.

If protos.proto is changed, you need to execute./generate_proto.sh to update the DART/SWIFT code.

Q&A

Android “Could not find org.jetbrains.kotlin:kotlin-stdlib-jre…”

The org. Jetbrains. Kotlin: kotlin – stdlib – jre to org. Jetbrains. Kotlin: kotlin stdlib – JDK (StackOverflow)