- Step 1. 把 JitPack repository 添加到build.gradle文件中 repositories的末尾:
repositories {
maven { url "https://jitpack.io" }
}
- Step 2. 在你的app build.gradle 的 dependencies 中添加依赖
dependencies {
compile 'com.github.wenchaosong:OCR_identify:3.0.9'
}
- Step 3. 获取 appkey 去百度云创建应用
打开百度云,创建应用,得到 AppKey secretKey,根据提示下载 jar 包和 lib 包,并放到项目中
- Step 4. 初始化
OCR.getInstance(this).initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
@Override
public void onResult(AccessToken result) {
}
@Override
public void onError(OCRError error) {
error.printStackTrace();
Log.d("onError", "msg: " + error.getMessage());
}
}, getApplicationContext(), "你注册的appkey", "你注册的sk");
- Step 5.拍照
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_ID_CARD_FRONT);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
- Step 6.回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) {
if (data != null) {
String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
String filePath = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
if (!TextUtils.isEmpty(contentType)) {
if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
} else if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(contentType)) {
recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
}
}
}
}
}
- Step 7.解析
private void recIDCard(String idCardSide, String filePath) {
IDCardParams param = new IDCardParams();
param.setImageFile(new File(filePath));
param.setIdCardSide(idCardSide);
param.setDetectDirection(true);
OCR.getInstance(this).recognizeIDCard(param, new OnResultListener<IDCardResult>() {
@Override
public void onResult(IDCardResult result) {
if (result != null) {
Log.d("onResult", "result: " + result.toString());
}
}
@Override
public void onError(OCRError error) {
Log.d("onError", "error: " + error.getMessage());
}
});
}
- 混淆
-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**