赞
踩
最近因为项目需要实现身份证识别与读取,对百度的文字识别进行了集成。很多地方都是参照官方文档来写,能力强的老哥可以去官方文档自行查阅。
第一步:创建项目,下载SDK
https://cloud.baidu.com/?from=console
在官网创建项目,下载SDK即可
第二步:集成SDK
1.把包内的libs文件下的SDK文件放入Android Stuio项目中的libs里
2.将libs目录下armeabi,arm64-v8a,armeabi-v7a,x86文件夹按需添加到android studio工程src/main/jniLibs目录中
当然ocr-sdk.jar包记得 右键add as library;
3.将orc_ui文件以模块方式导入到项目里
具体操作参考:https://blog.csdn.net/u013023845/article/details/85160792
第三步:代码实现
首先是布局,大家可以根据需求自己来制定
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="#F2F2F2" tools:context=".IdCardActivity"> <LinearLayout android:layout_width="match_parent" android:background="#ffffff" android:orientation="horizontal" android:layout_height="@dimen/dp_49"> <ImageView android:id="@+id/back" android:layout_width="@dimen/dp_10" android:layout_height="@dimen/dp_18" android:background="@drawable/back" android:layout_marginTop="@dimen/dp_19" android:layout_marginLeft="@dimen/dp_13" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_129" android:text="证件照片" android:textColor="#333333" android:layout_marginTop="@dimen/dp_19" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_marginTop="@dimen/dp_49" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍摄上传您的二代身份证" android:textSize="@dimen/sp_14" android:textColor="#333333" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/dp_26" /> <ImageView android:id="@+id/id_card_front_button_auto" android:layout_width="@dimen/dp_320" android:layout_height="@dimen/dp_229" android:background="@drawable/hitozura" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/dp_20" /> <ImageView android:id="@+id/id_card_back_button_auto" android:layout_width="@dimen/dp_320" android:layout_height="@dimen/dp_230" android:background="@drawable/rear" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/dp_37" /> </LinearLayout></RelativeLayout>
然后是Activity实现:
public class IdCardActivity extends AppCompatActivity { private static final int REQUEST_CODE_CAMERA = 102; private static final int REQUEST_CODE_DRIVING_LICENSE = 103; private static final int REQUEST_CODE_VEHICLE_LICENSE = 104; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_id_card); findViewById(R.id.back).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); // 正面(自动) findViewById(R.id.id_card_front_button_auto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IdCardActivity.this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true); // KEY_NATIVE_MANUAL设置了之后CameraActivity中不再自动初始化和释放模型 // 请手动使用CameraNativeHelper初始化和释放模型 // 推荐这样做,可以避免一些activity切换导致的不必要的异常 intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT); startActivityForResult(intent, REQUEST_CODE_CAMERA); } }); // 反面(自动) findViewById(R.id.id_card_back_button_auto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IdCardActivity.this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true); // KEY_NATIVE_MANUAL设置了之后CameraActivity中不再自动初始化和释放模型 // 请手动使用CameraNativeHelper初始化和释放模型 // 推荐这样做,可以避免一些activity切换导致的不必要的异常 intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK); startActivityForResult(intent, REQUEST_CODE_CAMERA); } }); // 初始化 initAccessTokenWithAkSk(); } private void initAccessTokenWithAkSk() { OCR.getInstance(IdCardActivity.this).initAccessTokenWithAkSk( new OnResultListener<AccessToken>() { @Override public void onResult(AccessToken result) { // 本地自动识别需要初始化 initLicense(); Log.d("MainActivity", "onResult: " + result.toString()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(IdCardActivity.this, "初始化认证成功", Toast.LENGTH_SHORT).show(); } }); } @Override public void onError(OCRError error) { error.printStackTrace(); Log.e("MainActivity", "onError: " + error.getMessage()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(IdCardActivity.this, "初始化认证失败,请检查 key", Toast.LENGTH_SHORT).show(); } }); } }, getApplicationContext(), // 需要自己配置 https://console.bce.baidu.com "88R6a3Fky9l5jcpOV0qNuFVh", // 需要自己配置 https://console.bce.baidu.com "a0GPi4bzLzuT9dDpU5Zp1Ce7InFDAjUw"); } private void initLicense() { CameraNativeHelper.init(this, OCR.getInstance(IdCardActivity.this).getLicense(), new CameraNativeHelper.CameraNativeInitCallback() { @Override public void onError(int errorCode, Throwable e) { final String msg; switch (errorCode) { case CameraView.NATIVE_SOLOAD_FAIL: msg = "加载so失败,请确保apk中存在ui部分的so"; break; case CameraView.NATIVE_AUTH_FAIL: msg = "授权本地质量控制token获取失败"; break; case CameraView.NATIVE_INIT_FAIL: msg = "本地质量控制"; break; default: msg = String.valueOf(errorCode); } runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(IdCardActivity.this, "本地质量控制初始化错误,错误原因: " + msg, Toast.LENGTH_SHORT).show(); } }); } }); } @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); } } } } } /** * 解析身份证图片 * * @a0GPi4bzLzuT9dDpU5Zp1Ce7InFDAjUw param idCardSide 身份证正反面 * @param filePath 图片路径 */ private void recIDCard(final String idCardSide, String filePath) { IDCardParams param = new IDCardParams(); param.setImageFile(new File(filePath)); // 设置身份证正反面 param.setIdCardSide(idCardSide); // 设置方向检测 param.setDetectDirection(true); // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20 param.setImageQuality(40); OCR.getInstance(IdCardActivity.this).recognizeIDCard(param, new OnResultListener<IDCardResult>() { @Override public void onResult(IDCardResult result) { if (result != null) { String name = ""; String sex = ""; String nation = ""; String num = ""; String address = ""; String issueAuthority = ""; String expiryDate = ""; if (result.getName() != null) { name = result.getName().toString(); } if (result.getGender() != null) { sex = result.getGender().toString(); } if (result.getEthnic() != null) { nation = result.getEthnic().toString(); } if (result.getIdNumber() != null) { num = result.getIdNumber().toString(); } if (result.getAddress() != null) { address = result.getAddress().toString(); } if (result.getIssueAuthority() != null) { issueAuthority = result.getIssueAuthority().toString(); } if (result.getExpiryDate() != null) { expiryDate = result.getExpiryDate().toString(); } if (idCardSide.equals("front")){ Toast.makeText(IdCardActivity.this, "姓名: " + name + "\n" + "性别: " + sex + "\n" + "民族: " + nation + "\n" + "身份证号码: " + num + "\n" + "住址: " + address + "\n", Toast.LENGTH_SHORT).show(); }else if (idCardSide.equals("back")){ Toast.makeText(IdCardActivity.this, "签发机关: " + issueAuthority + "\n" + "到期时间: " + expiryDate + "\n" , Toast.LENGTH_SHORT).show(); } } } @Override public void onError(OCRError error) { Toast.makeText(IdCardActivity.this, "识别出错,请查看log错误代码", Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "onError: " + error.getMessage()); } }); } @Override protected void onDestroy() { CameraNativeHelper.release(); // 释放内存资源 OCR.getInstance(IdCardActivity.this).release(); super.onDestroy(); } }
以上就是全部代码,有错请大家指正!稍后会上传Github以供大家参阅。
Demo内封装了身份证识别和银行卡绑定实例代码,使用时将Api Key和Secret Key替换为自己的就可以直接使用。
github地址:
https://github.com/qingfeng0213/CardView
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。