Flutter关键信息提取插件ktp_extractor的使用

发布于 1周前 作者 phonegap100 来自 Flutter

Flutter关键信息提取插件ktp_extractor的使用

特性

  • 自动KTP检测和裁剪:在图像中检测KTP区域并进行裁剪以供处理。
  • 文本识别:使用OCR从KTP图像中提取文本。
  • 数据解析:解析识别出的文本以提取特定字段,如身份证号(NIK)、姓名、出生日期、地址等。
  • 易于集成:简单API,可轻松集成到您的Flutter应用程序中。

要求

iOS
  • 最低iOS部署目标:15.5.0
  • Xcode 15.3.0或更新版本
  • Swift 5
  • ML Kit不支持32位架构(i386和armv7)。ML Kit支持64位架构(x86_64和arm64)。您可以查看此列表来确认您的设备是否具有所需的能力。更多信息这里

由于ML Kit不支持32位架构(i386和armv7),您需要在Xcode中排除armv7架构,以便运行flutter build iosflutter build ipa。更多信息这里

转到项目 > Runner > 构建设置 > 排除架构 > 任何SDK > armv7:

Build Settings

您的Podfile应如下所示:

platform :ios, '15.5.0'  # 或更新版本

...

# 添加这一行:
$iOSVersion = '15.5.0'  # 或更新版本

post_install do |installer|
  # 添加这些行:
  installer.pods_project.build_configurations.each do |config|
    config.build_settings["EXCLUDED_ARCHS[sdk=*]"] = "armv7"
    config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = $iOSVersion
  end

  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)

    # 添加这些行:
    target.build_configurations.each do |config|
      if Gem::Version.new($iOSVersion) > Gem::Version.new(config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'])
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = $iOSVersion
      end
    end

  end
end

请注意,最低IPHONEOS_DEPLOYMENT_TARGET为15.5.0,您可以将其设置为更新版本但不能更旧。

Android
  • minSdkVersion: 21
  • targetSdkVersion: 33
  • compileSdkVersion: 34

使用

从KTP图像中提取信息
import 'dart:io';
import 'package:ktp_extractor/ktp_extractor.dart';

void main() async {
  // 加载您的图像文件(确保它包含KTP)
  File imageFile = File('path_to_your_image.jpg');

  // 裁剪图像以获取KTP区域(可选但推荐)
  File? croppedImage = await KtpExtractor.cropImageForKtp(imageFile);

  // 如果可用,使用裁剪后的图像进行处理
  File imageToProcess = croppedImage ?? imageFile;

  // 提取KTP信息
  KtpModel ktpData = await KtpExtractor.extractKtp(imageToProcess);

  // 访问提取的数据
  print('NIK: ${ktpData.nik}');
  print('Name: ${ktpData.name}');
  print('Birth Date: ${ktpData.birthDay}');
  print('Address: ${ktpData.address}');
  // ... 访问其他字段
}

示例代码

以下是一个完整的示例演示如何使用ktp_extractor插件:

import 'dart:convert';
import 'dart:io';

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

import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:ktp_extractor/ktp_extractor.dart';
import 'package:ktp_extractor/utils/utils.dart';

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ImagePicker? _imagePicker;
  File? _image;
  KtpModel? _ktpModel;

  [@override](/user/override)
  void initState() {
    super.initState();
    _imagePicker = ImagePicker();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('插件示例应用'),
      ),
      body: ListView(
        children: [
          if (_image != null) ...[
            Image.file(_image!),
            const SizedBox(
              height: 12,
            ),
          ],
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: ElevatedButton(
              onPressed: _getImageAsset,
              child: const Text('从资源中选择'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: ElevatedButton(
              child: const Text('从相册中选择'),
              onPressed: () => _getImage(ImageSource.gallery),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: ElevatedButton(
              child: const Text('拍照'),
              onPressed: () => _getImage(ImageSource.camera),
            ),
          ),
          if (_ktpModel != null)
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '省 : ${_ktpModel!.province}',
                    maxLines: null,
                  ),
                  Text(
                    '市/县 : ${_ktpModel!.city}',
                    maxLines: null,
                  ),
                  Text(
                    '身份证号 : ${_ktpModel!.nik}',
                    maxLines: null,
                  ),
                  Text(
                    '姓名 : ${_ktpModel!.name}',
                    maxLines: null,
                  ),
                  Text(
                    '出生地 : ${_ktpModel!.placeBirth}',
                    maxLines: null,
                  ),
                  Text(
                    '出生日期 : ${_ktpModel!.birthDay}',
                    maxLines: null,
                  ),
                  Text(
                    '住址 : ${_ktpModel!.address}',
                    maxLines: null,
                  ),
                  Text(
                    '\t\t\t门牌号 : ${_ktpModel!.rt} / ${_ktpModel!.rw}',
                    maxLines: null,
                  ),
                  Text(
                    '\t\t\t社区 : ${_ktpModel!.subDistrict}',
                    maxLines: null,
                  ),
                  Text(
                    '\t\t\t区 : ${_ktpModel!.district}',
                    maxLines: null,
                  ),
                  Text(
                    '宗教 : ${_ktpModel!.religion}',
                    maxLines: null,
                  ),
                  Text(
                    '婚姻状况 : ${_ktpModel!.marital}',
                    maxLines: null,
                  ),
                  Text(
                    '职业 : ${_ktpModel!.occupation}',
                    maxLines: null,
                  ),
                  Text(
                    '国籍 : ${_ktpModel!.nationality}',
                    maxLines: null,
                  ),
                  Text(
                    '有效期至 : ${_ktpModel!.validUntil}',
                    maxLines: null,
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }

  Future _getImage(ImageSource source) async {
    setState(() {
      _image = null;
      _ktpModel = null;
    });
    final pickedFile = await _imagePicker?.pickImage(source: source);
    if (pickedFile != null) {
      _processFile(pickedFile.path);
    }
  }

  Future _getImageAsset() async {
    setState(() {
      _image = null;
      _ktpModel = null;
    });
    final manifestContent = await rootBundle.loadString('AssetManifest.json');
    final Map<String, dynamic> manifestMap = json.decode(manifestContent);
    final assets = manifestMap.keys
        .where((String key) => key.contains('images/'))
        .where((String key) =>
            key.contains('.jpg') ||
            key.contains('.jpeg') ||
            key.contains('.png') ||
            key.contains('.webp'))
        .toList();

    showDialog(
        // 忽略:使用build_context_synchronously
        context: context,
        builder: (BuildContext context) {
          return Dialog(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text(
                    '选择图像',
                    style: TextStyle(fontSize: 20),
                  ),
                  ConstrainedBox(
                    constraints:
                        BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 0.7),
                    child: SingleChildScrollView(
                      child: Column(
                        children: [
                          for (final path in assets)
                            GestureDetector(
                              onTap: () async {
                                Navigator.of(context).pop();
                                _processFile(await getAssetPath(path));
                              },
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Image.asset(path),
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                  ElevatedButton(
                      onPressed: () => Navigator.of(context).pop(), child: const Text('取消')),
                ],
              ),
            ),
          );
        });
  }

  Future _processFile(String path) async {
    _image = await KtpExtractor.cropImageForKtp(File(path));
    _image ??= File(path);
    _ktpModel = await KtpExtractor.extractKtp(_image!);
    setState(() {});
    // _path = path;
    // final inputImage = InputImage.fromFilePath(path);
    // widget.onImage(inputImage);
  }
}

更多关于Flutter关键信息提取插件ktp_extractor的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter关键信息提取插件ktp_extractor的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用ktp_extractor插件来提取关键信息的代码案例。ktp_extractor插件通常用于从文本中提取关键信息,比如人名、地名、时间等,不过请注意,具体的功能和使用方式可能会根据插件的版本和更新有所不同。以下是一个基本的示例代码:

1. 添加依赖

首先,你需要在你的pubspec.yaml文件中添加ktp_extractor的依赖。确保你的Flutter环境已经配置好,并且你能够访问pub.dev。

dependencies:
  flutter:
    sdk: flutter
  ktp_extractor: ^最新版本号  # 替换为实际的最新版本号

2. 导入包

在你的Dart文件中导入ktp_extractor包。

import 'package:ktp_extractor/ktp_extractor.dart';

3. 使用插件

下面是一个简单的示例,展示如何使用ktp_extractor来提取文本中的关键信息。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('KTP Extractor Demo'),
        ),
        body: Center(
          child: ExtractorDemo(),
        ),
      ),
    );
  }
}

