Flutter蓝牙设备文档阅读插件flutter_document_reader_btdevice的使用

Flutter蓝牙设备文档阅读插件flutter_document_reader_btdevice的使用

Regula Document Reader (Flutter)

Regula Document Reader SDK 允许你读取各种类型的身份证件,如护照、驾照、身份证等。所有处理都在你的设备上完全离线进行。没有任何数据离开你的设备。

你可以使用原生相机扫描证件或从图库中选择图像来提取其中的所有数据。

本仓库包含文档阅读 API 的源代码,以及一个演示如何使用文档阅读库进行 API 调用的示例应用程序。

目录

如何构建示例应用

  1. 访问 client.regulaforensics.com 获取试用许可证 (regula.license 文件)。许可证创建向导将引导你完成必要的步骤。
  2. 使用命令 git clone https://github.com/regulaforensics/DocumentReader-Flutter.git 下载或克隆此仓库。
  3. regula.license 文件复制到 example/assets 文件夹中。
  4. 在终端运行以下命令:
$ cd example
# 安装包
$ flutter pub get
# 检查支持的设备是否运行
$ flutter devices
# 运行应用
$ flutter run

解决许可证问题

如果你在运行应用程序时遇到许可证验证问题,请确保以下几点:

  1. 你使用的操作系统(例如,Android 和/或 iOS)已包含在许可证中。
  2. 许可证有效(未过期)。
  3. 你在运行应用程序的设备上的日期和时间有效。
  4. 你使用的是最新版本的文档阅读 SDK。
  5. 你已经将许可证文件放置在正确的文件夹中,如这里所述。

文档

你可以在此处找到有关 API 的文档:API 文档

附加信息

如果你有任何技术问题,随时通过电子邮件 dev.support@regulaforensics.com 或在此处创建问题 在这里 联系我们。

要将我们的 SDK 应用于自己的应用,你需要购买商业许可证。

示例代码

示例应用的主要文件

import 'dart:convert';
import 'dart:io' as io;
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart'
    show EventChannel, PlatformException, rootBundle;
