本文主要介绍“安卓如何在安卓上实现通用卡识别”。在日常操作中,相信很多人对安卓如何在安卓上实现通用卡识别有疑问。边肖查阅了各种资料,整理出简单易用的操作方法,希望能帮助大家解答“安卓如何在安卓上实现通用卡识别”的疑惑!接下来,请和边肖一起学习!
1 开发准备
这里列出了关键的开发步骤。
00-1010打开AndroidStudio项目级build.gradle文件,并增量添加以下maven地址:
buildscript{
存储库{
maven { URL ' http://developer . Huawei.com/repo/' }
} }所有项目{
存储库{
maven { URL ' http://developer . Huawei.com/repo/' }
}}
1.1 在项目级gradle里添加华为maven仓
依赖项{
//介绍基本的SDK
实现' com . Huawei . HMS 3360ml-computer-vision-ocr :1 . 0 . 3 . 300 '
//介绍拉丁字符识别模型包
实现' com . Huawei . HMS 3360ml-计算机视觉-ocr-latin-model:1.0.3.300 '
//介绍银行卡识别插件包
实现' com . Huawei . HMS 3360ml-computer-card-gcr-plugin 33601 . 0 . 3 . 300 ' }将以下语句添加到AndroidManifest.xml文件中:
显示
.
元数据
Android : name=' com . Huawei . HMS . ml . dependency '
android:value='ocr'/
./manifest
1.2 在应用级的build.gradle里面加上SDK依赖
只需按照官网操作说明:
https://developer . Huawei.com/consumer/cn/doc/development/HMS-Guides/ml-configuringbfuscing-scripts-4
1.3 配置混淆脚本
-link octicon octicon-link">1.4 在AndroidManifest.xml文件里面申请相机和存储权限
都是些基本操作,废话也不多说,按照官网指导来操作:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-assigning-permissions-4
2 代码开发
2.1 启动卡证识别
@Override
public void onClick(View v) {
switch (v.getId()) {
// 相册图片检测按钮。
case R.id.detect_picture:
this.startLocalImageActivity(cardImage, null, callback);
break;
// 视频流检测按钮。
case R.id.detect_video:
this.startCaptureActivity(null, callback);
break;
// 拍照检测按钮。
case R.id.detect_take_photo:
this.startTakePhotoActivity(null, callback);
break;
default:
break;
} }
视频流识别
private void startCaptureActivity(Object object, MLGcrCapture.Callback callback) {
// 创建通用卡证识别配置器。
MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().create();
// 创建通用卡证识别界面配置器。
MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()
// 设置扫描框颜色。
.setScanBoxCornerColor(Color.GREEN)
// 设置扫描框中的提示文字,建议少于30个字符。
.setTipText("Recognizing, align edges")
// 设置识别界面横竖屏,支持三种模式:
// MLGcrCaptureUIConfig.ORIENTATION_AUTO:自动模式,由物理感应器决定显示方向。
// MLGcrCaptureUIConfig.ORIENTATION_LANDSCAPE:横屏模式。
// MLGcrCaptureUIConfig.ORIENTATION_PORTRAIT:竖屏模式。
.setOrientation(MLGcrCaptureUIConfig.ORIENTATION_AUTO)
.create();
// 方式一:根据自定义的卡证识别界面配置器,创建通用卡证识别处理器。
MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
// 方式二:使用默认界面,创建通用卡证识别处理器。
MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
// 绑定通用卡证识别处理器和处理结果回调函数。
ocrManager.capturePreview(this, object, callback); }
拍照识别
private void startTakePhotoActivity(Object object, MLGcrCapture.Callback callback) {
// 创建通用卡证识别配置器。
MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().create();
// 创建通用卡证识别界面配置器。
MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()
// 设置扫描框颜色。
.setScanBoxCornerColor(Color.BLUE)
// 设置扫描框中的提示文字,建议少于30个字符。
.setTipText("Taking picture, align edges")
// 设置界面横竖屏,支持三种模式:
// MLGcrCaptureUIConfig.ORIENTATION_AUTO:自动模式,由物理感应器决定显示方向。
// MLGcrCaptureUIConfig.ORIENTATION_LANDSCAPE:横屏模式。
// MLGcrCaptureUIConfig.ORIENTATION_PORTRAIT:竖屏模式。
.setOrientation(MLGcrCaptureUIConfig.ORIENTATION_AUTO)
.create();
// 方式一:根据自定义的卡证识别界面配置器,创建通用卡证识别处理器。
MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
// 方式二:使用默认界面,创建通用卡证识别处理器。
MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
// 绑定通用卡证识别处理器和处理结果回调函数。
ocrManager.capturePhoto(this, object, callback); }
相册图片识别
private void startLocalImageActivity(Bitmap bitmap, Object object, MLGcrCapture.Callback callback) {
// 创建通用卡证识别配置器。
MLGcrCaptureConfig config = new MLGcrCaptureConfig.Factory().create();
MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(config);
// bitmap 为需要识别的Bitmap类型卡证图像,支持的图片格式包括:jpg/jpeg/png/bmp。
ocrManager.captureImage(bitmap, object, callback); }
2.2 对识别后的内容做后处理,进行关键信息提取
重载onResult, onCanceled, onFailure, onDenied四个方法;onResult表示返回了结果,MLGcrCaptureResult为卡证识别返回的结果,onCanceled 表示用户取消,onFailure 表示识别失败,onDenied 表示相机不可用等场景。
private MLGcrCapture.Callback callback = new MLGcrCapture.Callback() {
@Override
public int onResult(MLGcrCaptureResult result, Object object) {
Log.i(TAG, "callback onRecSuccess");
if (result == null) {
Log.e(TAG, "callback onRecSuccess result is null");
return MLGcrCaptureResult.CAPTURE_CONTINUE;
}
GeneralCardProcessor idCard = null;
GeneralCardResult cardResult = null;
/*港澳台通行证处理*/
if (cardTypeEnum == CardType.PASSCARD) {
idCard = new PassCardProcessor(result.text);
/*香港身份证处理*/
} else if (cardTypeEnum == CardType.HKIDCARD) {
idCard = new HKIdCardProcessor(result.text);
/*回乡证处理*/
} else if (cardTypeEnum == CardType.COMEHOMECARD) {
idCard = new HomeCardProcessor(result.text);
}
if (idCard != null) {
/*获取处理后的结果*/
cardResult = idCard.getResult();
}
showFrontImage(result.cardBitmap);
displayResult(cardResult);
// If the results don't match
if (cardResult == null || cardResult.valid.isEmpty() || cardResult.number.isEmpty()) {
return MLGcrCaptureResult.CAPTURE_CONTINUE;
}
displayResult(cardResult);
return MLGcrCaptureResult.CAPTURE_STOP;
}
};}
};
具体的卡号提取处理逻辑可以通过重写GeneralCardProcessor 类中的getResult()方法来完成,以港澳台通行证举例,更加详细的处理可以看github上的源码:
public class PassCardProcessor implements GeneralCardProcessor {
private static final String TAG = "PassCardProcessor";
private final MLText text;
public PassCardProcessor(MLText text) {
this.text = text;
}
@Override
public GeneralCardResult getResult() {
List<MLText.Block> blocks = text.getBlocks();
if (blocks.isEmpty()) {
Log.i(TAG, "Result blocks is empty");
return null;
}
ArrayList<BlockItem> originItems = getOriginItems(blocks);
String valid = "";
String number = "";
boolean validFlag = false;
boolean numberFlag = false;
for (BlockItem item : originItems) {
String tempStr = item.text;
if (!validFlag) {
String result = tryGetValidDate(tempStr);
if (!result.isEmpty()) {
valid = result;
validFlag = true;
}
}
if (!numberFlag) {
String result = tryGetCardNumber(tempStr);
if (!result.isEmpty()) {
number = result;
numberFlag = true;
}
}
}
return new GeneralCardResult(valid, number);
} }
到此,关于“Android如何在安卓上实现通用卡证识别”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/93185.html
