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("点击开始扫描二维码")),
      ),
    );
  }
}
相关推荐
程序员老刘20 小时前
别慌!GetX只是被误杀,但你的代码可能真的在裸奔
flutter·客户端
IntMainJhy21 小时前
【flutter for open harmony】第三方库Flutter 鸿蒙实战:商品详情页完整实现 + 点击跳转失效问题修复✨
flutter·华为·harmonyos
liulian09161 天前
【Flutter for OpenHarmony第三方库】Flutter for OpenHarmony应用更新检测功能实战指南
flutter·华为·学习方法·harmonyos
IntMainJhy1 天前
【Flutter for OpenHarmony 】第三方库 实战:`cached_network_image` 图片缓存+骨架屏鸿蒙适配全指南✨
flutter·缓存·harmonyos
恋猫de小郭1 天前
Flutter 3.41.7 ,小版本但 iOS 大修复,看完只想说:这是人能写出来的 bug ?
android·前端·flutter
吴声子夜歌2 天前
Vue.js——自定义指令
前端·vue.js·flutter
liulian09162 天前
Flutter 三方库 flutter_local_auth 的鸿蒙化适配指南
flutter·华为·学习方法·harmonyos
qwfy2 天前
瑞幸 UI 上 pub.dev 了 —— 22 个 Flutter 组件,与微信小程序版双端对齐
flutter·开源
liulian09162 天前
【Flutter for OpenHarmony】原生卡片 Widget 集成实战:从零构建待办清单桌面组件
flutter·华为·学习方法·harmonyos
2601_949593652 天前
Flutter OpenHarmony 三方库 video_player 视频播放器适配详解
flutter·音视频