import 'package:api_module_place_holder/document_reader.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Future<List<String>> getImages() async {
    setStatus("Processing image...");
    List<XFile>? files = await ImagePicker().pickMultiImage();
    List<String> result = [];
    for (XFile file in files)
      result.add(base64Encode(io.File(file.path).readAsBytesSync()));
    return result;
  }

  Object setStatus(String s) => {setState(() => _status = s)};
  String _status = "Loading...";
  bool isReadingRfidCustomUi = false;
  bool isReadingRfid = false;
  String rfidUIHeader = "Reading RFID";
  Color rfidUIHeaderColor = Colors.black;
  String rfidDescription = "Place your phone on top of the NFC tag";
  double rfidProgress = -1;
  var _portrait = Image.asset('assets/images/portrait.png');
  var _docImage = Image.asset('assets/images/id.png');
  List<List<String>> _scenarios = [];
  String _selectedScenario = "Mrz";
  bool _canRfid = false;
  bool _doRfid = false;

  bool getDocumentReaderIsReady = false;
  String btDeviceName = "Regula 0000";
  bool isBleServiceConnected = false;
  bool isBleDeviceReady = false;

  var printError = (Object error) => print((error as PlatformException).message);

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
    EventChannel('flutter_document_reader_api/event/completion')
        .receiveBroadcastStream()
        .listen((jsonString) => this.handleCompletion(
            DocumentReaderCompletion.fromJson(json.decode(jsonString))!));
    EventChannel('flutter_document_reader_api/event/database_progress')
        .receiveBroadcastStream()
        .listen(
            (progress) => setStatus("Downloading database: " + progress + "%"));
    EventChannel(
            'flutter_document_reader_api/event/rfid_notification_completion')
        .receiveBroadcastStream()
        .listen((event) => print("rfid_notification_completion: " + event.toString()));

    EventChannel('flutter_document_reader_api/event/bleOnServiceConnectedEvent')
        .receiveBroadcastStream()
        .listen((event) => bleOnServiceConnected());
    EventChannel(
            'flutter_document_reader_api/event/bleOnServiceDisconnectedEvent')
        .receiveBroadcastStream()
        .listen((event) => bleOnServiceDisconnected());
    EventChannel('flutter_document_reader_api/event/bleOnDeviceReadyEvent')
        .receiveBroadcastStream()
        .listen((event) => bleOnDeviceReady());
  }

  Future<void> initPlatformState() async {
    print(await DocumentReader.prepareDatabase("FullAuth"));
    setStatus("Database prepared");
  }

  void bleOnServiceConnected() {
    print("bleService connected, searching devices");

    setState(() => isBleServiceConnected = true);

    Future.delayed(const Duration(milliseconds: 7000), () {
      if (!isBleDeviceReady) {
        print("Failed to connect to the torch device");
        setStatus("Failed to connect to the torch device");
      }
    });
  }

  void bleOnServiceDisconnected() {
    print("bleService disconnected");
    setState(() => isBleServiceConnected = false);
  }

  void bleOnDeviceReady() {
    print("device connected, initializing");
    setStatus("Initializing...");
    setState(() => isBleDeviceReady = true);

    DocumentReader.initializeReaderBleDeviceConfig()
        .then((value) => {onInitCompleted()});
  }

  checkPermissionsAndConnect() {
    DocumentReader.isBlePermissionsGranted().then((granted) {
      if (granted) {
        this.setStatus("Loading...");
        if (!isBleServiceConnected) {
          print("connecting bleService");
          DocumentReader.setConfig({
            "functionality": {
              "useAuthenticator": true,
              "btDeviceName": btDeviceName
            }
          }).then((s) => DocumentReader.startBluetoothService());
        } else
          print("bleService already connected");
      } else
        setStatus("ble permissions denied");
    });
  }

  void addCertificates() async {
    List certificates = [];
    final manifestJson =
        await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
    final certPaths = json
        .decode(manifestJson)
        .keys
        .where((String key) => key.startsWith('assets/certificates'));

    for (var path in certPaths) {
      var findExt = path.split('.');
      var pkdResourceType = 0;
      if (findExt.length > 0)
        pkdResourceType =
            PKDResourceType.getType(findExt[findExt.length - 1].toLowerCase());
      ByteData byteData = await rootBundle.load(path);
      var certBase64 = base64.encode(byteData.buffer
          .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
      certificates
          .add({"binaryData": certBase64, "resourceType": pkdResourceType});
    }

    DocumentReader.addPKDCertificates(certificates)
        .then((value) => print("certificates added"));
  }

  void handleCompletion(DocumentReaderCompletion completion) {
    if (isReadingRfidCustomUi && (completion.action == DocReaderAction.CANCEL || completion.action == DocReaderAction.ERROR)) this.hideRfidUI();
    if (isReadingRfidCustomUi && completion.action == DocReaderAction.NOTIFICATION)
      this.updateRfidUI(completion.results!.documentReaderNotification);
    if (completion.action == DocReaderAction.COMPLETE) if (isReadingRfidCustomUi) if (completion.results!.rfidResult != 1)
      this.restartRfidUI();
    else {
      this.hideRfidUI();
      this.displayResults(completion.results!);
    } else
      this.handleResults(completion.results!);
    if (completion.action == DocReaderAction.TIMEOUT)
      this.handleResults(completion.results!);
    if (completion.action == DocReaderAction.CANCEL || completion.action == DocReaderAction.ERROR) isReadingRfid = false;
  }

  void showRfidUI() {
    // 显示动画
    setState(() => isReadingRfidCustomUi = true);
  }

  hideRfidUI() {
    // 显示动画
    this.restartRfidUI();
    DocumentReader.stopRFIDReader();
    setState(() {
      isReadingRfidCustomUi = false;
      rfidUIHeader = "Reading RFID";
      rfidUIHeaderColor = Colors.black;
    });
  }

  restartRfidUI() {
    setState(() {
      rfidUIHeaderColor = Colors.red;
      rfidUIHeader = "Failed!";
      rfidDescription = "Place your phone on top of the NFC tag";
      rfidProgress = -1;
    });
  }

  updateRfidUI(results) {
    if (results.code == ERFIDNotificationCodes.RFID_NOTIFICATION_PCSC_READING_DATAGROUP)
      setState(() => rfidDescription = results.number);
    setState(() {
      rfidUIHeader = "Reading RFID";
      rfidUIHeaderColor = Colors.black;
      rfidProgress = results.value / 100;
    });
    if (Platform.isIOS)
      DocumentReader.setRfidSessionStatus(
          rfidDescription + "\n" + results.value.toString() + "%");
  }

  customRFID() {
    this.showRfidUI();
    DocumentReader.readRFID();
  }

  usualRFID() {
    isReadingRfid = true;
    DocumentReader.startRFIDReader();
  }

  void onInitCompleted() async {
    setStatus("Ready");
    bool canRfid = await DocumentReader.isRFIDAvailableForUse();
    setState(() => _canRfid = canRfid);
    List<List<String>> scenarios = [];
    var scenariosTemp = json.decode(await DocumentReader.getAvailableScenarios());
    for (var i = 0; i < scenariosTemp.length; i++) {
      DocumentReaderScenario scenario = DocumentReaderScenario.fromJson(
          scenariosTemp[i] is String
              ? json.decode(scenariosTemp[i])
              : scenariosTemp[i])!;
      scenarios.add([scenario.name!, scenario.caption!]);
    }
    setState(() => _scenarios = scenarios);
    DocumentReader.setConfig({
      "functionality": {
        "videoCaptureMotionControl": true,
        "showCaptureButton": true
      },
      "customization": {
        "showResultStatusMessages": true,
        "showStatusMessages": true
      },
      "processParams": {"scenario": _selectedScenario}
    });
    DocumentReader.setRfidDelegate(RFIDDelegate.NO_PA);
    // addCertificates();
  }

  displayResults(DocumentReaderResults results) async {
    var name = await results.textFieldValueByType(EVisualFieldType.FT_SURNAME_AND_GIVEN_NAMES);
    var doc = await results.graphicFieldImageByType(EGraphicFieldType.GF_DOCUMENT_IMAGE);
    var portrait = await results.graphicFieldImageByType(EGraphicFieldType.GF_PORTRAIT);
    setState(() {
      _status = name ?? "";
      _docImage = Image.asset('assets/images/id.png');
      _portrait = Image.asset('assets/images/portrait.png');
      if (doc != null)
        _docImage = Image.memory(doc.data!.contentAsBytes());
      if (portrait != null)
        _portrait = Image.memory(portrait.data!.contentAsBytes());
    });
  }

  void handleResults(DocumentReaderResults results) {
    if (_doRfid && !isReadingRfid && results.chipPage != 0) {
      // customRFID();
      usualRFID();
    } else {
      isReadingRfid = false;
      displayResults(results);
    }
  }

  void onChangeRfid(bool? value) {
    setState(() => _doRfid = value! && _canRfid);
    DocumentReader.setConfig({
      "processParams": {"doRfid": _doRfid}
    });
  }

  Widget createImage(String title, double height, double width, ImageProvider image) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(title),
          Image(height: height, width: width, image: image)
        ]);
  }

  Widget createButton(String text, VoidCallback onPress) {
    return Container(
      padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
      transform: Matrix4.translationValues(0, -7.5, 0),
      child: TextButton(
          style: TextButton.styleFrom(
              backgroundColor: Color.fromARGB(50, 10, 10, 10)),
          onPressed: onPress,
          child: Text(text)),
      width: 150,
    );
  }

  Widget _buildRow(int index) {
    Radio radio = new Radio(
        value: _scenarios[index][0],
        groupValue: _selectedScenario,
        onChanged: (value) => setState(() {
              _selectedScenario = value;
              DocumentReader.setConfig({
                "processParams": {"scenario": _selectedScenario}
              });
            }));
    return Container(
        child: ListTile(
            title: GestureDetector(
                onTap: () => radio.onChanged!(_scenarios[index][0]),
                child: Text(_scenarios[index][1])),
            leading: radio),
        padding: EdgeInsets.only(left: 40));
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(title: Center(child: Text(_status))),
          body: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Visibility(
                    visible: isReadingRfidCustomUi,
                    child: Expanded(
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                          Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[]),
                          Container(
                              child: Text(rfidUIHeader,
                                  textScaleFactor: 1.75,
                                  style: TextStyle(color: rfidUIHeaderColor)),
                              padding: EdgeInsets.only(bottom: 40)),
                          Container(
                              child:
                                  Text(rfidDescription, textScaleFactor: 1.4),
                              padding: EdgeInsets.only(bottom: 40)),
                          FractionallySizedBox(
                              widthFactor: 0.6,
                              child: LinearProgressIndicator(
                                  value: rfidProgress,
                                  minHeight: 10,
                                  valueColor: new AlwaysStoppedAnimation<Color>(
                                      Color(0xFF4285F4)))),
                          TextButton(
                            onPressed: () => hideRfidUI(),
                            child: Text("X"),
                            style: TextButton.styleFrom(
                                padding: EdgeInsets.only(top: 50)),
                          ),
                        ]))),
                Visibility(
                    visible: !isReadingRfidCustomUi,
                    child: Expanded(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                          Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                createImage(
                                    "Portrait", 150, 150, _portrait.image),
                                createImage("Document image", 150, 200,
                                    _docImage.image),
                              ]),
                          Expanded(
                              child: Container(
                                  color: Color.fromARGB(5, 10, 10, 10),
                                  child: ListView.builder(
                                      itemCount: _scenarios.length,
                                      itemBuilder:
                                          (BuildContext context, int index) =>
                                              _buildRow(index)))),
                          CheckboxListTile(
                              value: _doRfid,
                              onChanged: onChangeRfid,
                              title: Text(
                                  "Process rfid reading ${_canRfid ? "" : "(unavailable)"}")),
                          Padding(
                              padding: EdgeInsets.only(bottom: 16),
                              child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Text("btDeviceName:  "),
                                    SizedBox(
                                      width: 120,
                                      child: TextFormField(
                                        decoration: InputDecoration(
                                            border: OutlineInputBorder(),
                                            hintText: 'btDeviceName'),
                                        onChanged: (text) {
                                          setState(() => btDeviceName = text);
                                        },
                                        initialValue: btDeviceName,
                                      ),
                                    ),
                                  ])),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                createButton("Scan document",
                                    () => DocumentReader.showScanner()),
                                createButton("Start service",
                                    () => checkPermissionsAndConnect()),
                              ])
                        ]))),
              ])),
    );
  }
}

