运行效果图
识别到的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23{ "words_result":[ { "words":"突然间有想看书的冲动" }, { "words":"eel" }, { "words":"你不是直正的快乐" }, { "words":"你说你有点难追" }, { "words":"我就打断你的腿CN" } ], "log_id":1329724251397160960, "words_result_num":5, "direction":0 }
百度SDK文字识别
- 一、创建平台应用
- 二、创建及配置Android项目
- 三、初始化
- 四、UI模块导入
- 五、UI模块使用
- 六、高精度文字识别
- 七、源码
一、创建平台应用
要使用百度文字识别,自然免不了要注册该平台的账号,否则凭什么让你使用,点击百度智能云进入,没有账号的可以先注册账号,注册应该就不用我讲解了吧?这里默认都有账号了,然后登录。
登录后 然后进入控制台,找到文字识别,然后点击。
可以看到这个文字识别SDK的使用场景,本文介绍的是通用场景,然后点击创建应用。
输入应用名称,指的是你在百度智能云上创建的文字识别应用。
向下滚动。填写相关资料之后,点击立即创建
创建完成之后,点击查看应用详情。
注意API Key、Secret Key这两个值,而这个包名就是你项目的包名。
点击上方下载SDK
下载到本地,然后解压。
解压之后文件如下图所示,里面的文件在项目中是要用到的。
二、创建及配置Android项目
打开Android Studio,创建一个名为TextOCRDemo的项目,包名和你刚才的要一致。
然后打开目录
先将libs目录中的ocr-sdk.jar文件拷贝到工程libs目录中,并加入工程依赖。
然后出现
点击OK,等待添加完成,然后打开你的app下的build.gradle中,你会发现多一句依赖。
然后在main下创建一个jniLibs文件夹。将libs目录下armeabi,arm64-v8a,armeabi-v7a,x86文件夹添加到工程src/main/jniLibs
目录中,如下图所示
创建好之后打开AndroidManifest.xml,注册一些需要用到的权限。
1
2
3
4
5<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"/>
三、初始化
然后进入到MainActivity中进行SDK的初始化。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37/** * 对应百度平台上的应用apiKey */ private String apiKey = "gQm5vnWxGuz5khN4IZ16yriL"; /** * 对应百度平台上的应用secretKey */ private String secretKey = "c8t796hbq0DXdsngSsOou5FCK2fFckpn"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTextSDK(); } /** * 用明文ak,sk初始化 */ private void initTextSDK() { OCR.getInstance(this).initAccessTokenWithAkSk(new OnResultListener<AccessToken>() { @Override public void onResult(AccessToken result) { String token = result.getAccessToken(); Log.d("result-->","成功!"+token); } @Override public void onError(OCRError error) { error.printStackTrace(); Log.d("result-->","失败!"+error.getMessage()); } }, getApplicationContext(), apiKey, secretKey); }
运行之后打印日志
四、UI模块导入
File → New → Import Module…
ocr_ui就是之前解压之后的一个文件,你可以放在一个好找的位置。
点击Finish,导入
然后发现报错了,模块中的gradle与app中的不一致。
点击Remove Build Tools version and sync project,删除构建项目并同步版本。
点击Do Refactor,发现还有报错。打开ocr_ui模块的build.gradle
修改如下,然后Sync
1
2
3
4
5
6
7
8
9
10dependencies { androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'com.android.support:appcompat-v7:25.1.1' testImplementation 'junit:junit:4.12' implementation files('libs/license.jar') }
现在就不报错了。为了确保导入的模块不会影响到之前的项目,现在称还没有用之前,先运行一下,运行到真机上,没有问题的话就说明这个模块导入正常,可以使用了。
五、UI模块使用
使用其他的模块也是需要添加依赖的,打开app的build.gradle,
1
2implementation project(path: ':ocr_ui')
然后Sync,完成后。你就可以在app中使用ocr_ui中的方法了。
首先修改activity_main.xml,这里只放了一个按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/general_basic_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="generalBasic" android:text="通用文字识别" /> </LinearLayout>
然后在MainActivity中创建一个generalBasic方法。如下:
1
2
3
4
5
6
7
8/** * 通用文字识别 * @param view */ public void generalBasic(View view) { }
然后创建一个成员变量
1
2
3
4
5/** * 通用文字识别请求码 */ private static final int REQUEST_CODE_GENERAL_BASIC = 100;
创建获取保存文件的方法和Toast提示的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19/** * Toast提示 * @param msg */ private void showMsg(String msg) { Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } /** * 获取保存文件 * @param context * @return */ public static File getSaveFile(Context context) { File file = new File(context.getFilesDir(), "pic.jpg"); return file; }
点击按钮时调用generalBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
14/** * 通用文字识别 * @param view */ public void generalBasic(View view) { Intent intent = new Intent(MainActivity.this, CameraActivity.class); //传入文件保存的路径 intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, getSaveFile(getApplication()).getAbsolutePath()); //传入文件类型 intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL); //跳转页面时传递请求码,返回时根据请求码判断获取识别的数据。 startActivityForResult(intent, REQUEST_CODE_GENERAL_BASIC); }
在com.llw.textocr包下新建一个RecognizeService类
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410/* * Copyright (C) 2017 Baidu, Inc. All Rights Reserved. */ package com.llw.textocr; import android.content.Context; import com.baidu.ocr.sdk.OCR; import com.baidu.ocr.sdk.OnResultListener; import com.baidu.ocr.sdk.exception.OCRError; import com.baidu.ocr.sdk.model.BankCardParams; import com.baidu.ocr.sdk.model.BankCardResult; import com.baidu.ocr.sdk.model.GeneralBasicParams; import com.baidu.ocr.sdk.model.GeneralParams; import com.baidu.ocr.sdk.model.GeneralResult; import com.baidu.ocr.sdk.model.OcrRequestParams; import com.baidu.ocr.sdk.model.OcrResponseResult; import com.baidu.ocr.sdk.model.Word; import com.baidu.ocr.sdk.model.WordSimple; import java.io.File; /** * Created by ruanshimin on 2017/4/20. */ public class RecognizeService { interface ServiceListener { public void onResult(String result); } public static void recGeneral(Context ctx, String filePath, final ServiceListener listener) { GeneralParams param = new GeneralParams(); param.setDetectDirection(true); param.setVertexesLocation(true); param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeGeneral(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { Word word = (Word) wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recAccurate(Context ctx, String filePath, final ServiceListener listener) { GeneralParams param = new GeneralParams(); param.setDetectDirection(true); param.setVertexesLocation(true); param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeAccurate(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { Word word = (Word) wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recAccurateBasic(Context ctx, String filePath, final ServiceListener listener) { GeneralParams param = new GeneralParams(); param.setDetectDirection(true); param.setVertexesLocation(true); param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeAccurateBasic(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { WordSimple word = wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recGeneralBasic(Context ctx, String filePath, final ServiceListener listener) { GeneralBasicParams param = new GeneralBasicParams(); param.setDetectDirection(true); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeGeneralBasic(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { WordSimple word = wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recGeneralEnhanced(Context ctx, String filePath, final ServiceListener listener) { GeneralBasicParams param = new GeneralBasicParams(); param.setDetectDirection(true); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeGeneralEnhanced(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { WordSimple word = wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recWebimage(Context ctx, String filePath, final ServiceListener listener) { GeneralBasicParams param = new GeneralBasicParams(); param.setDetectDirection(true); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeWebimage(param, new OnResultListener<GeneralResult>() { @Override public void onResult(GeneralResult result) { StringBuilder sb = new StringBuilder(); for (WordSimple wordSimple : result.getWordList()) { WordSimple word = wordSimple; sb.append(word.getWords()); sb.append("n"); } listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recBankCard(Context ctx, String filePath, final ServiceListener listener) { BankCardParams param = new BankCardParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeBankCard(param, new OnResultListener<BankCardResult>() { @Override public void onResult(BankCardResult result) { String res = String.format("卡号:%sn类型:%sn发卡行:%s", result.getBankCardNumber(), result.getBankCardType().name(), result.getBankName()); listener.onResult(res); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recVehicleLicense(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeVehicleLicense(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recDrivingLicense(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeDrivingLicense(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recLicensePlate(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeLicensePlate(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recBusinessLicense(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeBusinessLicense(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recReceipt(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); param.putParam("detect_direction", "true"); OCR.getInstance(ctx).recognizeReceipt(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recPassport(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizePassport(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recVatInvoice(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeVatInvoice(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recQrcode(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeQrcode(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recNumbers(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeNumbers(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recLottery(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeLottery(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recBusinessCard(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeBusinessCard(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recHandwriting(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeHandwriting(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } public static void recCustom(Context ctx, String filePath, final ServiceListener listener) { OcrRequestParams param = new OcrRequestParams(); param.putParam("templateSign", ""); param.putParam("classifierId", 0); param.setImageFile(new File(filePath)); OCR.getInstance(ctx).recognizeCustom(param, new OnResultListener<OcrResponseResult>() { @Override public void onResult(OcrResponseResult result) { listener.onResult(result.getJsonRes()); } @Override public void onError(OCRError error) { listener.onResult(error.getMessage()); } }); } }
这是百度开发人员写的一个工具类,用来处理返回的数据的。
然后在MainActivity中重写onActivityResult方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/** * Activity回调 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // 识别成功回调,通用文字识别 if (requestCode == REQUEST_CODE_GENERAL_BASIC && resultCode == Activity.RESULT_OK) { RecognizeService.recGeneralBasic(this, getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { showMsg(result); Log.d("result-->",result); } }); } }
返回时根据传递文件路径进行解析,这里解析的是一个图片,因为跳转的是一个相机Actiivty,拍照之后进行内容区域选取,然后返回。现在运行,你就会发现又报错了,这是改变了ocr_ui模块的gradle版本造成的。
1
2
3
4import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.annotation.IntDef;
这两个包没有了,需要改变为androidx下的。
1
2
3
4import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.annotation.IntDef;
这个要改的类比较多,慢慢改就好了,都改好之后再运行。
日志如下:
OK,就这么愉快而又简单的完成了。
六、高精度文字识别
通过之前的操作已经可以简单是识别拍照的文字,但是还不够,原因有两点,第一就是识别不准确,第二是返回的数据不够简洁。下面就来解决这两个点。
打开activity_main.xml,在里面增加一个按钮。
1
2
3
4
5
6
7<Button android:id="@+id/high_precision_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="highPrecision" android:text="通用文字识别(高精度)" />
然后进入到MainActivity中。
1
2
3
4
5
6
7
8
9
10/** * 通用文字识别(高精度)请求码 */ private static final int REQUEST_CODE_ACCURATE_BASIC = 101; /** * 弹窗 */ private AlertDialog.Builder mDialog;
在onCreate中
然后创建一个highPrecision方法
1
2
3
4
5
6
7
8
9
10
11
12/** * 通用文字识别 (高精度版) * * @param view */ public void highPrecision(View view) { Intent intent = new Intent(MainActivity.this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL); startActivityForResult(intent, REQUEST_CODE_ACCURATE_BASIC); }
高精度和普通的区别就在于不同的请求码,而关键就在于返回的处理上。
在onActivityResult中
1
2
3
4
5
6
7
8
9
10
11
12
13// 识别成功回调,通用文字识别(高精度版) if (requestCode == REQUEST_CODE_ACCURATE_BASIC && resultCode == Activity.RESULT_OK) { RecognizeService.recAccurateBasic(this, getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { //弹窗显示识别内容 showDialog(result); Log.d("result-->", result); } }); }
现在高精度识别已经有了,下面就是对JSON字符串的处理了,打开app的build.gradle,添加如下依赖:
1
2
3//GSON解析 implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
然后Sync同步一下,同步之后回到MainActivity中写showDialog方法。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/** * 显示识别结果弹窗 * @param result */ private void showDialog(final String result) { this.runOnUiThread(new Runnable() { @Override public void run() { //数据解析 Gson将JSON字符串转为实体Bean TextResult textResult = new Gson().fromJson(result, TextResult.class); if (textResult.getWords_result() == null && textResult.getWords_result().size() <= 0) { return; } String text = ""; //数据不为空并且大于0 for(int i = 0;i<textResult.getWords_result().size();i++){ text += textResult.getWords_result().get(i).getWords()+"n"; } mDialog.setMessage(text) .setPositiveButton("确定", null) .show(); } }); }
拿到高精度识别之后的JOSN字符串之后,进行转换,转换之后对数据进行判断,不为空则进行遍历,遍历完之后将数据显示在弹窗上。代码写完了,来运行一下吧。不过说真的,这是一个技术活,手抖一下就模糊了,就又有重新再拍,不过百度好像没有做防抖,不知道啥时候加上呢?
运行效果如下图:
七、源码
GitHub源码地址:TextOCRDemo
CSDN资源地址:TextOCRDemo.rar
最后
以上就是整齐白羊最近收集整理的关于Android 百度文字识别(详细步骤+源码)的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
发表评论 取消回复