Skip to content

Instantly share code, notes, and snippets.

@adrianoluis
Created February 8, 2017 02:51
Show Gist options
  • Save adrianoluis/fa9374d7f2f8ca1115b00cc83cd7aacd to your computer and use it in GitHub Desktop.
Save adrianoluis/fa9374d7f2f8ca1115b00cc83cd7aacd to your computer and use it in GitHub Desktop.
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
Copy link

Excellent, Thanks a lot! 👍

@vijaypatidar
Copy link

Thank you!

@yihongyuelan
Copy link

Kotlin version

import android.graphics.Bitmap
import android.graphics.Color
import com.google.zxing.BarcodeFormat
import com.google.zxing.MultiFormatWriter
import com.google.zxing.common.BitMatrix

fun encodeAsBitmap(source: String, width: Int, height: Int):Bitmap? {

    val result: BitMatrix = try {
        MultiFormatWriter().encode(source, BarcodeFormat.QR_CODE, width, height, null)
    } catch (e: Exception) {
        return null
    }

    val w = result.width
    val h = result.height
    val pixels = IntArray(w * h)

    for (y in 0 until h) {
        val offset = y * w
        for (x in 0 until w) {
            pixels[offset + x] = if (result[x, y]) Color.BLACK else Color.WHITE
        }
    }

    val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
    bitmap.setPixels(pixels, 0, width, 0, 0, w, h)

    return bitmap
}

@taimoorgit
Copy link

Do this to minimize the margins:

// Use a hintmap to minimize margins
HashMap hintMap = new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
hintMap.put(EncodeHintType.MARGIN, 0);

result = new MultiFormatWriter().encode(source, BarcodeFormat.QR_CODE, width, height, hintMap);

Note that there will still be a white margin, since if you have a black background the QR code would be unreadable.

@Mubeyd
Copy link

Mubeyd commented Apr 22, 2022

It was very useful, Thanks. 👍

@PistonDragon
Copy link

Great!
HelpFULL!
I tried to get image from text, but the most of codes gave error.
Only your code SUCCESS.
thanks

@dorianpavetic
Copy link

@taimoorgit Thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment