Empowering Mobile Apps with Barcode Scanning: A Comprehensive Guide to ZXing-Dart

Published on by Flutter News Hub

Empowering Mobile Apps with Barcode Scanning: A Comprehensive Guide to ZXing-Dart

ZXing-Dart is an open-source, multi-format 1D/2D barcode image processing library that empowers developers with the ability to effortlessly integrate barcode scanning functionality into their Dart and Flutter applications. As a pure Dart port of the original Java library, ZXing-Dart seamlessly leverages the power of mobile device cameras to capture and decode barcodes, unlocking a world of possibilities for mobile app development.

Getting Started with ZXing-Dart

ZXing-Dart offers straightforward setup, making it accessible for developers of all levels. To get started, follow these simple steps:

  1. Add the ZXing-Dart dependency to your project's pubspec.yaml file:
dependencies:
  zxing2: ^latest_version
  1. Import the necessary ZXing-Dart classes into your Dart code:
import 'package:zxing2/qrcode.dart';

Decoding QR Codes with ZXing-Dart

QR codes, ubiquitous in modern society, can be easily decoded using ZXing-Dart's QRCodeReader class. Here's a code snippet that demonstrates QR code decoding:

import 'dart:io';
import 'package:image/image.dart' as img;
import 'package:zxing2/qrcode.dart';

void main() {
  // Read the image file into a Dart image object
  var image = img.decodePng(File('tool/example.png').readAsBytesSync())!;

  // Convert the image to a LuminanceSource, which ZXing-Dart requires for decoding
  LuminanceSource source = RGBLuminanceSource(
      image.width,
      image.height,
      image
          .convert(numChannels: 4)
          .getBytes(order: img.ChannelOrder.abgr)
          .buffer
          .asInt32List());

  // Create a BinaryBitmap, which encapsulates the LuminanceSource and prepares it for decoding
  var bitmap = BinaryBitmap(GlobalHistogramBinarizer(source));

  // Instantiate the QRCodeReader
  var reader = QRCodeReader();

  // Decode the QR code
  var result = reader.decode(bitmap);

  // Print the decoded text
  print(result.text);
}

Encoding QR Codes with ZXing-Dart

ZXing-Dart also supports QR code encoding, allowing you to generate QR codes directly from your Dart code. Here's an example of QR code encoding:

import 'dart:io';
import 'package:image/image.dart' as img;
import 'package:zxing2/qrcode.dart';

void main() {
  // Create a QR code object
  var qrcode = Encoder.encode('ABCDEF', ErrorCorrectionLevel.h);

  // Extract the QR code matrix
  var matrix = qrcode.matrix!;

  // Set the desired scale factor for the QR code image
  var scale = 4;

  // Create an image object to store the QR code
  var image = img.Image(
      width: matrix.width * scale,
      height: matrix.height * scale,
      numChannels: 4);

  // Populate the image with black and white pixels based on the QR code matrix
  for (var x = 0; x < matrix.width; x++) {
    for (var y = 0; y < matrix.height; y++) {
      if (matrix.get(x, y) == 1) {
        img.fillRect(image,
            x1: x * scale,
            y1: y * scale,
            x2: x * scale + scale,
            y2: y * scale + scale,
            color: img.ColorRgba8(0, 0, 0, 0xFF));
      }
    }
  }

  // Encode the image to PNG format
  var pngBytes = img.encodePng(image);

  // Save the PNG file to disk
  File('tool/examples/encoded.png').writeAsBytes(pngBytes);
}

ZXing-Dart in Flutter

ZXing-Dart seamlessly integrates with Flutter, allowing you to add barcode scanning capabilities to your Flutter apps. The example code in the example folder demonstrates the integration of ZXing-Dart with the official Flutter camera plugin, enabling real-time barcode scanning directly from the device's camera.

Conclusion

ZXing-Dart empowers Dart and Flutter developers with a comprehensive barcode scanning solution. Its ability to decode and encode QR codes, coupled with its seamless integration with Flutter, makes it an indispensable tool for mobile app development. With ZXing-Dart, you can easily add barcode scanning functionality to your apps, empowering users to interact with the physical world in new and innovative ways.

Flutter News Hub