class ExtractorDemo extends StatefulWidget {
  @override
  _ExtractorDemoState createState() => _ExtractorDemoState();
}

class _ExtractorDemoState extends State<ExtractorDemo> {
  String result = "";

  void _extractKeyInfo() async {
    String text = "张三在2023年10月1日去了北京市。";
    try {
      // 初始化提取器(假设插件提供了这样的初始化方法,具体请参考插件文档)
      // KtpExtractor extractor = KtpExtractor(); // 如果需要初始化的话

      // 调用提取方法(这里的方法名和参数是假设的,请根据实际情况调整)
      Map<String, List<String>> extractedInfo = await KtpExtractor.extract(text);

      // 处理提取结果
      StringBuilder sb = StringBuilder();
      extractedInfo.forEach((key, value) {
        sb.writeln("$key: $value");
      });

      setState(() {
        result = sb.toString();
      });
    } catch (e) {
      setState(() {
        result = "Error: $e";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        ElevatedButton(
          onPressed: _extractKeyInfo,
          child: Text('Extract Key Info'),
        ),
        SizedBox(height: 20),
        Text("Extracted Info:\n$result"),
      ],
    );
  }
}

注意事项

  1. 插件版本:确保你使用的是ktp_extractor的最新版本,因为插件的API可能会随着版本更新而变化。
  2. 文档:查阅ktp_extractor的官方文档,了解所有可用的方法和参数。
  3. 错误处理:在实际应用中,添加更多的错误处理和用户反馈,以提高应用的健壮性和用户体验。

由于ktp_extractor插件的具体实现细节(如初始化方法、提取方法的具体名称和参数)可能有所不同,上述代码是基于假设的示例。在实际使用时,请参考插件的官方文档和示例代码。

回到顶部