直接在ImageView中生成QR码?

Ste*_*th 2 java android zxing imageview

你好,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();
                    }
Run Code Online (Sandbox Code Playgroud)

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

ρяσ*_*я K 14

您将需要Bitmap从BitMatrixImageView中直接设置图像,请执行以下操作:

    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);
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,您可以看到使用ZXing生成QR代码以从bitMatrix获取位图