在市场上有许多QR码工具和SDK。对于Android开发,Google ML Kit免费的、支持一般的QR码检测。然而,它不能满足高密度的QR码检测的要求。
作为一个商业条码SDK,Dynamsoft Barcode Reader可以覆盖特殊条码类型。在这篇文章中,我们将简化ML Kit的样本并将其与Dynamsoft Barcode SDK集成,以比较ML Kit和Dynamsoft Barcode Reader。
Barcode SDK Download
① ML Kit: 在 AndroidManifest.xml添加: <meta-dataandroid:name=”com.google.mlkit.vision.DEPENDENCIES”android:value=”barcode”/>
在build.gradle添加:dependencies { implementation ‘com.google.mlkit:barcode-scanning:17.0.0’ }
你可以在生成的apk文件中找到模型文件 apk/assets/mlkit_barcode_models/barcode_ssd_mobilenet_v1_dmp25_quant.tflite
② Dynamsoft Barcode Reader:下载地址点击
解压SDK并复制文件DynamsoftBarcodeReaderAndroid.aar到项目库,并在build.gradle添加:dependencies { implementation fileTree(dir: ‘libs’, include: [‘*.jar’, ‘*.aar’]) }
另外,您也可以在build.gradle文件中配置Maven仓库,方法如下:
allprojects { repositories { google() jcenter() maven { url "https://download2.dynamsoft.com/maven/dbr/aar" } } } dependencies { implementation 'com.dynamsoft:dynamsoftbarcodereader:8.8.0@aar' }
从静态图像中读取QR码
用谷歌ML套件扫描QR码,程序示例代码下载 这是一个优秀的样本应用程序,具有物体检测、人脸检测、文本识别、条形码扫描、图像标签、自定义图像标签、姿势检测和自拍分割的功能。
为了专注于静止图像的检测,我们只保留StillImageActivity,并在AndroidManifest.xml中把它设置为LAUNCHER。
<activity android_name=".java.StillImageActivity" android_exported="true" android_theme="@style/AppTheme"> <intent-filter> <action android_name="android.intent.action.MAIN"/> <category android_name="android.intent.category.LAUNCHER"/> </intent-filter></activity>
用Dynamsoft的SDK扫描QR码
我们创建一个dynamsoftbarcodescanner文件夹和两个类文件。DynamsoftBarcodeGraphic.java和DynamsoftBarcodeProcessor.java。
DynamsoftBarcodeGraphic类是用来绘制检测结果的:
@Overridepublic void draw(Canvas canvas) { if (result == null) { throw new IllegalStateException("Attempting to draw a null barcode."); } // Draws the bounding box around the BarcodeBlock. Point[] points = result.localizationResult.resultPoints; int minx = points[0].x; int miny = points[0].y; int maxx = points[0].x; int maxy = points[0].y; for (int i = 1; i < 4; i++) { if (points[i].x < minx) { minx = points[i].x; } else if (points[i].x > maxx) { maxx = points[i].x; } if (points[i].y < miny) { miny = points[i].y; } else if (points[i].y > maxy) { maxy = points[i].y; } } RectF rect = new RectF(minx, miny, maxx, maxy); // If the image is flipped, the left will be translated to right, and the right to left. float x0 = translateX(rect.left); float x1 = translateX(rect.right); rect.left = min(x0, x1); rect.right = max(x0, x1); rect.top = translateY(rect.top); rect.bottom = translateY(rect.bottom); canvas.drawRect(rect, rectPaint); // Draws other object info. float lineHeight = TEXT_SIZE + (2 * STROKE_WIDTH); float textWidth = barcodePaint.measureText(result.barcodeText); canvas.drawRect( rect.left - STROKE_WIDTH, rect.top - lineHeight, rect.left + textWidth + (2 * STROKE_WIDTH), rect.top, labelPaint); // Renders the barcode at the bottom of the box. canvas.drawText(result.barcodeText, rect.left, rect.top - STROKE_WIDTH, barcodePaint);}
DynamsoftBarcodeProcessor类用于解码QR码。当我们加载一个图像文件时,在DynamsoftBarcodeProcessor.java中会触发processBitmap(Bitmap bitmap, final GraphicOverlay graphicOverlay) 函数。此后,我们调用decodeBufferedImage(bitmap, “” )来识别QR码。
@Overridepublic void processBitmap(Bitmap bitmap, final GraphicOverlay graphicOverlay) { try { long frameStartMs = SystemClock.elapsedRealtime(); TextResult[] results = barcodeScanner.decodeBufferedImage(bitmap, ""); long frameEndMs = SystemClock.elapsedRealtime(); if (results != null) { for (int i = 0; i < results.length; ++i) { TextResult barcode = results[i]; graphicOverlay.add(new DynamsoftBarcodeGraphic(graphicOverlay, barcode)); } graphicOverlay.add( new InferenceInfoGraphic( graphicOverlay, frameEndMs - frameStartMs, frameEndMs - frameStartMs, null)); } } catch (IOException e) { e.printStackTrace(); } catch (BarcodeReaderException e) { e.printStackTrace(); }}
对比测试
图片我们使用同样的6张,来判断不同密度中两个SDK的效率。ML Kit的条形码扫描API对这些图像是无能为力的。ML Kit比Dynamsoft Barcode Reader花了更多的时间,却什么也没识别出来。相比之下,Dynamsoft Barcode Reader能够识别所有高密度的QR码图像。
本次实验使用到的源码
https://github.com/yushulx/android-dense-qr-code-detection
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!