Flutter calls the camera to scan the QR code plug-in

It is very simple for native platforms to scan QR codes. For example, iOS can quickly customize corresponding tools based on the Version library or AVFoudation library. Flutter can only rely on third-party plug-ins.

Plugin dependencies:

dependencies:
  flutter:
    sdk: flutter

  ...
  barcode_scan2: ^4.2.1

You can choose generation 1 or generation 2 according to your own Flutter project. The first generation does not support empty security, but the second generation supports it.
In addition, don't forget that the plugin's minimum dependency is Android v 18, and it will not compile if it is lower than this platform.

Native project permissions:

Taking Android as an example, declare the permissions in Mainifexist.xml:
(uses-permission tag)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.today_art">

   <application
   ...
   </application>

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

transfer

This operation is an asynchronous task.

ScanOptionsIt is an interface configuration model, which configures the text of the return key, the text of the flash on and off. resultIt returns the result asynchronously.

  _openQREvent() async {
    
    
    var options = const ScanOptions(
        android: AndroidOptions(aspectTolerance: 0.5, useAutoFocus: true),
        autoEnableFlash: false,
        strings: {
    
    'cancel': '退出', 'flash_on': '开启闪光灯', 'flash_off': '关闭闪光灯'}
    );
    var result = await BarcodeScanner.scan(options: options);
    String qrcode = result.rawContent;
    print(qrcode);
    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); //
  }

The interface is as follows:
Please add a picture description

Guess you like

Origin blog.csdn.net/kicinio/article/details/130022338