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