赞
踩
随着发展大多数App现在都会用到身份证,银行卡等识别功能,今天就集成一下百度的OCR识别功能,或不多说,开始整:
第一步:先登录百度智能云
登录进入,没有百度账号的需要注册一下,进入界面找到如下图文字识别
进入文字识别主界面,可以看到一些你集成百度OCR识别功能的调用详细数据的,如下图
然后点击创建应用(借用下百度文档图片)
这里因为保证Ak的安全所以百度希望通过授权的方式初始化Ak,因为放到项目里面很容易被别人破解
下载如上图的文件,然后放到我们需要集成的应用里面的main下面新建一个assets文件里面
做完这些还有 一个最重要的前期准备,那就是去下载SDK(http://ai.baidu.com/sdk/#ocr)
下载完成后,得到压缩包,解压如下图
做完这些后就可以开始集成了
1.(必须)将下载包libs目录中的ocr-sdk.jar文件拷贝到工程libs目录中,并加入工程依赖。
2.(必须)将libs目录下armeabi,arm64-v8a,armeabi-v7a,x86文件夹按需添加到android studio工程src/main/jniLibs目录中。这里如果不想手动创建jniLibs这这个文件夹,只需要在gradle文件里面的Android下加上如下图这句话
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
3.可选)如果需要使用UI模块,请在Android studio中以模块方式导入下载包中的ocr-ui文件夹。这个看个人意愿,如果需求可以自己去实现,这里就介绍导入的,很简单只需要
然后一路next即可。
这里要注意的是依赖的
m
o
d
e
里面的
g
r
a
d
l
e
版本信息一定要和项目的版本信息一致,不然如意出问题
\color{red}{这里要注意的是依赖的mode里面的gradle版本信息一定要和项目的版本信息一致,不然如意出问题}
这里要注意的是依赖的mode里面的gradle版本信息一定要和项目的版本信息一致,不然如意出问题
然后就可以实现功能了,下面是项目目录以及代码
主页
M
a
i
n
A
c
t
i
v
i
t
y
\color{red}{主页MainActivity}
主页MainActivity
package com.qy.myocrdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import com.baidu.ocr.sdk.OCR; import com.baidu.ocr.sdk.OnResultListener; import com.baidu.ocr.sdk.exception.OCRError; import com.baidu.ocr.sdk.model.AccessToken; import com.baidu.ocr.ui.camera.CameraActivity; import com.qy.myocrdemo.activity.IDCardActivity; import com.qy.myocrdemo.utils.FileUtil; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /** * 主页 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView tvResult; Button btn, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16; private static final int REQUEST_CODE_GENERAL = 105; private static final int REQUEST_CODE_GENERAL_BASIC = 106; private static final int REQUEST_CODE_ACCURATE_BASIC = 107; private static final int REQUEST_CODE_ACCURATE = 108; private static final int REQUEST_CODE_GENERAL_ENHANCED = 109; private static final int REQUEST_CODE_GENERAL_WEBIMAGE = 110; private static final int REQUEST_CODE_BANKCARD = 111; private static final int REQUEST_CODE_VEHICLE_LICENSE = 120; private static final int REQUEST_CODE_DRIVING_LICENSE = 121; private static final int REQUEST_CODE_LICENSE_PLATE = 122; private static final int REQUEST_CODE_BUSINESS_LICENSE = 123; private static final int REQUEST_CODE_RECEIPT = 124; private static final int REQUEST_CODE_PASSPORT = 125; private static final int REQUEST_CODE_NUMBERS = 126; private static final int REQUEST_CODE_QRCODE = 127; private static final int REQUEST_CODE_BUSINESSCARD = 128; private static final int REQUEST_CODE_HANDWRITING = 129; private static final int REQUEST_CODE_LOTTERY = 130; private static final int REQUEST_CODE_VATINVOICE = 131; private static final int REQUEST_CODE_CUSTOM = 132; private boolean hasGotToken = false; private AlertDialog.Builder alertDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.result); btn = findViewById(R.id.btn); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn3 = findViewById(R.id.btn3); btn4 = findViewById(R.id.btn4); btn5 = findViewById(R.id.btn5); btn6 = findViewById(R.id.btn6); btn7 = findViewById(R.id.btn7); btn8 = findViewById(R.id.btn8); btn9 = findViewById(R.id.btn9); btn10 = findViewById(R.id.btn10); btn11 = findViewById(R.id.btn11); btn12 = findViewById(R.id.btn12); btn13 = findViewById(R.id.btn13); btn14 = findViewById(R.id.btn14); btn15 = findViewById(R.id.btn15); btn16 = findViewById(R.id.btn16); alertDialog = new AlertDialog.Builder(this); initAccessToken(); btn.setOnClickListener(this); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btn10.setOnClickListener(this); btn11.setOnClickListener(this); btn12.setOnClickListener(this); btn13.setOnClickListener(this); btn14.setOnClickListener(this); btn15.setOnClickListener(this); btn16.setOnClickListener(this); } /** * 检测token是否未成功获取 * * @return */ private boolean checkTokenStatus() { if (!hasGotToken) { Toast.makeText(getApplicationContext(), "token还未成功获取", Toast.LENGTH_LONG).show(); } return hasGotToken; } /** * 以license文件方式初始化 */ private void initAccessToken() { OCR.getInstance(this).initAccessToken(new OnResultListener<AccessToken>() { @Override public void onResult(AccessToken accessToken) { String token = accessToken.getAccessToken(); hasGotToken = true; } @Override public void onError(OCRError error) { error.printStackTrace(); alertText("licence方式获取token失败", error.getMessage()); } }, getApplicationContext()); } /** * 提示弹出框 * * @param title * @param message */ private void alertText(final String title, final String message) { this.runOnUiThread(new Runnable() { @Override public void run() { alertDialog.setTitle(title) .setMessage(message) .setPositiveButton("确定", null) .show(); } }); } /** * 回调结果 * * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // 识别成功回调,通用文字识别(含位置信息) if (requestCode == REQUEST_CODE_GENERAL && resultCode == Activity.RESULT_OK) { RecognizeService.recGeneral(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用文字识别(含位置信息)", result); } }); } // 识别成功回调,通用文字识别(含位置信息高精度版) if (requestCode == REQUEST_CODE_ACCURATE && resultCode == Activity.RESULT_OK) { RecognizeService.recAccurate(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用文字识别(含位置信息高精度版)", result); } }); } // 识别成功回调,通用文字识别 if (requestCode == REQUEST_CODE_GENERAL_BASIC && resultCode == Activity.RESULT_OK) { RecognizeService.recGeneralBasic(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用文字识别", result); } }); } // 识别成功回调,通用文字识别(高精度版) 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) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用文字识别(高精度版)", result); } }); } // 识别成功回调,通用文字识别(含生僻字版) if (requestCode == REQUEST_CODE_GENERAL_ENHANCED && resultCode == Activity.RESULT_OK) { RecognizeService.recGeneralEnhanced(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用文字识别(含生僻字版)", result); } }); } // 识别成功回调,网络图片文字识别 if (requestCode == REQUEST_CODE_GENERAL_WEBIMAGE && resultCode == Activity.RESULT_OK) { RecognizeService.recWebimage(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,网络图片文字识别", result); } }); } // 识别成功回调,银行卡识别 if (requestCode == REQUEST_CODE_BANKCARD && resultCode == Activity.RESULT_OK) { RecognizeService.recBankCard(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,银行卡识别", result); } }); } // 识别成功回调,行驶证识别 if (requestCode == REQUEST_CODE_VEHICLE_LICENSE && resultCode == Activity.RESULT_OK) { RecognizeService.recVehicleLicense(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,行驶证识别", result); } }); } // 识别成功回调,驾驶证识别 if (requestCode == REQUEST_CODE_DRIVING_LICENSE && resultCode == Activity.RESULT_OK) { RecognizeService.recDrivingLicense(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,驾驶证识别", result); } }); } // 识别成功回调,车牌识别 if (requestCode == REQUEST_CODE_LICENSE_PLATE && resultCode == Activity.RESULT_OK) { RecognizeService.recLicensePlate(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,车牌识别", result); } }); } // 识别成功回调,营业执照识别 if (requestCode == REQUEST_CODE_BUSINESS_LICENSE && resultCode == Activity.RESULT_OK) { RecognizeService.recBusinessLicense(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,营业执照识别", result); } }); } // 识别成功回调,通用票据识别 if (requestCode == REQUEST_CODE_RECEIPT && resultCode == Activity.RESULT_OK) { RecognizeService.recReceipt(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,通用票据识别", result); } }); } // 识别成功回调,护照 if (requestCode == REQUEST_CODE_PASSPORT && resultCode == Activity.RESULT_OK) { RecognizeService.recPassport(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,护照", result); } }); } // 识别成功回调,二维码 if (requestCode == REQUEST_CODE_QRCODE && resultCode == Activity.RESULT_OK) { RecognizeService.recQrcode(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,二维码", result); } }); } // 识别成功回调,彩票 if (requestCode == REQUEST_CODE_LOTTERY && resultCode == Activity.RESULT_OK) { RecognizeService.recLottery(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,彩票", result); } }); } // 识别成功回调,增值税发票 if (requestCode == REQUEST_CODE_VATINVOICE && resultCode == Activity.RESULT_OK) { RecognizeService.recVatInvoice(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,增值税发票", 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) { tvResult.setText("结果:" + result); alertText("识别成功回调,数字", 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) { tvResult.setText("结果:" + result); alertText("识别成功回调,手写", result); } }); } // 识别成功回调,名片 if (requestCode == REQUEST_CODE_BUSINESSCARD && resultCode == Activity.RESULT_OK) { RecognizeService.recBusinessCard(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,名片", result); } }); } // 识别成功回调,自定义模板 if (requestCode == REQUEST_CODE_CUSTOM && resultCode == Activity.RESULT_OK) { RecognizeService.recCustom(this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(), new RecognizeService.ServiceListener() { @Override public void onResult(String result) { tvResult.setText("结果:" + result); alertText("识别成功回调,自定义模板", result); } }); } } /** * 释放资源 */ @Override protected void onDestroy() { super.onDestroy(); // 释放内存资源 OCR.getInstance(this).release(); } @Override public void onClick(View view) { Intent intent = null; //检测token是否成功获取 if (!checkTokenStatus()) { return; } switch (view.getId()) { case R.id.btn: //身份证识别 intent = new Intent(MainActivity.this, IDCardActivity.class); startActivity(intent); break; case R.id.btn1: //通用文字识别 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_GENERAL_BASIC); break; case R.id.btn2: //二维码识别 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_QRCODE); break; case R.id.btn3: //网络图片文字识别 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_GENERAL_WEBIMAGE); break; case R.id.btn4: //银行卡识别 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_BANK_CARD); startActivityForResult(intent, REQUEST_CODE_BANKCARD); break; case R.id.btn5: //行驶证识别 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_VEHICLE_LICENSE); break; case R.id.btn6: //驾驶证识别 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_DRIVING_LICENSE); break; case R.id.btn7: //车牌识别 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_LICENSE_PLATE); break; case R.id.btn8: //营业执照识别 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_BUSINESS_LICENSE); break; case R.id.btn9: //通用票据识别 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_RECEIPT); break; case R.id.btn10: //护照识别 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_PASSPORT); startActivityForResult(intent, REQUEST_CODE_PASSPORT); break; case R.id.btn11: //数字识别 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_NUMBERS); break; case R.id.btn12: //名片识别 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_BUSINESSCARD); break; case R.id.btn13: //增值税发票识别 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_VATINVOICE); break; case R.id.btn14: //彩票识别 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_LOTTERY); break; case R.id.btn15: //手写识别 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_HANDWRITING); break; case R.id.btn16: //自定义模板 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_CUSTOM); break; } } }
M a i n A c t i v i t y 的布局文件 \color{red}{MainActivity的布局文件} MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="身份证识别" /> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="通用文字识别" /> </LinearLayout> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="15dp" android:text="二维码识别(失败据说要创建工单开通权限有点麻烦)" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="网络图片文字识别" /> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="银行卡识别" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="行驶证识别" /> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="驾驶证识别" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="车牌识别(有时会失败)" /> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="营业执照识别" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="通用票据识别" /> <Button android:id="@+id/btn10" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="护照识别(容易检测失败)" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn11" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="数字识别" /> <Button android:id="@+id/btn12" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="名片识别(多次没有成功)" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn13" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="增值税发票识别" /> <Button android:id="@+id/btn14" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="彩票识别(多次没有成功)" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn15" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="手写识别" /> <Button android:id="@+id/btn16" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:padding="15dp" android:text="自定义模板(不明白)" /> </LinearLayout> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Hello World!" android:textSize="16sp" /> </LinearLayout>
身份证识别 I D C a r d A c t i v i t y \color{red}{身份证识别IDCardActivity} 身份证识别IDCardActivity
package com.qy.myocrdemo.activity; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.TextUtils; import android.view.View; import com.baidu.ocr.sdk.OCR; import com.baidu.ocr.sdk.OnResultListener; import com.baidu.ocr.sdk.exception.OCRError; import com.baidu.ocr.sdk.model.IDCardParams; import com.baidu.ocr.sdk.model.IDCardResult; import com.baidu.ocr.ui.camera.CameraActivity; import com.baidu.ocr.ui.camera.CameraNativeHelper; import com.qy.myocrdemo.R; import com.qy.myocrdemo.utils.FileUtil; import java.io.File; /** * 身份证识别 */ public class IDCardActivity extends AppCompatActivity { private static final int REQUEST_CODE_PICK_IMAGE_FRONT = 201; private static final int REQUEST_CODE_PICK_IMAGE_BACK = 202; private static final int REQUEST_CODE_CAMERA = 102; private AlertDialog.Builder alertDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_idcard); alertDialog = new AlertDialog.Builder(this); // 身份证正面拍照 findViewById(R.id.id_card_front_button).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_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT); startActivityForResult(intent, REQUEST_CODE_CAMERA); } }); // 身份证正面扫描 findViewById(R.id.id_card_front_button_native).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).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_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK); startActivityForResult(intent, REQUEST_CODE_CAMERA); } }); // 身份证反面扫描 findViewById(R.id.id_card_back_button_native).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); } }); } /** * 弹出提示框 * * @param title * @param message */ private void alertText(final String title, final String message) { this.runOnUiThread(new Runnable() { @Override public void run() { alertDialog.setTitle(title) .setMessage(message) .setPositiveButton("确定", null) .show(); } }); } private void recIDCard(String idCardSide, String filePath) { IDCardParams param = new IDCardParams(); param.setImageFile(new File(filePath)); // 设置身份证正反面 param.setIdCardSide(idCardSide); // 设置方向检测 param.setDetectDirection(true); // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20 param.setImageQuality(20); OCR.getInstance(this).recognizeIDCard(param, new OnResultListener<IDCardResult>() { @Override public void onResult(IDCardResult result) { if (result != null) { alertText("", result.toString()); } } @Override public void onError(OCRError error) { alertText("", error.getMessage()); } }); } private String getRealPathFromURI(Uri contentURI) { String result; Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close(); } return result; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_PICK_IMAGE_FRONT && resultCode == Activity.RESULT_OK) { Uri uri = data.getData(); String filePath = getRealPathFromURI(uri); recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath); } if (requestCode == REQUEST_CODE_PICK_IMAGE_BACK && resultCode == Activity.RESULT_OK) { Uri uri = data.getData(); String filePath = getRealPathFromURI(uri); recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath); } 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); } } } } } @Override protected void onDestroy() { // 释放本地质量控制模型 CameraNativeHelper.release(); super.onDestroy(); } }
I D C a r d A c t i v i t y 布局文件 \color{red}{IDCardActivity布局文件} IDCardActivity布局文件
<?xml version="1.0" encoding="utf-8"?><!-- ~ Copyright (C) 2017 Baidu, Inc. All Rights Reserved. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_idcard" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/cardview_default_radius" android:paddingTop="@dimen/cardview_default_radius" android:paddingRight="@dimen/cardview_default_radius" android:paddingBottom="@dimen/cardview_default_radius" tools:context=".activity.IDCardActivity"> <Button android:id="@+id/gallery_button_front" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="相册选择 正面" android:visibility="gone" tools:ignore="HardcodedText" /> <Button android:id="@+id/gallery_button_back" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="相册选择 反面" android:visibility="gone" tools:ignore="HardcodedText" /> <Button android:id="@+id/id_card_front_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="身份证正面" tools:ignore="HardcodedText" /> <Button android:id="@+id/id_card_front_button_native" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="身份证正面(本地质量控制)" tools:ignore="HardcodedText" /> <Button android:id="@+id/id_card_back_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="身份证反面" tools:ignore="HardcodedText" /> <Button android:id="@+id/id_card_back_button_native" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="身份证反面(本地质量控制)" tools:ignore="HardcodedText" /> <TextView android:id="@+id/info_text_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
弹出提示布局文件 \color{red}{弹出提示布局文件} 弹出提示布局文件
<?xml version="1.0" encoding="utf-8"?> <!-- ~ Copyright (C) 2017 Baidu, Inc. All Rights Reserved. --> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" android:fadingEdge="vertical" style="@style/result_pop_scroll" android:id="@+id/result_pop" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" style="@style/result_pop_scrollinner" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/result_text" /> </LinearLayout> </ScrollView>
到这里集成就完成了,有点烂,大家有需要的凑合看吧
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。