在Android上扫描并重新创建QR码

时间:2018-03-11 16:21:01

标签: android qr-code

有没有人知道可以在Android上扫描并重新创建QR码的方式(最好是单个应用)?

EG。我将手机指向代码,然后我可以在屏幕上显示该代码,因此可以通过其他设备再次扫描。这与简单地拍摄QR码的图片不同,因为我需要更长的时间来获得代码的漂亮照片,即使我这样做,质量仍然非常糟糕,使用其他设备扫描代码需要很长时间

1 个答案:

答案 0 :(得分:1)

尝试zxing library,https://github.com/zxing/zxing

<强> 1。扫描后将字符串数据存储为位图:

public static Bitmap encodeAsBitmap(final String contentAfterScan, final BarcodeFormat format,
                                        final int width, final int height) throws WriterException {
        MultiFormatWriter writer = new MultiFormatWriter();
        EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = writer.encode(contentAfterScan, format, width, height, hint);
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000/*BLACK*/ : 0xFFFFFFFF/*WHITE*/;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

<强> 2。将图片存储到您的文件夹

private void storeImageFromBitmap(Bitmap bitmap, String yourFolder, String imageName) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        File f = new File(yourFolder + File.separator + imageName);
        FileOutputStream fo;
        try {
            f.createNewFile();
            fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}