Skip to content

Instantly share code, notes, and snippets.

@harshildarji
Last active December 19, 2018 14:50
Show Gist options
  • Save harshildarji/a757ffb84b8523a6f5586d2cbeed5c9e to your computer and use it in GitHub Desktop.
Save harshildarji/a757ffb84b8523a6f5586d2cbeed5c9e to your computer and use it in GitHub Desktop.
Generating QR code in Android.
...
String QRcode = "...";
new generateQrcode(qrcodeImageview).execute(QRcode);
...
private class generateQrcode extends AsyncTask<String, Void, Bitmap> {
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment