美文网首页
Android 相册选择二维码图片识别关键代码

Android 相册选择二维码图片识别关键代码

作者: 晒太阳的蜗牛 | 来源:发表于2020-03-09 11:48 被阅读0次

    1获取图片地址路径


    Screen Shot 2020-03-09 at 11.46.36.png

    2识别
    public Result scanningImage(Uri uri) {
    if (uri == null) {
    return null;
    }

        Hashtable<DecodeHintType, String> hints = new Hashtable<>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码
    
        Bitmap bitmap = BitmapUtil.decodeUri(this, uri, 500, 500);
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
    
        int[] pix = new int[width * height];
        bitmap.getPixels(pix, 0, width, 0, 0, width, height);
    
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pix);
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        try {
            return reader.decode(bitmap1, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (FormatException e) {
            e.printStackTrace();
        }
        return null;
    }

    相关文章

      网友评论

          本文标题:Android 相册选择二维码图片识别关键代码

          本文链接:https://www.haomeiwen.com/subject/biiudhtx.html