general information
In the previous article, we talked about how to create the function of linking bank cards using the HUAWEI ML Kit's text recognition capabilities. Users only need to upload a photo of their card, and the application will automatically recognize all important information. This makes it much easier to enter bank card details. But can you do the same with invoices and coupons? Of course you can! In this article, we will show you how to use the HUAWEI ML Kit's OCR capabilities to automatically enter account numbers and discount codes.
Appointment
The OCR function can be used in a wide variety of situations. For example, if you scan the invoice below, please indicate that the service number starts with βNO.DE SERVICIOβ and also enter a length limit of up to 12 characters. Then you will quickly receive the account number "123456789123" using the text recognition function.
Likewise, if you scan the coupon below, customize the start of the βFAVE-β code, limit the length to 4 characters to receive the β8329β discount code and then complete the payment.
Useful, right? You can also customize the data your application can recognize.
Integration of the text recognition function
So let's find out how to handle invoice numbers and discount codes.
1. Preparation
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 β .