Android Zxing识别图片二维码识别率低

1、使用Zxing对图片进行识别二维码

在gradle中引入识别库:

implementation 'com.google.zxing:core:3.4.1'

对Bitmap进行识别二维码:

        int[] pixels = new int[1920 * 1920];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        RGBLuminanceSource source = new RGBLuminanceSource(
                width, height, pixels);
        BinaryBitmap tempBitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new QRCodeReader();
        try {
            reader.reset();
            return reader.decode(tempBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

发现识别率有点低,特别是对二维码的大小不同的时候,有效率更低。经过网上查找,发现太大的分辨率不利于识别,于是尝试缩小分辨率。

2、尝试缩小分辨率

把Bitmap缩放到指定的分辨率,如160000.

public static Bitmap getSmallerBitmap(Bitmap bitmap, double tarSize) {
        double size = bitmap.getWidth() * bitmap.getHeight() / tarSize;  // 160000  // 2000000
        if (size <= 1) return bitmap; // 如果小于
        else {
            float scale = (float) (1.0 / Math.sqrt(size));
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            return resizeBitmap;
        }
    }

对于一种指定的缩放分辨率,有时可以识别,根据二维码的占比不同时效果差别太大。

3、不断尝试多种分辨率

不确定原图需要缩放到多少的分辨率才是最佳的,那就尝试多种不同的分辨率。

private val PX_SIZES: MutableList<Double> =
        mutableListOf(90000.0, 230000.0, 370000.0, 510000.0, 650000.0, 800000.0)

同时对多种分辨率进行尝试,如果该分辨率成功了,就把该分辨率提到前面,否则就把后面的分辨率换一下位置:

fun processBitmap(bitmap: Bitmap): DetectResult? {
        val start = System.currentTimeMillis()
        var result: DetectResult? = null
        for (ix in PX_SIZES.indices) {
            val res = processBitmap(qrContext1, bitmap, PX_SIZES[ix])
            if (res != null) {
                result = res
                afterDetectSucceed(PX_SIZES, ix)
                break
            }
            // 识别不到,但是时间达到了,不再尝试
            if (abs(System.currentTimeMillis() - start) > DETECT_TIME_THRESHOLD) {
                break
            }
        }

        if (result == null) {
            afterDetectFailed(PX_SIZES)
        }
        return result
    }

    private fun afterDetectSucceed(pxs: MutableList<Double>, detectIx: Int) {
        if (detectIx > 1) {
            // 在第3位后面,直接提到第2位
            Collections.swap(pxs, detectIx, 1)
        } else if (detectIx > 0) {
            // 在第2位,直接放到向前移1位
            Collections.swap(pxs, detectIx, detectIx - 1)
        }
    }

    private fun afterDetectFailed(pxs: MutableList<Double>) {
        // 第3位后面轮换一下
        val tmp = pxs.last()
        var ix = pxs.lastIndex
        while (ix > 2) {
            pxs[ix] = pxs[ix - 1]
            --ix
        }
        pxs[2] = tmp
    }

还是使用同样的识别方法,但是 QRCodeReader对象和pixel数组可以重复使用,避免重复创建和销毁的性能损耗,可以把该对象存起来重复使用。

class QrReaderContext {
        private final Reader reader = new QRCodeReader();
        // 缓存的buffer数组对象,复用提高性能. 现在不能低于1920*1088
        private final int[] bufferPixels = new int[1920 * 1920];
}

public static Result scan(QrReaderContext context, Bitmap bitmap) {
        // 获取bitmap的宽高,像素矩阵
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width * height > context.bufferPixels.length) {
            BLLog.w(TAG, "scan buffer is not enough");
            return null;
        }

        int[] pixels = context.bufferPixels;
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        RGBLuminanceSource source = new RGBLuminanceSource(
                width, height, pixels);
        BinaryBitmap tempBitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = context.reader;
        try {
            reader.reset();
            return reader.decode(tempBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

这样感觉有点麻烦,不知道还有什么更好的方法呢?

4、尝试用多线程同时计算提高识别率

发现Bitmap的函数对于同一个对象都不是线程安全的,比如 Bitmap.createBitmap, Bitmap.getPixels,同时调用容易直接闪退,暂时放弃。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值