Set 2D Data Matrix barcode size in Java

Hello there,


Can you please guide me to set the 2D datamatrix size to 12x12 millimeter.i tried playing with the setsize() methods but did not get the desired size. I will appreciate your help.

Regards
Zulf
Hi Zulf,

Thank you for your inquiry.

Please use the following lines of code to set the barcode image size. For details please visit the links:



objBuilder.setAutoSize(false);
objBuilder.setImageHeight(12);
objBuilder.setImageWidth(12);

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

Hi Ikram,


I tried this but I get error. By the way this 12 is pixel or what unit. Can you please clarify

Exception in thread “main” class com.aspose.barcode.BarCodeException: Too small image’s size - width: 47.24409px, height: 47.24409px. The generated barcode don’t fit to image. Barcode size - width: 0p
x, height: 48px.
com.aspose.barcode.j.a(Unknown Source)
com.aspose.barcode.j.b(Unknown Source)
com.aspose.barcode.j.a(Unknown Source)
com.aspose.barcode.j.a(Unknown Source)
com.aspose.barcode.j.C(Unknown Source)
com.aspose.barcode.BarCodeBuilder.l(Unknown Source)
com.aspose.barcode.BarCodeBuilder.f(Unknown Source)
com.aspose.barcode.BarCodeBuilder.getBarCodeImage(Unknown Source)
com.aspose.barcode.BarCodeBuilder.save(Unknown Source)
com.aspose.barcode.BarCodeBuilder.save(Unknown Source)
com.demo.Demo1.main(Demo1.java:116)
at com.aspose.barcode.j.a(Unknown Source)
at com.aspose.barcode.j.b(Unknown Source)
at com.aspose.barcode.j.a(Unknown Source)
at com.aspose.barcode.j.a(Unknown Source)
at com.aspose.barcode.j.C(Unknown Source)
at com.aspose.barcode.BarCodeBuilder.l(Unknown Source)
at com.aspose.barcode.BarCodeBuilder.f(Unknown Source)
at com.aspose.barcode.BarCodeBuilder.getBarCodeImage(Unknown Source)
at com.aspose.barcode.BarCodeBuilder.save(Unknown Source)
at com.aspose.barcode.BarCodeBuilder.save(Unknown Source)
at com.demo.Demo1.main(Demo1.java:116)

regards
Zulf

Hi Zulf,

Thank you for writing us back.

Barcode image height and width can be measured in different units. The default size measurement unit for barcode images is millimeter. For more details on measurement units and how to set, please visit the link Set Size Unit for the Barcode Image.

Barcode size also depends upon the code text length. You will get runtime exception in case the code text do not fit in custom defined height and width. Please consider the following code snippets. Generated barcode images have also been attached for your reference.
**CODE-1** :

BarCodeBuilder objBuilder = new BarCodeBuilder();
objBuilder.setSymbologyType(com.aspose.barcode.Symbology.DataMatrix);
objBuilder.setCodeText("(01)09501101020917(17)190508(10)ABCD1234(410)9501101020917");
objBuilder.setAutoSize(true);
objBuilder.setImageHeight(12);
objBuilder.setImageWidth(12);
objBuilder.save("1.jpg");

**CODE-2:**

BarCodeBuilder objBuilder = new BarCodeBuilder();
objBuilder.setSymbologyType(com.aspose.barcode.Symbology.DataMatrix);
objBuilder.setCodeText("123456789");
objBuilder.setAutoSize(false);
objBuilder.setImageHeight(12);
objBuilder.setImageWidth(12);
objBuilder.save("2.jpg");