搜索分类、插件与代码仓

flutter_zxing 封面
插件详情v3.1.0

flutter_zxing

一个原生的二维码扫描和生成器,基于 ZXing 的 Flutter 和 Dart FFI。

AndroidiOSmacOSWindowsLinux
近 30 天下载
4.8万
喜欢数
150
Pub 分数
160

安装插件

flutter pub add flutter_zxing

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_zxing 是一个 Flutter 包,通过 Dart FFI 使用 ZXing 库实现原生条形码扫描和生成。它提供了高性能的跨平台功能,可在 Android、iOS、macOS、Windows 和 Linux 上读取和创建二维码和条形码。该包与 Flutter 的 UI 无缝集成,并利用原生代码以实现最佳速度和准确性。它支持实时扫描和可自定义的相机配置,非常适合需要可靠条形码交互的应用程序。

适合场景

  • 移动库存管理
  • 数字票务系统
  • 产品信息查询
  • 通过二维码共享联系人
  • 支付码扫描

主要特点

  • 通过 Dart FFI 实现原生性能
  • 跨平台支持
  • 实时条形码扫描
  • 可定制的相机设置
  • 二维码和条形码生成

适合对象

  • 需要快速条形码扫描的应用程序
  • 寻求原生级别性能的开发者
  • 跨平台移动应用
  • 零售和物流软件
  • 事件或票务平台

使用建议

  • 所有平台都需要相机权限
  • 可能需要为网页进行额外配置
  • 仅限支持的平台(默认不支持网页)
  • 相机预览可能需要自定义控件处理
  • 构建设置可能因平台而略有不同

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter ZXing

Flutter ZXing is a high-performance Flutter plugin for scanning and generating QR codes and barcodes. Built on the powerful ZXing C++ library, it provides fast and reliable barcode processing capabilities for Flutter applications. Whether you need to scan barcodes from the camera or generate custom QR codes, Flutter ZXing makes it seamless and efficient.


Table of Contents


Demo Screenshots

Scanner Screen Creator Screen

Left: Barcode Scanner, Right: QR Code Creator


Features

  • Scan QR codes and barcodes from the camera stream (on mobile platforms only), image file, or URL.
  • Scan multiple barcodes at once from the camera stream (on mobile platforms only), image file, or URL.
  • Generate QR codes with customizable content and size.
  • Return the position points of the scanned barcode.
  • Customizable scanner frame size and color, and the ability to enable or disable features like torch and pinch to zoom.

Supported Formats

Linear product Linear industrial Matrix
UPC-A Code 39 QR Code
UPC-E Code 93 Micro QR Code ᴿ
EAN-8 Code 128 rMQR Code ᴿ
EAN-13 Codabar Aztec
DataBar DataBar Expanded DataMatrix
DataBar Limited ᴿ ITF PDF417
Telepen ᴿ MicroPDF417 ᴿ
DX Film Edge ᴿ MaxiCode ᴿ

ᴿ read only — these can be scanned but not generated.

Powered by zxing-cpp v3.1.1.


Supported Platforms

Platform Status Notes
Android ✅ Fully Supported Minimum API level 23 (Android 6.0)
iOS ✅ Fully Supported Minimum iOS 13.0
MacOS ⚠️ Beta Minimum macOS 10.15, without camera support
Linux ⚠️ Beta Without Camera support
Windows ⚠️ Beta Without Camera support
Web ❌ Not Supported Dart FFI is not available on the web

Note: Flutter ZXing relies on the Dart FFI feature, making it unsupported on the web. Camera-based scanning is only available on mobile platforms.


ZXScanner

ZXScanner is a free QR code and barcode scanner app for Android and iOS. It is built using Flutter and the flutter_zxing plugin.

ZXScanner logo

Features
  • Fast and reliable QR code and barcode scanning.
  • Built-in support for multiple barcode formats.
  • Fully open-source and customizable.
Try ZXScanner

To learn more or contribute, visit the ZXScanner repository.

Getting Started

Cloning the flutter_zxing project

To clone the flutter_zxing project from Github which includes submodules, use the following command:

git clone --recursive https://github.com/khoren93/flutter_zxing.git
Installing dependencies

Use Melos to install the dependencies of the flutter_zxing project. Melos is a tool that helps you manage multiple Dart packages in a single repository. To install Melos, use the following command:

flutter pub global activate melos

To install the dependencies of the flutter_zxing project, use the following command:

melos bootstrap

To allow the building on iOS and MacOS, you need to run the following command:

./scripts/update_ios_macos_src.sh

To run the integration tests:

cd example
flutter test integration_test

Now you can run the flutter_zxing example app on your device or emulator.

Use with dependency_overrides

