IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Zxing识别图片二维码识别率低 -> 正文阅读

[移动开发]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,同时调用容易直接闪退,暂时放弃。

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-09-30 01:04:20  更:2022-09-30 01:06:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 23:29:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码