2

你好 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 而不是文件中?谢谢!

4

3 回答 3

18

you will need to get Bitmap from BitMatrix to set directly image in ImageView do it as:

    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);

for more detail you can see Generating QR Codes with ZXing for getting Bitmap from bitMatrix

于 2013-10-12T18:09:05.677 回答
1

简单的方法

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() 会在 size 未知的情况下动态分配输出数组,如果数组太大,可能会导致频繁分配内存

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);
}
于 2020-09-30T09:06:25.290 回答
-2

As appose to that,

get the directory to the image file then feed it in to:

Bitmap imageBitmap = BitmapFactory.decodeFile(path);

then its simple as

myImageView.setImageBitmap(imageBitmap);
于 2013-10-12T18:07:55.567 回答