https://blog.csdn.net/lovelixue/article/details/82848507
上一篇讲了各大平台文字识别功能的区别和局限性,这篇就直接讲解百度云文字识别demo的实现
private void initOCR() {
OCR.getInstance(this).initAccessToken(new OnResultListener() {
@Override
public void onResult(AccessToken result) {
// 调用成功,返回AccessToken对象
String token = result.getAccessToken();
Log.i("shibie","token = " + token);
}
@Override
public void onError(OCRError error) {
// 调用失败,返回OCRError子类SDKError对象
Log.i("shibie","token = " + error.getMessage());
}
}, getApplicationContext());
}
当然 还有个SDK的资源文件下载:
https://ai.baidu.com/sdk#ocr
将里面的libs的资源放到对应的libs里面去,同步即可
接下来 就要开始搞事情了,我也不分开了,直接整个类代码拷贝下来吧
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_text_distinguish;
private Button bt_print_distinguish;
private Button bt_number_distinguish;
private Button bt_test_text_distinguish;
private static final int REQUEST_CODE_ACCURATE_BASIC = 107;
private static final int REQUEST_CODE_HANDWRITING = 129;
private static final int REQUEST_CODE_NUMBERS = 126;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_text_distinguish = findViewById(R.id.bt_text_distinguish);
bt_print_distinguish = findViewById(R.id.bt_print_distinguish);
bt_number_distinguish = findViewById(R.id.bt_number_distinguish);
bt_test_text_distinguish = findViewById(R.id.bt_test_text_distinguish);
bt_text_distinguish.setOnClickListener(this);
bt_print_distinguish.setOnClickListener(this);
bt_number_distinguish.setOnClickListener(this);
bt_test_text_distinguish.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_text_distinguish:
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
CameraActivity.CONTENT_TYPE_GENERAL);
startActivityForResult(intent, REQUEST_CODE_ACCURATE_BASIC);
break;
case R.id.bt_print_distinguish:
// 手写识别
Intent intent1 = new Intent(MainActivity.this, CameraActivity.class);
intent1.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent1.putExtra(CameraActivity.KEY_CONTENT_TYPE,
CameraActivity.CONTENT_TYPE_GENERAL);
startActivityForResult(intent1, REQUEST_CODE_HANDWRITING);
break;
case R.id.bt_number_distinguish:
Intent intent2 = new Intent(MainActivity.this, CameraActivity.class);
intent2.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent2.putExtra(CameraActivity.KEY_CONTENT_TYPE,
CameraActivity.CONTENT_TYPE_GENERAL);
startActivityForResult(intent2, REQUEST_CODE_NUMBERS);
break;
case R.id.bt_test_text_distinguish:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 识别成功回调,通用文字识别(高精度版)
if (requestCode == REQUEST_CODE_ACCURATE_BASIC && resultCode == Activity.RESULT_OK) {
RecognizeService.recAccurateBasic(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(),
new RecognizeService.ServiceListener() {
@Override
public void onResult(String result) {
infoPopText(result);
}
});
}
// 识别成功回调,手写
if (requestCode == REQUEST_CODE_HANDWRITING && resultCode == Activity.RESULT_OK) {
RecognizeService.recHandwriting(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(),
new RecognizeService.ServiceListener() {
@Override
public void onResult(String result) {
infoPopText(result);
}
});
}
// 识别成功回调,数字
if (requestCode == REQUEST_CODE_NUMBERS && resultCode == Activity.RESULT_OK) {
RecognizeService.recNumbers(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(),
new RecognizeService.ServiceListener() {
@Override
public void onResult(String result) {
infoPopText(result);
}
});
}
}
private void infoPopText(final String result) {
Log.i("shibie", "result = " + result);
alertText("", result);
}
private void alertText(final String title, final String message) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(MainActivity.this).setTitle(title)
.setMessage(message)
.setPositiveButton("确定", null)
.show();
}
});
}
我相信大家看得懂,就是一个调用library里面的相机功能,然后在返回结果中做处理,当然还需要两个辅助类,这个在官方demo就有,你可以直接拿过来使用的
这样就基本能实现了
希望能帮到大家吧