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("点击开始扫描二维码")),
      ),
    );
  }
}
相关推荐
2501_920627613 小时前
Flutter 框架跨平台鸿蒙开发 - 派对策划助手应用
flutter·华为·harmonyos
里欧跑得慢4 小时前
Flutter 组件 powersync_core 的适配 鸿蒙Harmony 实战 - 驾驭极致离线优先架构、实现鸿蒙端高性能 SQL 增量同步与数据安全治理方案
flutter·harmonyos·鸿蒙·openharmony·powersync_core
芙莉莲教你写代码5 小时前
Flutter 框架跨平台鸿蒙开发 - 网络安全学习应用
学习·web安全·flutter·华为·harmonyos
kobesdu9 小时前
ROS Flutter GUI App:跨平台机器人可视化控制界面使用记录
flutter·机器人·ros
芙莉莲教你写代码9 小时前
Flutter 框架跨平台鸿蒙开发 - 时区转换器应用
学习·flutter·华为·harmonyos
芙莉莲教你写代码9 小时前
Flutter 框架跨平台鸿蒙开发 - 冥想指导应用
flutter·华为·harmonyos
王码码20359 小时前
Flutter 三方库 preact_signals 的鸿蒙化适配指南 - 掌控极致信号响应、Signals 架构实战、鸿蒙级精密状态指控专家
flutter·harmonyos·鸿蒙·openharmony·preact_signals
芙莉莲教你写代码11 小时前
Flutter 框架跨平台鸿蒙开发 - 密码管理器应用
服务器·flutter·华为·harmonyos
阿桂有点桂12 小时前
Flutter打包的app增加签名
flutter
芙莉莲教你写代码13 小时前
Flutter 框架跨平台鸿蒙开发 - 谜语大全
flutter·华为·harmonyos