搜索插件、代码仓与 APP 分类

插件详情v4.9.0

barcode_scan2

一个用于通过摄像头扫描二维条形码和二维码的Flutter插件。

AndroidiOS
近 30 天下载
2.1万
喜欢数
307
Pub 分数
160

安装插件

flutter pub add barcode_scan2

插件介绍

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

barcode_scan2 是一个 Flutter 插件,可使用设备的摄像头扫描二维条形码和二维码。它为将条形码扫描功能集成到 Flutter 应用程序中提供了简单可靠的接口。该插件支持 Android 和 iOS 平台,适用于跨平台移动开发。通过最少的设置,开发者可以快速为零售、物流、票务等应用添加扫描功能。源代码在 GitHub 上公开,便于透明化和自定义。

适合场景

  • 零售库存管理
  • 活动票务扫描
  • 产品信息查询
  • 物流追踪
  • 通过二维码进行用户身份验证

主要特点

  • 实时条形码扫描
  • 支持二维条形码和二维码
  • 跨平台兼容性(Android 和 iOS)
  • 与 Flutter 的简单集成
  • 开源且可定制

适合对象

  • 需要条形码扫描的移动应用
  • 开发库存或票务系统的开发者
  • 需要快速获取二维码数据的项目
  • 同时面向 Android 和 iOS 用户的应用程序

使用建议

  • 两个平台都需要相机权限
  • 可能需要额外的UI组件以获得最佳用户体验
  • 性能取决于设备摄像头的质量

原始 README

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

GitHub license GitHub stars Pub GitHub forks

Reborned🎉

Original barcode_scan was discontinued, so barcode_scan2 was borned with sound null safety support🎉

barcode_scan2

A flutter plugin for scanning 2D barcodes and QR codes.

This provides a simple wrapper for two commonly used iOS and Android libraries:

  • iOS: https://github.com/mikebuss/MTBBarcodeScanner
  • Android: https://github.com/dm77/barcodescanner
Features
  • [x] Scan 2D barcodes
  • [x] Scan QR codes
  • [x] Control the flash while scanning
  • [x] Permission handling

Getting Started

Android

For Android, you must do the following before you can use the plugin:

  • Add the camera permission to your AndroidManifest.xml

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

  • This plugin is written in Kotlin. Therefore, you need to add Kotlin support to your project. See installing the Kotlin plugin.

Edit your project-level build.gradle file to look like this:

buildscript {
    ext.kotlin_version = '1.3.61'
    // ...
    dependencies {
        // ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
// ...

Edit your app-level build.gradle file to look like this:

apply plugin: 'kotlin-android'
// ...
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // ...
}

Now you can depend on the barcode_scan plugin in your pubspec.yaml file:

dependencies:
    # ...
    barcode_scan2: any

Click "Packages get" in Android Studio or run flutter packages get in your project folder.

iOS

To use on iOS, you must add the the camera usage description to your Info.plist

<dict>
    <!-- ... -->
    <key>NSCameraUsageDescription</key>
    <string>Camera permission is required for barcode scanning.</string>
    <!-- ... -->
</dict>
iOS 26+ Compatibility

This fork includes a fix for the 10-15 second delay when dismissing the scanner on iOS 26+. The fix is built-in - no additional configuration required.

Usage

import 'package:barcode_scan2/barcode_scan2.dart';

void main() async {
  var result = await BarcodeScanner.scan();

  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); // If a unknown format was scanned this field contains a note
}

Advanced usage

You can pass options to the scan method:

import 'package:barcode_scan2/barcode_scan2.dart';

void main() async {

  var options = ScanOptions(
    // set the options
  );

  var result = await BarcodeScanner.scan(options: options);

  // ...
}
Supported options
Option Type Description Supported by
strings.cancel String The cancel button text on iOS iOS only
strings.flash_on String The flash on button text iOS + Android
strings.flash_off String The flash off button text iOS + Android
restrictFormat BarcodeFormat[] Restrict the formats which are recognized iOS + Android
useCamera int The index of the camera which is used for scanning (See BarcodeScanner.numberOfCameras) iOS + Android
autoEnableFlash bool Enable the flash when start scanning iOS + Android
android.aspectTolerance double Enable auto focus on Android Android only
android.useAutoFocus bool Set aspect ratio tolerance level used in calculating the optimal Camera preview size Android only
android.appBarTitle String Sets the text of the app bar Android only

Development setup

Setup protobuf

Mac:

$ brew install protobuf
$ brew install swift-protobuf

Windows / Linux: https://github.com/protocolbuffers/protobuf#protocol-compiler-installation

Activate the protobuf dart plugin:

$ flutter pub global activate protoc_plugin

Install theProtobuf Support plugin for IDEA / Android Studio or vscode-proto3 for VS Code

If you changed the protos.proto you've to execute the ./generate_proto.sh to update the dart / swift sources

Common problems

Android "Could not find org.jetbrains.kotlin:kotlin-stdlib-jre..."

Change org.jetbrains.kotlin:kotlin-stdlib-jre to org.jetbrains.kotlin:kotlin-stdlib-jdk (StackOverflow)

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目