Android Using ZXing Generate QR Code

后端 未结 4 1141
你的背包
你的背包 2020-12-29 21:06

I was having some problem when trying to generate QR code in Android Programming. Here is the Tutorial I followed. When my generate button on click, I am calling this method

4条回答
  •  情书的邮戳
    2020-12-29 21:58

    Following code worked for me:

    ...
    String QRcode = "...";
    new generateQrcode(qrcodeImageview).execute(QRcode);
    ...
    private class generateQrcode extends AsyncTask {
            public final static int WIDTH = 400;
            ImageView bmImage;
    
            public generateQrcode(ImageView bmImage) {
                this.bmImage = bmImage;
            }
    
            protected Bitmap doInBackground(String... urls) {
                String Value = urls[0];
                com.google.zxing.Writer writer = new QRCodeWriter();
                Bitmap bitmap = null;
                BitMatrix bitMatrix = null;
                try {
                    bitMatrix = writer.encode(Value, com.google.zxing.BarcodeFormat.QR_CODE, WIDTH, WIDTH,
                            ImmutableMap.of(EncodeHintType.MARGIN, 1));
                    bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
                    for (int i = 0; i < 400; i++) {
                        for (int j = 0; j < 400; j++) {
                            bitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK
                                    : Color.WHITE);
                        }
                    }
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                return bitmap;
            }
    
            protected void onPostExecute(Bitmap result) {
                bmImage.setImageBitmap(result);
            }
        }
    

提交回复
热议问题