如何使用HUAWEI ML Kit的OCR功能实现自动号码输入

一般信息



一篇文章中,我们讨论了如何使用HUAWEI ML Kit的文本识别功能创建链接银行卡的功能。用户只需要上传卡的照片,应用程序就会自动识别所有重要信息。这样可以更轻松地输入银行卡详细信息。但是您可以使用发票和优惠券吗?当然可以!在本文中,我们将向您展示如何使用HUAWEI ML Kit的OCR功能自动输入帐号和折扣代码。



约定



OCR功能可用于多种情况。例如,如果您扫描下面的发票,请指出服务编号以“ NO.DE SERVICIO”开头,并且最多输入12个字符的长度限制。然后,您将使用文本识别功能快速收到帐号“ 123456789123”。



图片


同样,如果您扫描下面的优惠券,请自定义“ FAVE-”代码的开头,将长度限制为4个字符以接收“ 8329”折扣代码,然后完成付款。



图片


有用吧?您还可以自定义应用程序可以识别的数据。



整合文字识别功能



因此,让我们了解如何处理帐号和折扣代码。



1.准备



HUAWEI Developer. .



1.1 Maven build.gradle



buildscript {
    repositories {
     ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
}
 dependencies {
   ...
        classpath 'com.huawei.agconnect:agcp:1.3.1.300'
    }
allprojects {
    repositories {
     ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
}


1.2



SDK :



apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'


1.3 SDK build.gradle



dependencies {
    // Import the base SDK.
    implementation 'com.huawei.hms:ml-computer-vision-ocr:2.0.1.300'
    // Import the Latin character recognition model package.
    implementation 'com.huawei.hms:ml-computer-vision-ocr-latin-model:2.0.1.300'
    // Import the Japanese and Korean character recognition model package.
    implementation 'com.huawei.hms:ml-computer-vision-ocr-jk-model:2.0.1.300'
    // Import the Chinese and English character recognition model package.
    implementation 'com.huawei.hms:ml-computer-vision-ocr-cn-model:2.0.1.300'
}


1.4 AndroidManifest.xml



<manifest>
    ...
    <meta-data
        android:name="com.huawei.hms.ml.DEPENDENCY"
        android:value="ocr" />
     ...
</manifest>   


1.5



<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />


2.



2.1



MLTextAnalyzer analyzer = new MLTextAnalyzer.Factory(context).setLanguage(type).create();


2.2



analyzer.setTransactor(new OcrDetectorProcessor());


2.3 API



LensEngine SDK , .



lensEngine = new LensEngine.Creator(context, analyzer)
  .setLensType(LensEngine.BACK_LENS)
  .applyDisplayDimension(width, height)
  .applyFps(30.0f)
  .enableAutomaticFocus(true)
  .create(); 


2.4 run



try {
    lensEngine.run(holder);
} catch (IOException e) {
    // Exception handling logic.
    Log.e("TAG", "e=" + e.getMessage());
}


2.5 ,



public class OcrDetectorProcessor implements MLAnalyzer.MLTransactor<MLText.Block> {
    @Override
    public void transactResult(MLAnalyzer.Result<MLText.Block> results) {
         SparseArray<MLText.Block> items = results.getAnalyseList();
         // Process the recognition result as required. Only the detection results are processed.
        // Other detection-related APIs provided by ML Kit cannot be called.
    }
    @Override
    public void destroy() {
        // Callback method used to release resources when the detection ends.
    }
}


2.6



if (analyzer != null) {
    try {
        analyzer.stop();
    } catch (IOException e) {
        // Exception handling.
    }
}
if (lensEngine != null) {
    lensEngine.release();
}




! , . , .



图片


, .



图片


Github



Github

→ -: HUAWEI ML Kit — .




All Articles