更多关于Flutter蓝牙设备文档阅读插件flutter_document_reader_btdevice的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter蓝牙设备文档阅读插件flutter_document_reader_btdevice的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 flutter_document_reader_btdevice 插件来读取蓝牙设备上的文档的示例代码。请注意,这个插件是假设存在的,因为实际上 Flutter 并没有一个直接名为 flutter_document_reader_btdevice 的官方插件。但基于你的要求,我将创建一个模拟的代码案例,展示如何通过蓝牙与设备交互来读取文档。

在实际应用中,你可能需要寻找或创建一个支持蓝牙文档读取功能的自定义插件,或者利用现有的蓝牙通信插件(如 flutter_blue)结合文档解析库来实现功能。

以下是一个简化的代码示例,假设我们有一个名为 flutter_document_reader_btdevice 的插件,它提供了蓝牙连接和文档读取功能:

1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加依赖(注意:这里假设的依赖名,实际使用时请替换为真实存在的插件名):

dependencies:
  flutter:
    sdk: flutter
  flutter_document_reader_btdevice: ^x.y.z  # 替换为实际版本号

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:flutter/material.dart';
import 'package:flutter_document_reader_btdevice/flutter_document_reader_btdevice.dart';

3. 初始化蓝牙和读取文档

下面是一个简单的 Flutter 应用示例,展示如何初始化蓝牙设备,扫描设备,连接设备,并读取文档内容:

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BluetoothReader _bluetoothReader = BluetoothReader();
  List<BluetoothDevice> _devices = [];
  String _documentContent = '';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Bluetooth Document Reader'),
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView.builder(
                itemCount: _devices.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(_devices[index].name),
                    onTap: () async {
                      bool connected = await _bluetoothReader.connectToDevice(_devices[index].address);
                      if (connected) {
                        _documentContent = await _bluetoothReader.readDocument();
                        setState(() {});
                      }
                    },
                  );
                },
              ),
            ),
            Text(
              'Document Content:\n$_documentContent',
              style: TextStyle(fontSize: 16),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            await _bluetoothReader.startScan().then((scannedDevices) {
              setState(() {
                _devices = scannedDevices;
              });
            });
          },
          tooltip: 'Scan Devices',
          child: Icon(Icons.bluetooth_searching),
        ),
      ),
    );
  }
}

