Android integrated Baidu character recognition 1.4.0
Baidu AI open platform has opened the service of character recognition, which can realize the recognition requirements of some general characters, network pictures and characters, various cards (ID card, bank card, driver's license, driving license, license plate business license, general bill), and simplify the input operation. Next, record the process of your own practice for future use.
Integration process:
1: Go to the SDK download page to download the SDK: http://ai.baidu.com/sdk/#ocr , which includes demo, libs, and ui modules. The introduction of jar and so is not recorded. The ui module is mainly for taking photos, cutting and other ui. I record the modification of demo here
2: Add permissions and check your own permissions as follows:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
It mainly refers to the permission for taking photos and obtaining album pictures.
3: Confusing configuration:
-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**
4: When Android studio imports the demo project and runs it directly, there will be a token problem. How to get the token? The acquisition of token is the same as the acquisition of appkey of the alliance. You need to Baidu console Add an application, and then add it as follows
5: Initialization token, baidu gives two ways, look at the code
private void initAccessToken() {
OCR.getInstance().initAccessToken(new OnResultListener<AccessToken>() {
@Override
public void onResult(AccessToken accessToken) {
String token = accessToken.getAccessToken();
hasGotToken = true;
}
@Override
public void onError(OCRError error) {
error.printStackTrace();
alertText("licence Method acquisition token fail", error.getMessage());
}
}, getApplicationContext());
}
private void initAccessTokenWithAkSk() {
OCR.getInstance().initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
@Override
public void onResult(AccessToken result) {
String token = result.getAccessToken();
hasGotToken = true;
}
@Override
public void onError(OCRError error) {
error.printStackTrace();
alertText("AK,SK Method acquisition token fail", error.getMessage());
}
}, getApplicationContext(), "Please fill in your AK", "Please fill in your SK");
}
High security: initAccessToken, you need to download the License file from the above figure and put it in the asset folder of the project, download the License file, and integrate it into the SDK under the text recognition, you can use the security mode to protect the security of the key in the mobile client.
Low security: initAccessTokenWithAkSk, you need to copy the as and sk contents directly from the project details to the method
6: Operation effect:
function
Photograph
Result