Barcode Height and Width settings

Hi,

I am trying to create Barcode with specific height and width in java. I am using the following code.

BarCodeBuilder b = new BarCodeBuilder();
// Set up symboogy
b.setSymbology(Symbology.CODE128);
// Set up code text
b.setCodeText(“Subhakar098”);

b.setAutoSize(false);
b.setGraphicsUnit(GraphicsUnit.PIXEL);
b.setHeight(32.0f);
b.setWidth(124.0f);
b.setCodeTextVisible(false);

But the image is not showing properly in the frontend.

Please advise about any specific changes that are required for JAVA programming.

Thanks in advance.

Hi Subhakar,

Thanks for considering Aspose.

Please add the following 2 lines for setting xDimension and yDimension in your code:

b.setXDimension(1f);
b.setYDimension(1f);

I tried to test it in Java, the image is generated according to the specified pixels, but it is actually cut to match the width and height settings. And therefore, its not saved properly.

Using the codetext mentioned in your code, if we set the width to 170 pixels and set x and y dimensions to 1, its saved correctly. However, I will discuss this with the developers further to find a solution and will get back to you.

Hi,

Thank you for your reply.

But based on what I understand from the documentation, YDimension is for 2D Barcodes. For 1D barcodes, I am trying to set only BarHeight and XDimension.

I am able to generate the image but the image height and width are not getting adjusted properly.

Can you provide a sample of values that can be set to Height and Width and result image size in Pixels?

Hi,

Thanks for considering Aspose.

We do not have samples of height and width values for a given barcode symbology and codetext. In the above example, the width was 170 and height was 32, in which the barcode could fit properly in the image. But, in this example, the code text was "Subhakar098". If you increase the characters in the codetext, the width will also increase to adjust more characters. There is no formula for calculating the required width and height.

xDimension has effect on 1D barcodes as well. If you dont set its value to 1, the resulting image will be much bigger in size.

But, you can use the following code snippet, to see the resulting width and height of the barcode image. Just give different values for the codetext and the program will just output the width/height of the resulting image.

BarCodeBuilder b = new BarCodeBuilder();
// Set up symboogy
b.setSymbology(Symbology.CODE128);
// Set up code text
b.setCodeText("Subhakar098");

b.setAutoSize(true);
b.setGraphicsUnit(GraphicsUnit.PIXEL);
b.setXDimension(1f);
b.setCodeTextVisible(false);
b.save("c:\\temp.jpg");

BarCodeReader reader = new BarCodeReader(Toolkit.getDefaultToolkit().getImage("c:\\temp.jpg"), BarCodeReadType.Code128);
if (reader.read() == true)
{
System.err.println("BarCode found: codetext: " + reader.getCodeText());
}
else
{
System.err.println("BarCode not found");
}
Image img = Toolkit.getDefaultToolkit().getImage("c:\\temp.jpg");
System.out.println("Height: " + img.getHeight(null) + "\nWidth: " + img.getWidth(null));
reader.close();