class BluetoothReader {
  // 模拟蓝牙连接和文档读取的方法
  Future<bool> connectToDevice(String address) async {
    // 这里应该实现蓝牙连接的逻辑
    // 返回连接结果(true 或 false)
    return Future.delayed(Duration(seconds: 2), () => true); // 模拟连接成功
  }

  Future<String> readDocument() async {
    // 这里应该实现读取蓝牙设备上文档的逻辑
    // 返回文档内容
    return Future.delayed(Duration(seconds: 2), () => 'This is the content of the document.'); // 模拟文档内容
  }

  Future<List<BluetoothDevice>> startScan() async {
    // 这里应该实现蓝牙扫描设备的逻辑
    // 返回扫描到的设备列表
    return Future.delayed(Duration(seconds: 2), () => [
      BluetoothDevice(address: 'AA:BB:CC:DD:EE:FF', name: 'Device 1'),
      BluetoothDevice(address: '11:22:33:44:55:66', name: 'Device 2'),
    ]); // 模拟扫描到的设备
  }
}

class BluetoothDevice {
  String address;
  String name;

  BluetoothDevice({required this.address, required this.name});
}

注意

  1. 实际插件使用:上述代码是基于假设的插件 flutter_document_reader_btdevice 编写的。在实际应用中,你需要查找或创建一个支持蓝牙文档读取的插件。
  2. 蓝牙权限:在 Android 和 iOS 上使用蓝牙功能时,你需要确保在 AndroidManifest.xmlInfo.plist 中添加了相应的权限声明。
  3. 错误处理:上述代码未包含错误处理逻辑。在实际应用中,你应该添加适当的错误处理来确保应用的健壮性。

希望这个示例代码能帮助你理解如何在 Flutter 应用中实现蓝牙设备文档读取功能。如果你有更具体的需求或遇到问题,请随时提问!

回到顶部