当前位置:   article > 正文

Android studio Android集成百度身份证识别 详细步骤_baidu ocr 加载授权文件

baidu ocr 加载授权文件

最近因为项目需要,进行了集成百度身份证识别的验证damo;

很多地方都是按照官方的来的,能力强的小伙伴可以直接看官方damo;


一,创建项目,并下载SDK


1>去百度大脑处注册账号并创建项目,地址:https://ai.baidu.com/

    1.1点击控制台,进入管理中心



1.2 进入管理中心后,点击左侧产品服务,选择文字识别



1.3 进入文字识别概览,选择创建应用,点击立即创建



1.4 按照要求填好资料,出于安全考虑还是绑定包名



1.4提示创建成功后点击 下载SDK



1.5下载 文字识别里的Android SDK



二,集成SDK

2.1 把包内的libs文件下的所有文件放入Android Stuio项目中的libs里

当然ocr-sdk.jar包记得 右键add as library


由于个人习惯的原因,官方要求的是放到jnilibs 我没有按照官方要求的做,所以还要在build.gradle里添加以下代码

sourceSets {
        main {
            jniLibs.srcDir(['libs'])
        }
    }


2.2 把orc_ui文件以模块方式导入到项目里



2.3 引入后还有最后一步,把授权文件aip.license放入到assets目录中,这步很重要,不做会导致授权失败,无法识别

授权文件 aip.license在这个位置下载




三,代码实现


首先是布局


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_idcard"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context="com.example.administrator.baiduidcard.MainActivity">
  10. <Button
  11. android:text="相册选择 正面"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/gallery_button_front"
  15. tools:ignore="HardcodedText"/>
  16. <Button
  17. android:text="相册选择 反面"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/gallery_button_back"
  21. tools:ignore="HardcodedText"/>
  22. <Button
  23. android:id="@+id/id_card_front_button"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="身份证正面"
  27. tools:ignore="HardcodedText" />
  28. <Button
  29. android:id="@+id/id_card_back_button"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="身份证反面"
  33. tools:ignore="HardcodedText" />
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="20dp"
  37. android:orientation="horizontal">
  38. <TextView
  39. android:layout_width="0dp"
  40. android:layout_weight="1"
  41. android:layout_height="match_parent"
  42. android:text="姓名"
  43. android:gravity="center"/>
  44. <TextView
  45. android:id="@+id/tv_name"
  46. android:layout_width="0dp"
  47. android:layout_weight="3"
  48. android:layout_height="20dp" />
  49. </LinearLayout>
  50. <LinearLayout
  51. android:layout_width="match_parent"
  52. android:layout_height="20dp"
  53. android:orientation="horizontal">
  54. <TextView
  55. android:layout_width="0dp"
  56. android:layout_weight="1"
  57. android:layout_height="match_parent"
  58. android:text="性别"
  59. android:gravity="center"/>
  60. <TextView
  61. android:id="@+id/tv_gender"
  62. android:layout_width="0dp"
  63. android:layout_weight="3"
  64. android:layout_height="20dp"/>
  65. </LinearLayout>
  66. <LinearLayout
  67. android:layout_width="match_parent"
  68. android:layout_height="20dp"
  69. android:orientation="horizontal">
  70. <TextView
  71. android:layout_width="0dp"
  72. android:layout_weight="1"
  73. android:layout_height="match_parent"
  74. android:text="民族"
  75. android:gravity="center"/>
  76. <TextView
  77. android:id="@+id/tv_ethnic"
  78. android:layout_width="0dp"
  79. android:layout_weight="3"
  80. android:layout_height="20dp" />
  81. </LinearLayout>
  82. <LinearLayout
  83. android:layout_width="match_parent"
  84. android:layout_height="20dp"
  85. android:orientation="horizontal">
  86. <TextView
  87. android:layout_width="0dp"
  88. android:layout_weight="1"
  89. android:layout_height="match_parent"
  90. android:text="生日"
  91. android:gravity="center"/>
  92. <TextView
  93. android:id="@+id/tv_birthday"
  94. android:layout_width="0dp"
  95. android:layout_weight="3"
  96. android:layout_height="20dp"/>
  97. </LinearLayout>
  98. <LinearLayout
  99. android:layout_width="match_parent"
  100. android:layout_height="20dp"
  101. android:orientation="horizontal">
  102. <TextView
  103. android:layout_width="0dp"
  104. android:layout_weight="1"
  105. android:layout_height="match_parent"
  106. android:text="住址"
  107. android:gravity="center"/>
  108. <TextView
  109. android:id="@+id/tv_address"
  110. android:layout_width="0dp"
  111. android:layout_weight="3"
  112. android:layout_height="20dp" />
  113. </LinearLayout>
  114. <LinearLayout
  115. android:layout_width="match_parent"
  116. android:layout_height="20dp"
  117. android:orientation="horizontal">
  118. <TextView
  119. android:layout_width="0dp"
  120. android:layout_weight="1"
  121. android:layout_height="match_parent"
  122. android:text="身份证号"
  123. android:gravity="center"/>
  124. <TextView
  125. android:id="@+id/tv_idNumber"
  126. android:layout_width="0dp"
  127. android:layout_weight="3"
  128. android:layout_height="20dp"/>
  129. </LinearLayout>
  130. <LinearLayout
  131. android:layout_width="match_parent"
  132. android:layout_height="20dp"
  133. android:orientation="horizontal">
  134. <TextView
  135. android:layout_width="0dp"
  136. android:layout_weight="1"
  137. android:layout_height="match_parent"
  138. android:text="签发机关"
  139. android:gravity="center"/>
  140. <TextView
  141. android:id="@+id/tv_issueAuthority"
  142. android:layout_width="0dp"
  143. android:layout_weight="3"
  144. android:layout_height="20dp"/>
  145. </LinearLayout>
  146. <LinearLayout
  147. android:layout_width="match_parent"
  148. android:layout_height="20dp"
  149. android:orientation="horizontal">
  150. <TextView
  151. android:layout_width="0dp"
  152. android:layout_weight="1"
  153. android:layout_height="match_parent"
  154. android:text="有效期限"
  155. android:gravity="center"/>
  156. <TextView
  157. android:id="@+id/tv_youxiaodate"
  158. android:layout_width="0dp"
  159. android:layout_weight="3"
  160. android:layout_height="20dp"/>
  161. </LinearLayout>
  162. </LinearLayout>


