Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate QR Code directly into ImageView?

I am generating a QR code with ZXing library in an Android app.

Instead of directly saving it into a file, I want it to go into an ImageView.

Here is my code:

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

How to modify it, so that it puts it into an ImageView instead of a file?

like image 405
Steve Smith Avatar asked Jul 29 '26 01:07

Steve Smith


2 Answers

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

like image 124
ρяσѕρєя K Avatar answered Jul 30 '26 15:07

ρяσѕρєя K


simple way

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

or

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

or, because Stream.toArray() allocate the output array dynamically if size unknown, which may cause memory allocation frequently if the array size too large

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);
}
like image 45
Yessy Avatar answered Jul 30 '26 15:07

Yessy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!