Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple utility class to create QR Code as Bitmap on Android using ZXing library
import android.graphics.Bitmap;
import android.graphics.Color;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class QRCodeUtil {
private QRCodeUtil() {}
public static Bitmap encodeAsBitmap(String source, int width, int height) {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(source, BarcodeFormat.QR_CODE, width, height, null);
} catch (IllegalArgumentException | WriterException e) {
// Unsupported format
return null;
}
final int w = result.getWidth();
final int h = result.getHeight();
final int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
final int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, w, h);
return bitmap;
}
}
@pablo384

This comment has been minimized.

Copy link

pablo384 commented Jul 10, 2019

Excellent, Thanks a lot! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.