On this page

Stuck? Open a support ticket and the barKoder team will help. Open a ticket

Flutter Gallery scan

The SDK allows you to decode barcodes from image files. It supports several image formats, including BMP, PNG, and JPG.

To utilize this feature, pass a base64 encoded string. The code on this page goes in the State class of the General example, whose _barkoder holds the instance onBarkoderViewCreated gave; the callback gets a BarkoderResult whose decoderResults is empty when nothing was read:

Dart
_barkoder!.scanImage((result) {
  // use the result
}, base64Image);

If you want to use a photo picker to choose an image and decode it, we implement it on our example app, which you can find together with the plugin in the folder example.

We recommend using rigorous speed when decoding an image (for example at the end of _onBarkoderViewCreated):

Dart
_barkoder!.setDecodingSpeed(DecodingSpeed.rigorous);

Steps to Integrate an Image Picker into Your Application:

1. Add the image_picker #

Add the package to the dependencies in your pubspec.yaml file (or run flutter pub add image_picker):

YAML
image_picker: ^1.2.4

2. Import the image_picker package #

In the file with the State class (lib/barcode_scanner.dart in the General example), import the package, and dart:convert for base64Encode:

Dart
import 'dart:convert';
import 'package:image_picker/image_picker.dart';

3. Setup _scanImage #

Then add a _scanImage method to the State class: it opens the image picker, converts the picture to base64, sends it to the decoder and shows the result as the General example does (scannedResult). Call it from a button, for example ElevatedButton(onPressed: _scanImage, child: Text('Scan Image')) next to the General example's:

Dart
void _scanImage() async {
  final ImagePicker picker = ImagePicker();
  final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
  if (pickedFile != null) {
    // Read the file and convert it to base64
    final bytes = await pickedFile.readAsBytes();
    String base64Image = base64Encode(bytes);
    // Call scanImage with the base64 string and the callback
    _barkoder!.scanImage((result) {
      setState(() {
        scannedResult = result.decoderResults.isNotEmpty
            ? result.decoderResults[0].textualData
            : 'No barcode found';
      });
    }, base64Image);
  }
}

4. Best settings #

It is important to note that the settings that we use in this mode, which should result in best decoding results are listed below. Note that this is an all-in setting, so if you are sure you don't want to scan some symbologies, or you don't need to scan deformed images you could change this object:

JSON
"gallery_scan": {
     "decoder": {
      "Aztec": {
      "enabled": true
      },
      "Aztec Compact": {
      "enabled": true
      },
      "QR": {
      "enabled": true
      },
      "QR Micro": {
      "enabled": true
      },
      "Code 128": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Code 93": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Code 39": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0,
      "checksum": 0
      },
      "Codabar": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Code 11": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0,
      "checksum": 0
      },
      "MSI": {
      "enabled": true,
      "minimumLength": 4,
      "maximumLength": 0,
      "checksum": 1
      },
      "Upc-A": {
      "enabled": true
      },
      "Upc-E": {
      "enabled": true
      },
      "Upc-E1": {
        "enabled": false
      },
      "Ean-13": {
      "enabled": true
      },
      "Ean-8": {
      "enabled": true
      },
      "PDF 417": {
      "enabled": true
      },
      "PDF 417 Micro": {
      "enabled": true
      },
      "Datamatrix": {
      "enabled": true,
      "dpmMode": 1
      },
      "Code 25": {
      "enabled": true,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Interleaved 2 of 5": {
      "enabled": true,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "ITF 14": {
      "enabled": true
      },
      "IATA 25": {
      "enabled": true,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Matrix 25": {
      "enabled": true,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Datalogic 25": {
      "enabled": false,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "COOP 25": {
      "enabled": true,
      "checksum": 0,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Code 32": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Telepen": {
      "enabled": true,
      "minimumLength": 0,
      "maximumLength": 0
      },
      "Dotcode": {
      "enabled": true
      },
      "ID Document": {
        "enabled": true
      },
      "general": {
      "maxThreads": 2,
      "decodingSpeed": 3,
      "roi_x": 0,
      "roi_y": 0,
      "roi_w": 100,
      "roi_h": 100,
      "formattingType": 1,
      "encodingCharacterSet": "",
      "enableMisshaped1D": true,
      "upcEanDeblur": true,
      "enableVINRestrictions": false
      }
     },
     "closeSessionOnResultEnabled": true,
     "barkoderResolution": 0,
     "beepOnSuccessEnabled": true,
     "vibrateOnSuccessEnabled": false,
     "locationInImageResultEnabled": true,
     "locationLineWidth": 7
     }