Flutter文档扫描插件authenteq_flow_docscanner的使用

Flutter文档扫描插件authenteq_flow_docscanner的使用

介绍

authenteq_flow_docscanner 是一个用于在 Flutter 应用中集成文档扫描功能的插件。它允许用户通过简单的操作完成文档的识别和验证。

示例代码

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:authenteq_flow/authenteq_flow.dart';
import 'package:authenteq_flow/models/IdentificationParameters.dart';
import 'package:authenteq_flow/models/IdentificationResult.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _version = '未知'; // SDK版本
  dynamic _identificationResult;
  Exception? _exception;

  @override
  void initState() {
    super.initState();
    loadSdkVersion(); // 加载SDK版本
  }

  Future<void> loadSdkVersion() async {
    String version;
    try {
      version = await AuthenteqFlow.getVersion;
    } on Exception {
      version = '获取SDK版本失败。';
    }

    if (!mounted) return;

    setState(() {
      _version = version;
    });
  }

  Future<void> startIdentification() async {
    IdentificationResult? result;
    Exception? exception;
    try {
      IdentificationParameters parameters = IdentificationParameters();
      parameters.clientId = '<我的客户端ID>'; // 客户端ID
      parameters.clientSecret = '<我的密钥>'; // 密钥
      parameters.theme = {
        'primaryColor': '#00a2ff', // 主颜色
        'AndroidStyle': 'AuthenteqCustom', // Android样式
        'identificationInstructionImageForDriverLicense': 'graphics/driver.png' // 驾驶证识别说明图
      };
      result = await AuthenteqFlow.identification(parameters);
    } on Exception catch(e) {
      exception = e;
    }

    setState(() {
      _identificationResult = result;
      _exception = exception;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Authenteq Flow 插件示例'),
        ),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Spacer(),
              ElevatedButton(
                onPressed: () {
                  startIdentification(); // 开始识别
                },
                child: const Text('开始识别'),
              ),
              Text('正在运行的 Authenteq SDK 版本: $_version\n'), // 当前SDK版本
              const Spacer(),
              exceptionWidget(), // 异常处理
              resultWidget(), // 识别结果
            ],
          ),
        ),
      ),
    );
  }

  Widget exceptionWidget() {
    return _exception == null ? Container() : Text(_exception.toString(), style: const TextStyle(color: Colors.red)); // 显示异常信息
  }

  Widget resultWidget() {
    return _identificationResult == null ? Container() : Text(
        "验证ID: ${_identificationResult.verificationId}" // 显示识别结果
    );
  }
}

使用步骤

  1. 初始化项目: 创建一个新的 Flutter 项目,并在 pubspec.yaml 文件中添加依赖:

    dependencies:
      flutter:
        sdk: flutter
      authenteq_flow: ^1.0.0 # 根据实际版本号进行替换
    
  2. 导入库: 在 main.dart 文件中导入必要的库:

    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:authenteq_flow/authenteq_flow.dart';
    import 'package:authenteq_flow/models/IdentificationParameters.dart';
    import 'package:authenteq_flow/models/IdentificationResult.dart';
    
  3. 配置参数: 设置 IdentificationParameters 对象,包括客户端ID、密钥和其他自定义主题设置。

    IdentificationParameters parameters = IdentificationParameters();
    parameters.clientId = '<我的客户端ID>';
    parameters.clientSecret = '<我的密钥>';
    parameters.theme = {
      'primaryColor': '#00a2ff',
      'AndroidStyle': 'AuthenteqCustom',
      'identificationInstructionImageForDriverLicense': 'graphics/driver.png'
    };
    
  4. 启动识别: 调用 startIdentification() 方法来启动文档识别流程。

    Future<void> startIdentification() async {
      IdentificationResult? result;
      Exception? exception;
      try {
        result = await AuthenteqFlow.identification(parameters);
      } on Exception catch(e) {
        exception = e;
      }
    
      setState(() {
        _identificationResult = result;
        _exception = exception;
      });
    }
    
  5. 展示结果: 在界面上展示识别结果或异常信息。

    Widget exceptionWidget() {
      return _exception == null ? Container() : Text(_exception.toString(), style: const TextStyle(color: Colors.red));
    }
    
    Widget resultWidget() {
      return _identificationResult == null ? Container() : Text(
          "验证ID: ${_identificationResult.verificationId}"
      );
    }
    

更多关于Flutter文档扫描插件authenteq_flow_docscanner的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文档扫描插件authenteq_flow_docscanner的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


authenteq_flow_docscanner 是一个用于Flutter的文档扫描插件,它可以帮助你在应用中集成文档扫描功能。这个插件通常用于需要用户上传身份证、护照或其他文档的场景。

安装

首先,你需要在 pubspec.yaml 文件中添加 authenteq_flow_docscanner 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  authenteq_flow_docscanner: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

使用

1. 导入包

在你的Dart文件中导入 authenteq_flow_docscanner 包:

import 'package:authenteq_flow_docscanner/authenteq_flow_docscanner.dart';

2. 初始化扫描器

你可以通过调用 AuthenteqFlowDocScannerstartScan 方法来启动文档扫描流程。

void startDocumentScan() async {
  try {
    final result = await AuthenteqFlowDocScanner.startScan();
    if (result != null) {
      // 处理扫描结果
      print('Scanned document path: ${result.path}');
    } else {
      // 用户取消了扫描
      print('Scan was cancelled');
    }
  } catch (e) {
    // 处理错误
    print('Error during scan: $e');
  }
}

3. 处理扫描结果

startScan 方法返回一个 ScannedDocument 对象,其中包含扫描文档的路径和其他相关信息。你可以根据需要处理这些信息。

ScannedDocument scannedDocument = await AuthenteqFlowDocScanner.startScan();
if (scannedDocument != null) {
  String documentPath = scannedDocument.path;
  // 你可以使用这个路径来显示图像或上传到服务器
}

4. 配置扫描选项(可选)

AuthenteqFlowDocScanner 可能允许你配置扫描选项,例如扫描的文档类型、图像质量等。你可以通过传递配置参数来定制扫描流程。

final result = await AuthenteqFlowDocScanner.startScan(
  config: ScanConfig(
    documentType: DocumentType.ID_CARD,
    imageQuality: ImageQuality.HIGH,
  ),
);

注意事项

  • 权限:确保你的应用已经请求了必要的权限(如相机权限),否则扫描功能可能无法正常工作。
  • 错误处理:在调用 startScan 时,建议使用 try-catch 来捕获可能的异常。
  • 平台支持:确保插件支持你目标平台(iOS 和 Android)。

示例

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 authenteq_flow_docscanner 插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Document Scanner Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                final result = await AuthenteqFlowDocScanner.startScan();
                if (result != null) {
                  // 处理扫描结果
                  print('Scanned document path: ${result.path}');
                } else {
                  // 用户取消了扫描
                  print('Scan was cancelled');
                }
              } catch (e) {
                // 处理错误
                print('Error during scan: $e');
              }
            },
            child: Text('Scan Document'),
          ),
        ),
      ),
    );
  }
}
回到顶部