zxing解析带logo二维码会报com.google.zxing.NotFoundException

时间:2023-03-09 05:15:31
zxing解析带logo二维码会报com.google.zxing.NotFoundException

参考原文:https://blog.****.net/cyl1226541/article/details/70557232

      //复杂模式,开启PURE_BARCODE模式(☆☆☆)
HINTS.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
//优化精度
HINTS.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);     上述两句代码建议不加,因为加上以后,在保证只有二维码的情况下,才可以正常识别。
    
// 解码设置编码方式为:utf-8,
HINTS.put(DecodeHintType.CHARACTER_SET, "utf-8");

上述方法,尝试过后,不可行, 还有说法是,将彩色图片转换为黑白图片。

用这个博客提供的https://blog.****.net/android_machong/article/details/53169048图片转换为黑白图片后,还是不行

// 彩色图片转黑白
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组 bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j]; // 分离三原色
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF); // 转化成灰度像素
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
// 新建图片
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
// 设置图片数据
newBmp.setPixels(pixels, 0, width, 0, 0, width, height); Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
return resizeBmp;
}