一、集成身份证识别第一步首先要在有自己的AppID 和签名的前提下进行去官网 下载文字识别的SDK :
地址:https://ai.baidu.com/sdk#ocr
二、 权限 (如果清单有权限就不用添加小心demo崩溃)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
三、 将下载的SDK解压打开 如图:
把解压的文件导入您Android studio 的 project 中具体操作如下:
注意往下看
点击ok后 直接finish 这样就把文字识别的SDK导入进来了
**四、把解压的文件再打开 → 点击 libs 文件夹 把文件 Ctrl+A 直接 CV 大法粘贴到自己studio 的当前model 也要在assets 文件夹中 导入 aip.license 文件 **
五、导入依赖
1. 在android { } 中添加依赖:
/* Java版本 使用1.8 */
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//jniLibs
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
2.在dependencies { } 中加入百度识别的依赖
//识别身份证
//EventBus 依赖
implementation 'org.greenrobot:eventbus:3.1.1'
//百度识别依赖
compile 'com.android.support:appcompat-v7:25.1.1'
compile files('libs/ocr-sdk.jar')
compile project(path: ':ocr_ui')
第二行报红正常 可以当没看见 不用在意
3.在当前model 的 proguard-rules.pro 加两行效果如图
代码在这呢:
-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**
六 创建一下自己的布局
<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="#fff"
android:orientation="vertical"
tools:context=".view.activity.shoiuye.ShiName_Activity">
<LinearLayout
android:id="@+id/qq"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:orientation="horizontal"
>
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:id="@+id/xin_fan"
android:layout_margin="20dp"
android:src="@mipmap/common_icon_back_black_n"
>
ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="实名认证"
android:layout_marginRight="50dp"
android:textSize="20sp"
android:gravity="center"
>
TextView>
LinearLayout>
<TextView
android:id="@+id/ww"
android:layout_below="@+id/qq"
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#F2ECEC"
>
TextView>
<ImageView
android:layout_below="@+id/ww"
android:id="@+id/ee"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="30dp"
android:src="@mipmap/id_card_front"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
ImageView>
<ImageView
android:id="@+id/shi_xiang"
android:layout_below="@+id/ww"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/my_icon_camera_n"
android:layout_marginTop="65dp"
android:layout_centerHorizontal="true"
>
ImageView>
<TextView
android:id="@+id/shi_xiang2"
android:layout_below="@+id/shi_xiang"
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="点击拍摄/上传人面像"
android:textStyle="bold"
android:textColor="#0091EA"
>
TextView>
<ImageView
android:id="@+id/rr"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/ee"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/id_card_back"
>
ImageView>
<ImageView
android:id="@+id/tt"
android:layout_below="@+id/ee"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/my_icon_camera_n"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"
>
ImageView>
<TextView
android:id="@+id/tt2"
android:layout_below="@+id/tt"
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="点击拍摄/上传国徽面"
android:textStyle="bold"
android:textColor="#0091EA"
>
TextView>
<TextView
android:id="@+id/xiayibu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rr"
android:text="下一步"
android:gravity="center"
android:padding="10dp"
android:background="#0090E9"
android:textColor="#fff"
android:layout_margin="30dp"
>
TextView>
RelativeLayout>
七、上代码
1.新建一个类
public class FileUtilll {
public static File getSaveFile(Context context) {
File file = new File(context.getFilesDir(), "pic.jpg");
return file;
}
}
2.最后一步
public class ShiName_Activity extends AppCompatActivity {
@BindView(R.id.xin_fan)
ImageView xinFan;
@BindView(R.id.qq)
LinearLayout qq;
@BindView(R.id.ww)
TextView ww;
@BindView(R.id.ee)
ImageView ee;
@BindView(R.id.shi_xiang)
ImageView shiXiang;
@BindView(R.id.shi_xiang2)
TextView shiXiang2;
@BindView(R.id.rr)
ImageView rr;
@BindView(R.id.tt)
ImageView tt;
@BindView(R.id.tt2)
TextView tt2;
@BindView(R.id.xiayibu)
TextView xiayibu;
private static final int REQUEST_CODE_CAMERA = 102;
private String filePath; //获取到的照片
private boolean a = true; //正面照片
private boolean b = true; //反面照片
String name = "";
String sex = "";
String nation = "";
String birthday = "";
String address = "";
String idNumber = "";
String issueOffice = "";
private String s7;
private String s6;
private String s5;
private String s4;
private String s3;
private String s1;
private String s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shi_name);
ButterKnife.bind(this);
initAccessTokenWithAkSk();
}
@OnClick({
R.id.xin_fan, R.id.shi_xiang, R.id.tt, R.id.xiayibu})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.xin_fan:
finish();
break;
case R.id.shi_xiang:
Intent intent = new Intent(ShiName_Activity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
FileUtilll.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true);
intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true);
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
break;
case R.id.tt:
Intent intent1 = new Intent(ShiName_Activity.this, CameraActivity.class);
intent1.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
FileUtilll.getSaveFile(getApplication()).getAbsolutePath());
intent1.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true);
intent1.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true);
intent1.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
startActivityForResult(intent1, REQUEST_CODE_CAMERA);
break;
case R.id.xiayibu:
//判断是否上传上照片
if (a & b == true) {
Toast.makeText(ShiName_Activity.this, "请上传前后照片", Toast.LENGTH_SHORT).show();
} else if (a == true) {
Toast.makeText(ShiName_Activity.this, "请输入前照片", Toast.LENGTH_SHORT).show();
} else if (b == true) {
Toast.makeText(ShiName_Activity.this, "请输入后照片", Toast.LENGTH_SHORT).show();
} else {
try {
s1 = RsaCoder.encryptByPublicKey(name);
s2 = RsaCoder.encryptByPublicKey(sex);
s3 = RsaCoder.encryptByPublicKey(nation);
s7 = RsaCoder.encryptByPublicKey(birthday);
s4 = RsaCoder.encryptByPublicKey(idNumber);
s5 = RsaCoder.encryptByPublicKey(address);
s6 = RsaCoder.encryptByPublicKey(issueOffice);
} catch (Exception e) {
e.printStackTrace();
}
//这个是网络工具类 “根据自己的需求调用自己网络工具类”
HttpUtils.getInstance().getShenFenId(FrescoApp.date.getUserId(), s1, s2, s3, s7, s5, s4, s6, new Observer<ShenFenBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShenFenBean value) {
Toast.makeText(ShiName_Activity.this, value.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
finish();
}
break;
}
}
//在onCreate方法外写初始化,记得更换你自己的AK,SK;
private void initAccessTokenWithAkSk() {
OCR.getInstance(ShiName_Activity.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(ShiName_Activity.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(ShiName_Activity.this, "初始化认证失败,请检查 key", Toast.LENGTH_SHORT).show();
}
});
}
}, getApplicationContext(),
// 需要自己配置 https://console.bce.baidu.com
"zGgXZaxbN4xNXMRjppBXhxy6",
// 需要自己配置 https://console.bce.baidu.com
"5o6GEEZFgSPaWvkDOVtRADFSf9GGmH04");
}
private void initLicense() {
CameraNativeHelper.init(this, OCR.getInstance(ShiName_Activity.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(ShiName_Activity.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);
//照片
filePath = FileUtilll.getSaveFile(getApplicationContext()).getAbsolutePath();
if (!TextUtils.isEmpty(contentType)) {
if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
ee.setImageURI(Uri.parse(filePath));
shiXiang.setVisibility(View.GONE);
shiXiang2.setVisibility(View.GONE);
a = false;
} else if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(contentType)) {
recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
rr.setImageURI(Uri.parse(filePath));
tt.setVisibility(View.GONE);
tt2.setVisibility(View.GONE);
b = false;
}
}
}
}
}
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(ShiName_Activity.this).recognizeIDCard(param, new OnResultListener<IDCardResult>() {
@Override
public void onResult(IDCardResult result) {
if (result != null) {
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) {
idNumber = result.getIdNumber().toString(); //身份证号
}
if (result.getAddress() != null) {
address = result.getAddress().toString(); //住址
}
if (result.getBirthday() != null) {
birthday = result.getBirthday().toString(); //生日
}
if (result.getIssueAuthority() != null) {
issueOffice = result.getIssueAuthority().toString(); //签发机关
}
if (idCardSide.equals("front")) {
Toast.makeText(ShiName_Activity.this, "姓名: " + name + "\n" +
"性别: " + sex + "\n" +
"民族: " + nation + "\n" +
"身份证号码: " + idNumber + "\n" +
"住址: " + address + "\n"+
"生日: " + address + "\n", Toast.LENGTH_SHORT).show();
} else if (idCardSide.equals("back")) {
Toast.makeText(ShiName_Activity.this, "签发机关: " + issueOffice + "\n", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onError(OCRError error) {
Toast.makeText(ShiName_Activity.this, "识别出错,请查看log错误代码", Toast.LENGTH_SHORT).show();
Log.d("MainActivity", "onError: " + error.getMessage());
}
});
}
@Override
protected void onDestroy() {
CameraNativeHelper.release();
// 释放内存资源
OCR.getInstance(ShiName_Activity.this).release();
super.onDestroy();
}
}
这样就 no problem !