接上篇文章Android扫码优化(一)-openCV 官方Demo配置,只是简单讲解了在Android Studio怎么配置openCV sdk Demo。对于上文提到的扫码不好扫的特殊场景,该怎么优化呢?
相机优化
二维码解码的数据是通过硬件Camera获取到的,相机的优化策略非常重要。
相机一般优化设置:
- 选择最佳预览尺寸/图片尺寸
- 设置适合的相机放大倍数
- 调整聚焦时间
- 设置自动对焦区域
- 调整合理扫描区域
但相机的硬件属性咱们改变不了,比如低端机器的Camera性能不行,按照以上策略再怎么优化也不行。设备性能好,聚焦快,扫码速度就快。相机做了一般处理,解码优化就很关键了。
解码优化
ZXing解码
- 将Camera预览的byte数据转换成YUV数据
- 将转换后的YUV数据转成BinaryBitmap
- 通过预先设置好的解码器对BinaryBitmap进行解码
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width; // Here we are swapping, that's the difference to #11
width = height;
height = tmp;
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
} finally {
multiFormatReader.reset();
}
ZBar解码
相对来说简单,直接将Camera预览数据转换成定义好的Image对象,通过预先设置的ImageScanner对Image进行解码。
Image image = new Image(width, height, "Y800");
image.setData(data);
int result = scanner.scanImage(image);
if (result != 0) {
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
return sym.getData();
}
}
针对以上的ZXing和ZBar解码常见的优化如下:
-
减少解码方式
将multiFormatReader中不需要的解码器直接剔除
-
解码算法优化
ZBar比ZXing快,Zbar直接通过JNI调取C代码解码
-
减少解码数据
解码预览数据 不选择整个屏幕而是取扫码框区域
解码库Zxing和Zbar结合
OpenCV
-
通过openCV将预览框的PlanarYUVLuminanceSource转换成RGB截取
void getCropRect(unsigned char *nv21, int width, int height, unsigned char *dest, int rectLeft, int rectTop, int rectWidth, int rectHeight) { Mat imgMat(height * 3 / 2, width, CV_8UC1, nv21); Mat rgbMat(height, width, CV_8UC3); Mat resultMat(rectHeight * 3 / 2, rectWidth, CV_8UC1, dest); cvtColor(imgMat, rgbMat, CV_YUV2RGB_NV21); Rect cropRect(rectLeft, rectTop, rectWidth, rectHeight); cvtColor(rgbMat(cropRect), resultMat, CV_RGB2YUV_I420); }
-
通过openCV对预览数据进行降噪
cvtColor(imgMat, innerMat, CV_YUV2GRAY_NV21); medianBlur(innerMat, innerMat, 7);
-
缩小二值化阀值计算范围
灰色二维码之所以难以识别是因为背景的色调过暗,拉低了二值化阈值的计算,导致整个二维码的难以识别。通过将二值化阈值的计算区域缩小到预览框的2/5,然后再通过计算的阈值对整个预览区域的数据进行二值化。
int rectWidth = width / 5; int rectHeight = height / 5; int centerX = width / 2, centerY = height / 2; Rect cropLTRect(centerX - rectWidth, centerY - rectHeight, rectWidth, rectHeight); Rect cropRTRect(centerX, centerY - rectHeight, rectWidth, rectHeight); Rect cropLBRect(centerX - rectWidth, centerY, rectWidth, rectHeight); Rect cropRBRect(centerX, centerY, rectWidth, rectHeight); double threshLT = threshold(innerMat(cropLTRect), binMat(cropLTRect), 0, 255, CV_THRESH_OTSU); double threshRT = threshold(innerMat(cropRTRect), binMat(cropRTRect), 0, 255, CV_THRESH_OTSU); double threshLB = threshold(innerMat(cropLBRect), binMat(cropLBRect), 0, 255, CV_THRESH_OTSU); double threshRB = threshold(innerMat(cropRBRect), binMat(cropRBRect), 0, 255, CV_THRESH_OTSU);
-
分块对预览区进行二值化
分块进行计算阈值,是考虑到不同区域的亮度是不同的,如果整体进行计算的话,会丢失部分有效信息。而之所以选取靠近中心的小部分进行阈值化计算,是因为用户行为通常会将二维码对准预览框的中心,因此中心部分,包含有效亮度信息更为精确,减少背景亮度对整体二值化的影响。
Rect LTRect(0, 0, centerX, centerY); Rect RTRect(centerX - 1, 0, centerX, centerY); Rect LBRect(0, centerY - 1, centerX, centerY); Rect RBRect(centerX - 1, centerY - 1, centerX, centerY); threshold(innerMat(LTRect), innerMat(LTRect), threshLT, 255, CV_THRESH_BINARY); threshold(innerMat(RTRect), innerMat(RTRect), threshRT, 255, CV_THRESH_BINARY); threshold(innerMat(LBRect), innerMat(LBRect), threshLB, 255, CV_THRESH_BINARY); threshold(innerMat(RBRect), innerMat(RBRect), threshRB, 255, CV_THRESH_BINARY);
最终就形成了openCV+Zxing+ZBar的解码策略:
// 1.使用Zbar
Result rawResult = zbarDecode(processSrc,processWidth,processWidth);
//2.使用ZXing
if (rawResult == null) {
rawResult = zxingDecode(processSrc,processWidth,processHeight);
}
if (rawResult == null) {
// OpenCV预处理
byte[] processData = new byte[processWidth * processHeight * 3 / 2];
ImagePreProcess.preProcess(processSrc, processWidth, processHeight, processData);
//3. opencv+Zbar
rawResult = zbarDecode(processSrc,processWidth,processHeight);
//4. opencv+Zxing
if (rawResult == null) {
rawResult = zxingDecode(processSrc,processWidth,processHeight);
}
}
总结
经过上述opencv预处理尝试之后,大大提升了特殊情景的二维码识别。
GitHub地址:https://github.com/gongchaobin/ScanMaster
参考文章
Android二维码扫描优化:http://blog.jostey.com/2018/04/27/Android二维码扫描优化/
二维码扫码优化-字节跳动: https://zhuanlan.zhihu.com/p/44845942