How to use dpi as input to Writers/Encoders?

Skip to first unread message

Zorawar B.

Mar 9, 2017, 4:16:39 PM3/9/17
to zxing

It will be great if someone can clarify how to use dpi while encoding/generating barcodes. I am using the API first time, so greatly appreciate your answers.

I am trying to generate Data Matrix barcodes to be of size 0.24in @ 300dpi. That is 72px(=0.24*300) in width and height.
However, the generated barcode is not 0.24in but much larger.
I get the required size when the dimensions are 18px.

Am I missing something here?
Or, should I have to scale the generated barcode later on?
In general, I am interested in knowing how should I account for printing barcodes on printers with different resolutions(dpi)?

Please see the below sample code.
int dpi = 300;
int size = 0.24; //inches
int widthInPixels = size * dpi; // 72px
int heightInPixels = widthInPixels; //square barcode

// hints
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
hints.put(EncodeHintType.MIN_SIZE, new Dimension(widthInPixels, heightInPixels));

// write out
String contents = ... //valid content
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, BarcodeFormat.DATA_MATRIX, widthInPixels, heightInPixels, hints);
Path path = ... //some path
MatrixToImageWriter.writeToPath(result, "PNG", path);



Sean Owen

Mar 13, 2017, 4:28:28 PM3/13/17
to zxing
Nothing about the library has a notion of dots-per-inch. You would need to take the output, which seems to be the size you need in pixels, and construe it as the resolution you wish.
Reply all
Reply to author
0 new messages