Android 疾速集成文档校对能力 超简略
前言
在之前的《超简略集成华为HMS ML Kit文本辨认SDK,一键实现账单号主动录入》文章中,咱们给大家介绍了华为HMS ML Kit文本辨认技术如何通过拍照自动识别照片中的文本信息。那么有的小伙伴可能会问,如果拍照时不是正对着文本拍摄,拍进去的照片是歪斜的,那么还能精确辨认文本吗?当然能够啦。HMS ML Kit文档校对技术能够自动识别文档地位,校对拍摄角度,并且反对用户自定义边界点地位,即便在歪斜角度也可能拍摄出文档的侧面图像。
利用场景
文档校对技术在生活中有很多的利用场景。比如说在拍摄纸质文档时,相机处于歪斜的角度,导致浏览文档十分不不便。应用文档校对技术能够把文档调整到正对的视角,这样浏览起来就顺利多了。
再比方在记录卡证信息时,应用文档校对技术,不须要调整到正对卡证的视角,也能够拍摄出卡证的侧面照片。
另外,在行程中因为身处于歪斜地位,道路旁的路牌难以精确辨认,这时能够通过文档校对技术拍摄到路牌侧面图片。
怎么样,是不是很不便呢?那咱们接下来具体给大家介绍安卓如何疾速集成文档校对技术。
开发实战
具体的筹备步骤能够参考华为开发者联盟:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4
这里列举要害的开发步骤。
1.1 我的项目级gradle里配置Maven仓地址
buildscript { repositories { ... maven {url 'https://developer.huawei.com/repo/'} } } dependencies { ... classpath 'com.huawei.agconnect:agcp:1.3.1.300' } allprojects { repositories { ... maven {url 'https://developer.huawei.com/repo/'} } }
1.2 利用级gradle里配置SDK依赖
dependencies{ // 引入根底SDK implementation 'com.huawei.hms:ml-computer-vision-documentskew:2.0.2.300' // 引入文档检测/校对模型包 implementation 'com.huawei.hms:ml-computer-vision-documentskew-model:2.0.2.300' }
1.3 在文件头增加配置
apply plugin: 'com.huawei.agconnect' apply plugin: 'com.android.application'
1.4 增加如下语句到AndroidManifest.xml文件中,自动更新机器学习模型到设施
<meta-data android:name="com.huawei.hms.ml.DEPENDENCY" android:value= "dsc"/>
1.5 申请摄像机权限和读本地图片权限
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2. 代码开发
2.1 创立文本框检测/校对分析器
MLDocumentSkewCorrectionAnalyzerSetting setting = new MLDocumentSkewCorrectionAnalyzerSetting.Factory().create();MLDocumentSkewCorrectionAnalyzer analyzer = MLDocumentSkewCorrectionAnalyzerFactory.getInstance().getDocumentSkewCorrectionAnalyzer(setting);
2.2 通过android.graphics.Bitmap创立MLFrame对象用于分析器检测图片,反对的图片格式包含:jpg/jpeg/png,倡议图片尺寸不小于320320像素,不大于19201920像素。
MLFrame frame = MLFrame.fromBitmap(bitmap);
2.3 调用asyncDocumentSkewDetect异步办法或analyseFrame同步办法进行文本框的检测。当返回码是MLDocumentSkewCorrectionConstant.SUCCESS时,将会返回文本框的四个顶点的坐标值,该坐标值是绝对于传入图像的坐标,若与设施坐标不统一,需调用者进行转换;否则,返回的数据没有意义。
// asyncDocumentSkewDetect异步调用。 Task<MLDocumentSkewDetectResult> detectTask = analyzer.asyncDocumentSkewDetect(mlFrame); detectTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewDetectResult>() { @Override public void onSuccess(MLDocumentSkewDetectResult detectResult) { // 检测胜利。 } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // 检测失败。 } }) // analyseFrame同步调用。 SparseArray<MLDocumentSkewDetectResult> detect = analyzer.analyseFrame(mlFrame); if (detect != null && detect.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) { // 检测胜利。 } else { // 检测失败。 }
2.4 检测胜利后,别离获取文本框四个顶点的坐标数据,而后以左上角为终点,按顺时针方向,别离把左上角、右上角、右下角、左下角退出到列表(List<Point>)中,最初构建MLDocumentSkewCorrectionCoordinateInput对象。
2.4.1 如果应用analyseFrame同步调用,先获取到检测后果,如下所示(应用asyncDocumentSkewDetect异步调用可疏忽此步骤间接进行步骤2.4.2):
MLDocumentSkewDetectResult detectResult = detect.get(0);
2.4.2 获取文本框四个顶点的坐标数据并构建MLDocumentSkewCorrectionCoordinateInput对象:
Point leftTop = detectResult.getLeftTopPosition(); Point rightTop = detectResult.getRightTopPosition(); Point leftBottom = detectResult.getLeftBottomPosition(); Point rightBottom = detectResult.getRightBottomPosition(); List<Point> coordinates = new ArrayList<>(); coordinates.add(leftTop); coordinates.add(rightTop); coordinates.add(rightBottom); coordinates.add(leftBottom); MLDocumentSkewCorrectionCoordinateInput coordinateData = new MLDocumentSkewCorrectionCoordinateInput(coordinates);
2.5 调用asyncDocumentSkewCorrect异步办法或syncDocumentSkewCorrect同步办法进行文本框的校对。
// asyncDocumentSkewCorrect异步调用。Task<MLDocumentSkewCorrectionResult> correctionTask = analyzer.asyncDocumentSkewCorrect(mlFrame, coordinateData); correctionTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewCorrectionResult>() { @Override public void onSuccess(MLDocumentSkewCorrectionResult refineResult) { // 检测胜利。 } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // 检测失败。 } }); // syncDocumentSkewCorrect同步调用。SparseArray<MLDocumentSkewCorrectionResult> correct= analyzer.syncDocumentSkewCorrect(mlFrame, coordinateData); if (correct != null && correct.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) { // 校对胜利。} else { // 校对失败。}
2.6 检测实现,进行分析器,开释检测资源。
if (analyzer != null) { analyzer.stop();}
Demo成果
上面这个demo展现了在歪斜角度扫描文档,文档校对技术能够把文档调整到正对视角。成果是不是很棒?
文档校对技术还能够辅助文档辨认技术,将歪斜的文档调整到侧面视角,疾速实现从纸质文件到电子文件的转化,大幅度晋升信息的录入效率。
Github源码
https://github.com/HMS-Core/hms-ml-demo/blob/master/MLKit-Sample/module-text/src/main/java/com/mlkit/sample/activity/DocumentSkewCorretionActivity.java
更具体的开发指南参考华为开发者联盟官网
https://developer.huawei.com/consumer/cn/hms/huawei-mlkit
欲了解更多详情,请参阅:
华为开发者联盟官网:https://developer.huawei.com/consumer/cn/hms
获取开发领导文档:https://developer.huawei.com/consumer/cn/doc/development
参加开发者探讨请到Reddit社区:https://www.reddit.com/r/HMSCore/
下载demo和示例代码请到Github:https://github.com/HMS-Core
解决集成问题请到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
文章起源:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202344452930050418&fid=18
作者:留下落叶