Flutter_学习记录_barcode_scan2 实现扫描条形码、二维码

插件地址:https://pub.dev/packages/barcode_scan2

  1. 安装插件
  2. Android 的配置

配置:android\app\src\main\AndroidManifest.xml

dart 复制代码
<uses-permission android:name="android.permission.CAMERA" />
  1. iOS 的配置

配置:info.plist

dart 复制代码
 <key>NSCameraUsageDescription</key> 
 	<string>Camera permission is required for barcode scanning</string> 
  1. 使用
dart 复制代码
var options = ScanOptions(
      // 是否自动打开闪光灯
      autoEnableFlash: true, 
      // 配置文案
      strings: {
         'cancel': '取消', 
         'flash_on': '打开Flash', 
         'flash_off': '关闭Flash'
      }
    );
    var result = await BarcodeScanner.scan(options: options);
    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
  1. 完整代码
dart 复制代码
import 'package:flutter/material.dart';
import 'package:barcode_scan2/barcode_scan2.dart';


class ScanDemo extends StatelessWidget {
  const ScanDemo({super.key});

  void _doBarcodeScan() async {
    var options = ScanOptions(
      // 是否自动打开闪光灯
      autoEnableFlash: true, 
      // 配置文案
      strings: {
         'cancel': '取消', 
         'flash_on': '打开Flash', 
         'flash_off': '关闭Flash'
      }
    );
    var result = await BarcodeScanner.scan(options: options);
    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
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("扫描二维码"),
      ),
      body: Center(
        child: ElevatedButton(onPressed: _doBarcodeScan, child: Text("点击开始扫描二维码")),
      ),
    );
  }
}
相关推荐
程序员Ctrl喵9 小时前
异步编程:Event Loop 与 Isolate 的深层博弈
开发语言·flutter
前端不太难11 小时前
Flutter 如何设计可长期维护的模块边界?
flutter
小蜜蜂嗡嗡12 小时前
flutter列表中实现置顶动画
flutter
始持12 小时前
第十二讲 风格与主题统一
前端·flutter
始持12 小时前
第十一讲 界面导航与路由管理
flutter·vibecoding
始持12 小时前
第十三讲 异步操作与异步构建
前端·flutter
新镜13 小时前
【Flutter】 视频视频源横向、竖向问题
flutter
黄林晴13 小时前
Compose Multiplatform 1.10 发布:统一 Preview、Navigation 3、Hot Reload 三箭齐发
android·flutter
Swift社区14 小时前
Flutter 应该按功能拆,还是按技术层拆?
flutter
肠胃炎14 小时前
树形选择器组件封装
前端·flutter