一、在百度AI官网下载文字识别的SDK
步骤一、 权限
<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" />
-
下载完解压到根目录,打开libs会有一个ocr-sdk.jar还有4个文件夹
-
把ocr-sdk.jar和四个文件夹放进项目的libs文件里
-
导入Model,把我下面指定的ocr_ui导入到项目里,OCRDemo是一个demo可以进行参考,至于怎么导入ocr_ui往下看我给大家截图看一下
-
在main下建一个新文件夹assets把下载的aip.license放进去
-
**这是导入ocr_ui的直接方式,还有其他方式导入你们也可以用你们自己的方式,不会导入的可以用我的方式进行导入 **
-
目前已经配置第三方的已经配置完了,最后再给大家一个依赖复制到自己的项目里,我的项目是weidujiangkang
这个依赖就是跟咱们导入的ocr_ui进行关联的 下面jniLibs和EventBus存入build.gradle的android里
//jniLibs
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
//EventBus
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
下面的依赖加入build.gradle的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')
- 混淆的依赖
-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**
1. xml
<ImageView
android:id="@+id/hui_ji"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:src="@mipmap/ji"
>
</ImageView>
<TextView
android:id="@+id/hao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
>
</TextView>
-
获取id点击开始扫描银行卡
BindbankActivity类
private ImageView huiJi;
private TextView hao;
private boolean hasGotToken = false;
private static final int REQUEST_CODE_BANKCARD = 111;
private String path;
//找控件
huiJi= findViewById(R.id.hui_ji);
hao = findViewById(R.id.hao);
//银行卡识别
huiJi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!checkTokenStatus()) {
return;
}
//没有加依赖这里的CameraActivity会报红,FileUtil报红是对的因为你没这个类,我在下面给你类了复制就行
Intent intent = new Intent(BindbankActivity.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);
}
});
// 请选择您的初始化方式
initAccessTokenWithAkSk();
- BindbankActivity类里的方法和回调的值
private boolean checkTokenStatus() {
if (!hasGotToken) {
Toast.makeText(getApplicationContext(), "token还未成功获取", Toast.LENGTH_LONG).show();
}
return hasGotToken;
}
//初始化
private void initAccessTokenWithAkSk() {
OCR.getInstance(this).initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
@Override
public void onResult(AccessToken result) {
String token = result.getAccessToken();
Log.i("token",token);
hasGotToken = true;
}
@Override
public void onError(OCRError error) {
error.printStackTrace();
Toast.makeText(BindbankActivity.this, "AK,SK方式获取token失败", Toast.LENGTH_SHORT).show();
}
}, getApplicationContext(), "您的应用AK", "您的应用SK");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 识别成功回调,银行卡识别
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) {
//获取的照片
path = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
huiJi.setImageURI(Uri.parse(path));
//卡号、发卡行、类型 自己设置一个文本让他显示出来看看有没有获取到
hao.setText(result);
}
});
}
}
复制
FileUtil 类
/*
* Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
*/
package com.wd.health.shezhi;
import java.io.File;
import android.content.Context;
public class FileUtil {
public static File getSaveFile(Context context) {
File file = new File(context.getFilesDir(), "pic.jpg");
return file;
}
}
下面这是获取图片上的文字和图片有身份证、车牌号、银行卡等。
/*
* Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
*/
package com.wd.health.shezhi;
import android.content.Context;
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.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.sdk.model.GeneralBasicParams;
import com.baidu.ocr.sdk.model.GeneralParams;
import com.baidu.ocr.sdk.model.GeneralResult;
import com.baidu.ocr.sdk.model.OcrRequestParams;
import com.baidu.ocr.sdk.model.OcrResponseResult;
import com.baidu.ocr.sdk.model.Word;
import com.baidu.ocr.sdk.model.WordSimple;
import com.wd.health.even.KaHaoEvent;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
/**
* Created by ruanshimin on 2017/4/20.
*/
public class RecognizeService {
interface ServiceListener {
public void onResult(String result);
}
public static void recGeneral(Context ctx, String filePath, final ServiceListener listener) {
GeneralParams param = new GeneralParams();
param.setDetectDirection(true);
param.setVertexesLocation(true);
param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeGeneral(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
Word word = (Word) wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recAccurate(Context ctx, String filePath, final ServiceListener listener) {
GeneralParams param = new GeneralParams();
param.setDetectDirection(true);
param.setVertexesLocation(true);
param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeAccurate(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
Word word = (Word) wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recAccurateBasic(Context ctx, String filePath, final ServiceListener listener) {
GeneralParams param = new GeneralParams();
param.setDetectDirection(true);
param.setVertexesLocation(true);
param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeAccurateBasic(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
WordSimple word = wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recGeneralBasic(Context ctx, String filePath, final ServiceListener listener) {
GeneralBasicParams param = new GeneralBasicParams();
param.setDetectDirection(true);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeGeneralBasic(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
WordSimple word = wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recGeneralEnhanced(Context ctx, String filePath, final ServiceListener listener) {
GeneralBasicParams param = new GeneralBasicParams();
param.setDetectDirection(true);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeGeneralEnhanced(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
WordSimple word = wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recWebimage(Context ctx, String filePath, final ServiceListener listener) {
GeneralBasicParams param = new GeneralBasicParams();
param.setDetectDirection(true);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeWebimage(param, new OnResultListener<GeneralResult>() {
@Override
public void onResult(GeneralResult result) {
StringBuilder sb = new StringBuilder();
for (WordSimple wordSimple : result.getWordList()) {
WordSimple word = wordSimple;
sb.append(word.getWords());
sb.append("\n");
}
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
//银行卡
public static void recBankCard(Context ctx, String filePath, final ServiceListener listener) {
BankCardParams param = new BankCardParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeBankCard(param, new OnResultListener<BankCardResult>() {
@Override
public void onResult(BankCardResult result) {
String res = String.format("卡号:%s\n类型:%s\n银行:%s",
result.getBankCardNumber(),
result.getBankCardType().name(),
result.getBankName());
//传过去是一个string类型的一长串拼接在了一块,我在这使用了EventBus传了一下值
KaHaoEvent kaHaoEvent = new KaHaoEvent();
kaHaoEvent.setKahao(result.getBankCardNumber());
kaHaoEvent.setLeixing(result.getBankCardType().name());
kaHaoEvent.setYinhang(result.getBankName());
EventBus.getDefault().postSticky(kaHaoEvent);
listener.onResult(res);
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recVehicleLicense(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeVehicleLicense(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recDrivingLicense(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeDrivingLicense(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recLicensePlate(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeLicensePlate(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recBusinessLicense(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeBusinessLicense(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recReceipt(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
param.putParam("detect_direction", "true");
OCR.getInstance(ctx).recognizeReceipt(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recPassport(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizePassport(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recVatInvoice(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeVatInvoice(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recQrcode(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeQrcode(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recNumbers(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeNumbers(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recLottery(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeLottery(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recBusinessCard(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeBusinessCard(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recHandwriting(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeHandwriting(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
public static void recCustom(Context ctx, String filePath, final ServiceListener listener) {
OcrRequestParams param = new OcrRequestParams();
param.putParam("templateSign", "");
param.putParam("classifierId", 0);
param.setImageFile(new File(filePath));
OCR.getInstance(ctx).recognizeCustom(param, new OnResultListener<OcrResponseResult>() {
@Override
public void onResult(OcrResponseResult result) {
listener.onResult(result.getJsonRes());
}
@Override
public void onError(OCRError error) {
listener.onResult(error.getMessage());
}
});
}
}
EventBus粘性事件
package com.wd.health.even;
/**
* @author WangZhuangYu
* @description:银行卡号\银行类型\银行
* @date :2021/1/6 15:05
*/
public class KaHaoEvent {
private String kahao;
private String leixing;
private String yinhang;
public String getKahao() {
return kahao;
}
public void setKahao(String kahao) {
this.kahao = kahao;
}
public String getLeixing() {
return leixing;
}
public void setLeixing(String leixing) {
this.leixing = leixing;
}
public String getYinhang() {
return yinhang;
}
public void setYinhang(String yinhang) {
this.yinhang = yinhang;
}
}
接收银行卡号、银行卡的类型、银行发行
private String kahao;
private String leixing;
private String yinhang;
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void ka(KaHaoEvent kaHaoEvent){
//银行卡号
kahao = kaHaoEvent.getKahao();
//银行类型
leixing = kaHaoEvent.getLeixing();
//发行卡 农业银行
yinhang = kaHaoEvent.getYinhang();
}
//注册
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
//注销
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
好了,到此为止绑定银行卡的功能就实现了,一步步按照操作细心点 思考完在复制 !!!