New to Zxing... Generated QR codes do not respect size parameters?

1,753 views
Skip to first unread message

Dana Duerr

unread,
Jul 9, 2014, 5:45:16 PM7/9/14
to zx...@googlegroups.com
Hello...

I'm writing java code that uses Zxing to create a QR code on printed conference badges that contain attendee information to be used for lead retrieval.

I've been able to create a QR code and have been able to scan it successfully on most phones. A few phones were not able to scan the QR code so we wanted to make it a bit larger but we still need it to fit on the badge without conflicting with other elements.

When generating the QR code Zxing does not seem to be respecting the size that I'm passing in. If I use sizes of 100-105 I get the same size QR code. If I use 106 the size of the finished QR code is doubled. For the life of me I can't figure out what I'm doing wrong.

Here is the code... Does anyone have any suggestions?

<SNIP>

String filePath = System.getProperty("catalina.base") +
System.getProperty("file.separator") +
"webapps" + System.getProperty("file.separator") +
"craps" + System.getProperty("file.separator") +
"tmp";
File qrDir = new File(filePath);
File qrFile = File.createTempFile("CrapsBC", ".png", qrDir);
OutputStream os = new FileOutputStream(qrFile);

Hashtable hintMap = new Hashtable();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

String content = "Some user information.";
String size = 106;
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hintMap);

MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
os.flush();
os.close();


// Add the QR code to the PDF
Image image = Image.getInstance(qrFile.getAbsolutePath());
template.addImage(image, size, 0, 0, size, 170, 60); // (image, image_width, 0, 0, image_height, x, y)

// Make sure to delete the QR code file.
qrFile.delete();
</SNIP>

Dana Duerr

unread,
Jul 9, 2014, 5:53:58 PM7/9/14
to zx...@googlegroups.com
Whoops... That size declaration is an int.

Lachezar Dobrev

unread,
Jul 10, 2014, 4:58:02 AM7/10/14
to Dana Duerr, zxing
The size of the bar-code is chosen so that the scaling coefficient
is an integer, not a fraction. The extra pixels are just made white.
This is done to avoid introducing inconsistencies between pixels
(modules) in the bar-code. That can be a problem since small
fractional scaling factors (between 1 and 2 for instance) will double
some rows/columns, while others will not be doubled. This will create
a bar-code with significant artefacts that will make it harder to
read.
What I would suggest is that you create the bar-code image with size
1x1 which will get you the smallest available image possible, and do
whatever scaling you want when printing the bar-code. This should work
very well since typically print media has significantly high
resolution and scaling would use a factor significantly larger than 1.

2014-07-10 0:53 GMT+03:00 Dana Duerr <dana....@gmail.com>:
> Whoops... That size declaration is an int.
>
> --
> You received this message because you are subscribed to the Google Groups "zxing" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to zxing+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dana Duerr

unread,
Jul 10, 2014, 9:58:35 AM7/10/14
to zx...@googlegroups.com
That worked perfectly.... Thank you! The new size codes scan on all our devices. Even the ones that did not work before.
Reply all
Reply to author
Forward
0 new messages