I am trying to generate a QR Code and set it to an ImageView out of a PGP Public Key in my Android Application with Java. The libary I use is XZing and I am on Android Studio. This worked well before with smaller Strings, however when I try to do it with something as big as a PGP Public Key I get this error:
com.google.zxing.WriterException: Data too big
So my data is too big. Is there any possibility to create a QR Code with a String this big in XZing?
I have tried it with two different methods but both lead to the same exception.
Method one:
private void generateQR(String value) {
qrImage.setVisibility(View.VISIBLE);
QRGEncoder qrgEncoder = new QRGEncoder(value, null, QRGContents.Type.TEXT, 1000);
try {
// Getting QR-Code as Bitmap
Bitmap bm = qrgEncoder.encodeAsBitmap();
qrImage.setImageBitmap(bm);
// Setting Bitmap to ImageView
} catch (WriterException e) {
Log.v(TAG, e.toString());
}
}
Method two:
private Bitmap textToImage(String text, int width, int height) throws WriterException, NullPointerException {
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.DATA_MATRIX.QR_CODE,
width, height, null);
} catch (IllegalArgumentException Illegalargumentexception) {
return null;
}
int bitMatrixWidth = bitMatrix.getWidth();
int bitMatrixHeight = bitMatrix.getHeight();
int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
int colorWhite = 0xFFFFFFFF;
int colorBlack = 0xFF000000;
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? colorBlack : colorWhite;
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, width, 0, 0, bitMatrixWidth, bitMatrixHeight);
return bitmap;
}
QR Codes have a max size.
I changed the error correction level to L
and this then can fit in a longer URL.
This may work for you:
new MultiFormatWriter().encode(qrCodeText, BarcodeFormat.QR_CODE, width, height, mapWith(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L));
User contributions licensed under CC BY-SA 3.0