华为HMS MLKit提供的服务愈来愈多了,上期给你们介绍了银行卡识别的功能,从上期的介绍中咱们能够了解到使用专用的银行卡识别接口,几行代码就能够完成银行卡卡号的识别,那么有的小伙伴可能会问,我想识别的卡不是银行卡,会员卡能够识别吗,或者某一类证件号识别?也没有问题~但由于不一样商家的卡号位置,版面样式各异,因此没法像银行卡识别那样使用专用的银行卡识别API,这个时候咱们能够选用通用卡证识别的能力,通用卡证识别能够给你们提供一个标准的通用卡证对准框,你们只须要拉起这个对准框,就能够获取对准框内识别到的卡证所有内容,这个时候再针对这些内容按照必定的规则作关键信息提取,便可获取到本身想要的内容,好比连续的8位数字即为卡号,或者在某些特殊字母后的内容即为卡号内容。html
通用卡证识别,顾名思义,是各类卡证的识别,能识别的范围很广,经常使用的应用场景:java
银行卡、中国二代身份证号码识别虽然能够也可使用通用卡证识别来完成,但推荐使用华为ML Kit的专有的银行卡识别、身份证识别的API,专有卡证识别针对银行卡、身份证识别作了特别的优化,准确率更高,能够直接返回处理好之后的卡号数据,小编后续能够详细介绍下专有卡证和通用卡证识别的区别。android
给你们画了个简单的流程图,能够看到只须要把图片、拍照、视频流传给ML Kit,便可获取到对应的识别内容,针对识别内容作必定后处理便可提取出要识别的卡证号内容。git
能够看到开发流程也很是的简单,只须要启动卡证识别Activity,获取Activity识别到的内容,而后对内容作简单后处理提取关键信息就能够完成代码开发了。github
核心提示,端侧能力全免费,全终端覆盖!非华为手机也可使用机器学习
本次的开发实战中实现了对港澳通行证、回乡证、香港居民身份证的处理,你们伙可参考实现。maven
详细的准备步骤能够参考华为开发者联盟:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4ide
这里列举关键的开发步骤。函数
打开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); } }
看下Demo效果如何,是否是还不错!
源码已经上传github,你们也能够在github上一块儿完善该功能。
github源码地址:https://github.com/HMS-MLKit/HUAWEI-HMS-MLKit-Sample
通用卡证识别demo代码路径:
MLKit-Sample\module-text\src\main\java\com\mlkit\sample\activity\GeneralCardRecognitionActivity.java
更详细的开发指南参考华为开发者联盟官网
华为开发者联盟机器学习服务开发指南
往期连接:Android | 教你如何在安卓上实现二代身份证识别,一键实名认证
内容来源:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0201226181206630022&fid=18 原做者:AI_talking