I'm using in my app ZXing library for generating QR code. I want to generated QR code that should fits to the width of the screen (maybe some small padding).
If I set width of the screen as width size of QR code I get smaller QR code. Look at the screenshot (it's 320x240 resolution). I want QR code to fit the black area. Why is QR code in red so small?
How do I stretch it to the black area?
My code:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Bitmap bm = encodeAsBitmap(mGeneratedURL, BarcodeFormat.QR_CODE, width, width);
qrcodeImage.setImageBitmap(bm);
Generating QR code:
private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
//hints.put(EncodeHintType.CHARACTER_SET, encoding);
hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? RED : Color.BLACK;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
On click of button_scan_qr_code , CaptureActivity will start scanning using default camera. Once it scans any QR code, it sends back the result to onActivityResult the MainActivity . ZXing also provides online QR Code Generator. Enter the required fields, generate and scan it to get the results.
In order to use the Zxing library in our application we need to add it's dependency in our application's gradle file. For adding the dependency Go to Gradle Scripts > build. gradle(Module: app) and add the following dependencies. After adding the dependency you need to click on Sync Now.
I find a problem!
This line:
String encoding = guessAppropriateEncoding(contentsToEncode);
returns null
So It doesn´t set
EncodeHintType.MARGIN.
Remove the codition and it should be ok.
//if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
//hints.put(EncodeHintType.CHARACTER_SET, encoding);
hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */
//}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With