这篇文章主要介绍“Android如何在安卓上实现通用卡证识别”,在日常操作中,相信很多人在Android如何在安卓上实现通用卡证识别问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android如何在安卓上实现通用卡证识别”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
这里列举关键的开发步骤。
打开AndroidStudio项目级build.gradle文件,增量添加如下maven地址:
buildscript {
repositories {
maven {url 'http://developer.huawei.com/repo/'}
} }allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}}
dependencies{
// 引入基础SDK
implementation 'com.huawei.hms:ml-computer-vision-ocr:1.0.3.300'
// 引入拉丁语文字识别模型包
implementation 'com.huawei.hms:ml-computer-vision-ocr-latin-model:1.0.3.300'
// 引入银行卡识别plugin包
implementation 'com.huawei.hms:ml-computer-card-gcr-plugin:1.0.3.300' }
将以下语句添加到AndroidManifest.xml文件中:
<manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value= "ocr"/>
... </manifest>
按照官网操作指导来就行了:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-configuringobfuscation-scripts-4
都是些基本操作,废话也不多说,按照官网指导来操作:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-assigning-permissions-4
@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); }
重载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如何在安卓上实现通用卡证识别”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69970551/viewspace-2692361/