然后是Mainactivity

  1. package com.example.administrator.baiduidcard;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import android.provider.MediaStore;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v7.app.AlertDialog;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.text.TextUtils;
  14. import android.view.View;
  15. import android.widget.TextView;
  16. import com.baidu.ocr.sdk.OCR;
  17. import com.baidu.ocr.sdk.OnResultListener;
  18. import com.baidu.ocr.sdk.exception.OCRError;
  19. import com.baidu.ocr.sdk.model.AccessToken;
  20. import com.baidu.ocr.sdk.model.IDCardParams;
  21. import com.baidu.ocr.sdk.model.IDCardResult;
  22. import com.baidu.ocr.ui.camera.CameraActivity;
  23. import java.io.File;
  24. public class MainActivity extends AppCompatActivity {
  25. private static final int REQUEST_CODE_PICK_IMAGE_FRONT = 201;
  26. private static final int REQUEST_CODE_PICK_IMAGE_BACK = 202;
  27. private static final int REQUEST_CODE_CAMERA = 102;
  28. private AlertDialog.Builder alertDialog;
  29. private TextView tv_name,tv_gender,tv_ethnic,tv_birthday,tv_address,tv_idNumber,tv_issueAuthority,tv_youxiaodate;
  30. private boolean checkGalleryPermission() {
  31. int ret = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission
  32. .READ_EXTERNAL_STORAGE);
  33. if (ret != PackageManager.PERMISSION_GRANTED) {
  34. ActivityCompat.requestPermissions(MainActivity.this,
  35. new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
  36. 1000);
  37. return false;
  38. }
  39. return true;
  40. }
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_main);
  45. alertDialog = new AlertDialog.Builder(this);
  46. tv_name = (TextView) findViewById(R.id.tv_name);
  47. tv_gender = (TextView) findViewById(R.id.tv_gender);
  48. tv_ethnic = (TextView) findViewById(R.id.tv_ethnic);
  49. tv_birthday = (TextView) findViewById(R.id.tv_birthday);
  50. tv_address = (TextView) findViewById(R.id.tv_address);
  51. tv_idNumber = (TextView) findViewById(R.id.tv_idNumber);
  52. tv_issueAuthority = (TextView) findViewById(R.id.tv_issueAuthority);
  53. tv_youxiaodate = (TextView) findViewById(R.id.tv_youxiaodate);
  54. findViewById(R.id.gallery_button_front).setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. if (checkGalleryPermission()) {
  58. Intent intent = new Intent(Intent.ACTION_PICK);
  59. intent.setType("image/*");
  60. startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE_FRONT);
  61. }
  62. }
  63. });
  64. findViewById(R.id.gallery_button_back).setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. if (checkGalleryPermission()) {
  68. Intent intent = new Intent(Intent.ACTION_PICK);
  69. intent.setType("image/*");
  70. startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE_BACK);
  71. }
  72. }
  73. });
  74. findViewById(R.id.id_card_front_button).setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. Intent intent = new Intent(MainActivity.this, CameraActivity.class);
  78. intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
  79. FileUtil.getSaveFile(getApplication()).getAbsolutePath());
  80. intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
  81. startActivityForResult(intent, REQUEST_CODE_CAMERA);
  82. }
  83. });
  84. findViewById(R.id.id_card_back_button).setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. Intent intent = new Intent(MainActivity.this, CameraActivity.class);
  88. intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
  89. FileUtil.getSaveFile(getApplication()).getAbsolutePath());
  90. intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
  91. startActivityForResult(intent, REQUEST_CODE_CAMERA);
  92. }
  93. });
  94. OCR.getInstance().initAccessToken(new OnResultListener<AccessToken>() {
  95. @Override
  96. public void onResult(AccessToken result) {
  97. // 调用成功,返回AccessToken对象
  98. String token = result.getAccessToken();
  99. }
  100. @Override
  101. public void onError(OCRError error) {
  102. // 调用失败,返回OCRError子类SDKError对象
  103. }
  104. }, getApplicationContext());
  105. }
  106. private void recIDCard(final String idCardSide, String filePath) {
  107. IDCardParams param = new IDCardParams();
  108. param.setImageFile(new File(filePath));
  109. param.setIdCardSide(idCardSide);
  110. param.setDetectDirection(true);
  111. OCR.getInstance().recognizeIDCard(param, new OnResultListener<IDCardResult>() {
  112. @Override
  113. public void onResult(IDCardResult result) {
  114. if (result != null) {
  115. if (idCardSide.equals("front")){
  116. tv_name.setText(result.getName().toString());
  117. tv_gender.setText(result.getGender().toString());
  118. tv_ethnic.setText(result.getEthnic().toString());
  119. tv_birthday.setText(result.getBirthday().toString());
  120. tv_address.setText(result.getAddress().toString());
  121. tv_idNumber.setText(result.getIdNumber().toString());
  122. }else if (idCardSide.equals("back")){
  123. tv_issueAuthority.setText(result.getIssueAuthority().toString());
  124. tv_youxiaodate.setText(result.getSignDate()+"-"+result.getExpiryDate());
  125. }
  126. }
  127. }
  128. @Override
  129. public void onError(OCRError error) {
  130. alertText("", error.getMessage());
  131. }
  132. });
  133. }
  134. @Override
  135. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  136. super.onActivityResult(requestCode, resultCode, data);
  137. if (requestCode == REQUEST_CODE_PICK_IMAGE_FRONT && resultCode == Activity.RESULT_OK) {
  138. Uri uri = data.getData();
  139. String filePath = getRealPathFromURI(uri);
  140. recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
  141. }
  142. if (requestCode == REQUEST_CODE_PICK_IMAGE_BACK && resultCode == Activity.RESULT_OK) {
  143. Uri uri = data.getData();
  144. String filePath = getRealPathFromURI(uri);
  145. recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
  146. }
  147. if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) {
  148. if (data != null) {
  149. String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
  150. String filePath = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
  151. if (!TextUtils.isEmpty(contentType)) {
  152. if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
  153. recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
  154. } else if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(contentType)) {
  155. recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. private void alertText(final String title, final String message) {
  162. this.runOnUiThread(new Runnable() {
  163. @Override
  164. public void run() {
  165. alertDialog.setTitle(title)
  166. .setMessage(message)
  167. .setPositiveButton("确定", null)
  168. .show();
  169. }
  170. });
  171. }
  172. private String getRealPathFromURI(Uri contentURI) {
  173. String result;
  174. Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
  175. if (cursor == null) { // Source is Dropbox or other similar local file path
  176. result = contentURI.getPath();
  177. } else {
  178. cursor.moveToFirst();
  179. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  180. result = cursor.getString(idx);
  181. cursor.close();
  182. }
  183. return result;
  184. }
  185. }


还有个FileUtil工具类

  1. /*
  2. * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
  3. */
  4. package com.example.administrator.baiduidcard;
  5. import android.content.Context;
  6. import java.io.File;
  7. public class FileUtil {
  8. public static File getSaveFile(Context context) {
  9. File file = new File(context.getFilesDir(), "pic.jpg");
  10. return file;
  11. }
  12. }



以上就是全部代码,有错误还望指正,转发请注明出处!



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/683080
推荐阅读
相关标签
  

闽ICP备14008679号