直接在ImageView中生成QR码?

时间:2013-10-12 18:01:13

标签: java android imageview zxing

Stack Overflow的好人,

在我的代码中,我在Android应用中使用ZXing库生成QR码。我不想直接将其保存到文件中,而是希望它进入ImageView。

这是我的代码:

 String WIFIQRCODE = "";
                    String SSID = etSSID.getText().toString();
                    String PASS = etPASS.getText().toString();
                    String PASSTYPE = sTYPE.getSelectedItem().toString();
                    WIFIQRCODE =   "WIFI:T:"+PASSTYPE+";S:"+SSID+";P:"+PASS;
                    //Inform the user the button1 has been clicked
                    QRCodeWriter writer = new QRCodeWriter();
                    BitMatrix bitMatrix = null;
                    try {
                        bitMatrix = writer.encode(WIFIQRCODE, BarcodeFormat.QR_CODE, 300, 300);
                        File file = new File(context.getFilesDir(), "/sdcard/Images/"+SSID+".png");
                        MatrixToImageWriter.writeToFile(bitMatrix, "png", file);

                    } catch (WriterException e){
                        e.printStackTrace();
                    } catch (IOException e){
                        e.printStackTrace();
                    }

如何修改它以便将其放入ImageView而不是文件中?谢谢!

3 个答案:

答案 0 :(得分:12)

您需要从Bitmap获取BitMatrix才能在ImageView中直接设置图片:

    int height = bitMatrix.getHeight();
    int width = bitMatrix.getWidth();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++){
        for (int y = 0; y < height; y++){
            bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
        }
    }
   ImageView qr_image = (ImageView) findViewById(R.id.qrimage);
    qr_image.setImageBitmap(bmp);

有关详细信息,您可以看到生成QR Codes with ZXing以从bitMatrix获取Bitmap

答案 1 :(得分:0)

简单方法

public static Bitmap createQRImage(final String url, final int width, final int height) throws WriterException {
    final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
    return Bitmap.createBitmap(IntStream.range(0, height).flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE)).toArray(),
            width, height, Bitmap.Config.ARGB_8888);
}

public static Bitmap createQRImage2(final String url, final int width, final int height) throws WriterException {
    final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
    return Bitmap.createBitmap(IntStream.range(0, height * width).map(s -> bitMatrix.get(s % width, s / width) ? Color.BLACK : Color.WHITE).toArray(),
            width, height, Bitmap.Config.ARGB_8888);
}

或者,因为如果大小未知,Stream.toArray()会动态分配输出数组,如果数组大小太大,可能会导致频繁分配内存

public static Bitmap createQRImage3(final String url, final int width, final int height) throws WriterException {
    final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
    return Bitmap.createBitmap(IntStream.range(0, height)
                    .flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE))
                    .collect(() -> IntBuffer.allocate(width * height), IntBuffer::put, IntBuffer::put)
                    .array(),
            width, height, Bitmap.Config.ARGB_8888);
}

答案 2 :(得分:-2)

与此相反,

获取图像文件的目录,然后将其输入:

Bitmap imageBitmap = BitmapFactory.decodeFile(path);

然后简单为

myImageView.setImageBitmap(imageBitmap);