Bar Code returns different widths from get methods

I am generating a small bar code and I noticed that the width changes depending on which getBarCode method I use.

getOnlyBarCodeImage returns a very nice bar code of the correct width.
getBarCodeImage (to keep the text) returns the same code, but the image is stretched a bit.

You can see the difference using the code below:
Shouldn’t both methods return the same bar code?

Also, is there any difference between getBarCodeImage() and generateBarCodeImage()?


BarCodeBuilder builder = new BarCodeBuilder();
builder.setSymbologyType( Symbology.Code128 );
builder.setCodeText( “abcd.1234567890” );
builder.setCodeLocation( CodeLocation.Below );
builder.setGraphicsUnit( GraphicsUnit.Millimeter );
builder.setBarHeight( 8 );
builder.getMargins().set(0);

ImageIO.write( builder.getBarCodeImage(), “PNG”, new File(“C:/Dev/BarCode.png”) );
ImageIO.write( builder.getOnlyBarCodeImage(), “PNG”, new File(“C:/Dev/OnlyBarCode.png”) );

Hi David,

Thank you for your inquiry.

There are differences between getOnlyBarCodeImage and getBarCodeImage method. The image size is different. You will get only the barcode image in case of getOnlyBarCodeImage method, no extra image boundaries and no barcode caption. getBarCodeImage method returns image with barcode caption and padding.

Furthermore getBarCodeImage() and generateBarCodeImage() methods produce the same results. In case you want to customized barcode image, please use the following lines of code.

java.awt.image.BufferedImage img2 = builder.getCustomSizeBarCodeImage(new java.awt.Dimension(width, height), false);
java.io.File outputfile = new java.io.File("barcode_image.png");
javax.imageio.ImageIO.write(img2, "png", outputfile);

Hope the above information helps. Feel free to reach us in case you have any query or comments.

The problem was that the barcode with a caption was twice as wide as the one without the caption. You can see the comparison in the original post. The second bar code is just too wide.

The image width of the bar code should be the same with or without a caption.
Adding the caption should not cause the bar code to be double wide.

Thanks again!

Hi David,

We are in communication with our product team to get more details on why the width is double and if it is feasible to get the same width from both methods. We will update you soon.

In the meantime, you can use the method shared by Ikram to get custom sized barcode images.

Best Regards,

Hi David,

When you set GraphicsUnit to millimeter or inch, you must set resolution in this case. I do not see setResolution used in your code. Please use pixel as graphics unit or set resolution (with height and width) if you want to use millimeter to get accurate results.

If you will set GraphicsUnit to pixel, you will get the same result from both methods. Please also keep the following calculations in mind when setting the size.

inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inches
1 centimeter = 10 millimeters therefore 1 millimeter = 0.0393700787 inches
pixel = cm 0.393700787 dpi
8 Millimeters = 8 * 0.0393700787 inches = 0.1181102361 inches

Best Regards,