Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce border width on QR Codes generated by ZXing?

Tags:

zxing

I'm using com.google.zxing.qrcode.QRCodeWriter to encode data and com.google.zxing.client.j2se.MatrixToImageWriter to generate the QR Code image. On a 400x400 image, there is about a 52 pixel wide border around the code. I'd like this border to be narrower, maybe 15 pixels, but I don't see anything in the API for doing that. Am I missing something in the documenation? Or would I need to process the image myself?

For reference, here is an example 400x400 QR Code produced with the ZXing library:

An example QR Code

like image 924
James Sumners Avatar asked Apr 13 '12 14:04

James Sumners


People also ask

How do I resize my QR code?

To reduce the size of your QR code, you can do three things. Lower the character count, lower the error correction, and remove a central graphic if it exists. Large QR codes can slow down scanning. Decreasing QR codes keeps them true to their name.

Can you scale down a QR code?

You can resize as much as you want. The information is encoded in the pattern of the data, not in the size of the dots themselves. As long as a scanner can resolve properly between light/dark, the QR code should be readable at any size.

How do I resize a QR code without losing quality?

Press the Ctrl+R hotkey to open the Resize/Resample window. Enter your desired dimensions in pixels or percentages to resize the QR code without losing quality.

Can you put a border around a QR code?

You can add borders around a QR code, specifying the border size, color, and type. This can be done through styles or by using local formatting.


1 Answers

The QR spec requires a four module quiet zone and that's what zxing creates. (See QUIET_ZONE_SIZE in QRCodeWriter.renderResult.)

More recent versions of ZXing allow you to set the size of the quiet zone (basically the intrinsic padding of the QR code) by supplying an int value with the EncodeHintType.MARGIN key. Simply include it in the hints Map you supply to the Writer's encode(...) method, e.g.:

Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */ 

If you change this, you risk lowering the decode success rate.

like image 105
smparkes Avatar answered Sep 28 '22 18:09

smparkes