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("点击开始扫描二维码")),
      ),
    );
  }
}
相关推荐
肥肥呀呀呀13 小时前
flutter 资料收集
前端·flutter
程序猿阿伟13 小时前
《社交应用架构生存战:React Native与Flutter的部署容灾决胜法则》
flutter·react native·架构
肥肥呀呀呀13 小时前
flutter利用 injectable和injectable_generator 自动get_it注册
flutter
恋猫de小郭1 天前
Compose Multiplatform iOS 稳定版发布:可用于生产环境,并支持 hotload
android·flutter·macos·ios·kotlin·cocoa
yuanlaile1 天前
Flutter开发HarmonyOS实战-鸿蒙App商业项目
flutter·华为·harmonyos·flutter开发鸿蒙
WangMing_X1 天前
Flutter开发IOS蓝牙APP的大坑
flutter·ios
WDeLiang1 天前
Flutter - 概览
flutter
程序猿阿伟1 天前
《让内容“活”起来:Flutter社交应用瀑布流布局的破界实践》
前端·flutter
帅次2 天前
Flutter TabBar / TabBarView 详解
android·flutter·ios·小程序·iphone·taro·reactnative
WDeLiang2 天前
Flutter 布局
前端·flutter·dart