If you want to use a forked version of the flutter_zxing library in your project, you can specify it using dependency_overrides in your pubspec.yaml. However, be aware that flutter_zxing relies on the ZXing C++ code included as a git submodule. When using dependency_overrides with a git repository, these submodules are not automatically included, and the update_ios_macos_src.sh script is not run, which can lead to errors, especially on iOS.

Your project might build but encounter runtime errors due to missing library exports that appear as an error like this: flutter: type 'ArgumentError' is not a subtype of type 'Code' in type cast

Recommended Approach: Using a Git Submodule

To ensure all necessary files are present, it's recommended to add your forked repository as a git submodule, initialize the submodules recursively, and reference it using a local path. Follow these steps:

  1. In your project add your fork as a submodule and initialize it recursively.
git submodule add https://github.com/YourUsername/flutter_zxing.git flutter_zxing
git submodule update --init --recursive
  1. Add your submodule to your pubspec.yaml file as a path dependency override.
dependency_overrides:
  flutter_zxing:
    path: ./flutter_zxing
  1. Run flutter pub get to install the dependencies.
  2. Run the scripts/update_ios_macos_src.sh script to update the iOS and MacOS source files. (Or add it to your own projects build process)
  3. Run flutter clean to clear the build cache.
  4. Build and run your project.
Why Not Use a Direct Git Reference?

Referencing your forked repo as a direct git reference in the depenency_overrides section of the pubspec.yaml does not include submodules or run the update_ios_macos_src.sh script. Manually running these steps in the .pub-cache directory is not practical, since the path changes with each commit.

Usage

To read barcode

flutter_zxing re-exports the camera types that appear in its own API (CameraImage, XFile, CameraController, ...), so a single import is enough.

import 'package:flutter_zxing/flutter_zxing.dart';

// Use ReaderWidget to quickly read barcode from camera image
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: ReaderWidget(
      onScan: (result) async {
        // Do something with the result
      },
    ),
  );
}

// Or use flutter_zxing plugin methods
// To read barcode from camera image directly
await zx.startCameraProcessing(); // Call this in initState

cameraController?.startImageStream((image) async {
    final Code result = await zx.processCameraImage(
        image,
        DecodeParams(
            imageFormat: ImageFormat.lum, // the luminance plane of a YUV420/NV21 frame
            format: Format.any,
            width: image.width,
            height: image.height,
        ),
    );
    if (result.isValid) {
        debugPrint(result.text);
    }
});

zx.stopCameraProcessing(); // Call this in dispose

// To read a barcode from an XFile, a path, a URL or raw bytes.
// Every method takes a DecodeParams; the defaults are a good starting point.
XFile xFile = XFile('Your image path');
Code resultFromXFile = await zx.readBarcodeImagePath(xFile, DecodeParams());

String path = 'Your local image path';
Code resultFromPath = await zx.readBarcodeImagePathString(path, DecodeParams());

String url = 'Your remote image url';
Code resultFromUrl = await zx.readBarcodeImageUrl(url, DecodeParams());

// `readBarcode` is synchronous and takes already-decoded pixels, so the image
// dimensions and pixel format have to be described explicitly.
Uint8List bytes = Uint8List.fromList(yourImageBytes);
Code resultFromBytes = zx.readBarcode(
    bytes,
    DecodeParams(imageFormat: ImageFormat.rgb, width: width, height: height),
);

// The reading methods never throw: a failure is reported on the result.
if (!resultFromPath.isValid) {
    debugPrint(resultFromPath.error);
}

// Every method above has a `readBarcodes...` counterpart that returns `Codes`
// with every symbol found in the image.
Codes allCodes = await zx.readBarcodesImagePath(xFile, DecodeParams());
To create barcode
import 'dart:typed_data';

import 'package:flutter_zxing/flutter_zxing.dart';

// Use WriterWidget to quickly create barcode
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: WriterWidget(
      onSuccess: (result, bytes) {
        // Do something with the result
      },
      onError: (error) {
        // Do something with the error
      },
    ),
  );
}

// Or use FlutterZxing to create barcode directly
final Encode result = zx.encodeBarcode(
    contents: 'Text to encode',
    params: EncodeParams(
        format: Format.qrCode,
        width: 120,
        height: 120,
        margin: 10,
        eccLevel: EccLevel.low,
    ),
);
if (result.isValid && result.data != null) {
    // `result.data` holds one byte per pixel. Always render it with the size
    // the encoder reports: when the symbol does not fit the requested box,
    // zxing enlarges it, so `result.width`/`result.height` may differ from the
    // width and height that were asked for.
    final Uint8List encodedBytes = pngFromBytes(
        result.data!,
        result.width!,
        result.height!,
    );
    // use encodedBytes as you wish, for example Image.memory(encodedBytes)
} else {
    debugPrint(result.error);
}

License

MIT License. See LICENSE.

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目