Flutter has long lacked an easy-to-use and powerful library for scanning qr codes, bar codes, and other codes. It needed to support both camera scanning and local image code recognition. To solve this problem, I investigated the native scan ecology, selected the powerful HUAWEI ScanKit SDK, and packaged the Flutter plug-in package.

Huawei’s ScanKit SDK supports both Android and iOS, as well as a scan interface and gallery selection function, which is ideal for introducing the Flutter world.

Warehouse address: flutter_scankit

Chinese document

This is a scan for Flutter plugin, which is the HUAWEI ScanKit SDK package for Flutter. HUAWEI ScanKit is a powerful library that is easy to use. It has a high recognition rate and high code recognition speed.

Thanks to Huawei’s accumulated capability in computer vision, Scan Kit can detect and automatically enlarge remote codes or small codes, and optimize the recognition of common complex code scanning scenes (such as reflection, dark light, stain, blur, and column), improving the success rate of code scanning and user experience.

  • Android
  • iOS

scan the code

The Scan Kit supports scanning for 13 global mainstream codes. If your application only handles some specific codes, you can also specify the codes in the interface to speed up the scanning. Supported codes:

  • One-dimensional codes: EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128, ITF-14
  • QR Code: QR Code, Data Matrix, PDF417, Aztec

Support camera scan code and local image code recognition.

usage

  1. Configure permissions
  2. Processing permission requests
  3. Call API

Configure permissions

iOS

Add the following to ios/Runner/ info.plist

    <key>NSCameraUsageDescription</key>
    <string>Explain to the user why you need this permission</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Explain to the user why you need this permission</string>
Copy the code

Note that replacing the contents of the
tag gives the user a reason for needing this permission.

No configuration required for Android!

Permission to request

In Flutter, you need a plugin library to handle permissions. Here I recommend another plugin library: Flutter_easy_permission. For details, see here.

Open the ios/Podfile file and add the following configuration:

target 'Runner' do
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  # Add the library of permissions you need here
  pod 'LBXPermission/Camera'
  pod 'LBXPermission/Photo'
end
Copy the code

Then execute the command to install.

Call API

  void initState() {
    super.initState(); scanKit = FlutterScankit() .. addResultListen((val) {// Return the recognition result
      debugPrint("scanning result:$val");
    });

    FlutterEasyPermission().addPermissionCallback(
        onGranted: (requestCode, perms,perm) {
          startScan();
        },
        onDenied: (requestCode, perms,perm, isPermanent) {});
  }
Copy the code

Scan the code:

    // If there is no permission request
    if (!await FlutterEasyPermission.has(perms: _permissions,permsGroup: _permissionGroup)) {
          FlutterEasyPermission.request(perms: _permissions,permsGroup: _permissionGroup);
    } else {
          // call if you have permission
          startScan();
    }
    
    
Future<void> startScan() async {
    try {
      await scanKit.startScan(scanTypes: [ScanTypes.ALL]);
    } on PlatformException {}
}
Copy the code

For the use of FlutterEasyPermission, see here.

example

For a complete example, seehere.

TODO

SDK itself supports custom scan UI, but the plug-in has not been deeply encapsulated so it cannot support custom pages. In the future, some space will be removed to conduct deep encapsulation through external texture, and the custom scan interface can be directly defined in the Flutter Widget layer.

Plug-in development

If you are interested in the development of the Flutter plugin, you can go to my website to check out the course full Stack Flutter Development – Advanced. This course provides an exclusive in-depth explanation of all aspects of Flutter plugin development and explains several practical cases (including the development process of this plugin).

Welcome to the public account: the path of programming from 0 to 1