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

2020-03-09  本文已影响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;
}
上一篇下一篇

猜你喜欢

热点阅读