Customize Barcode Size

Customize Barcode Size

Barcode size is determined by the encoded data, X-dimension, vertical dimensions, padding, captions, and optional fixed image bounds.

Set X-dimension and bar height

BarcodeGenerator generator = new BarcodeGenerator(
        EncodeTypes.CODE_128,
        "SIZE-1D"
);

generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE);
generator.getParameters().getBarcode().getXDimension().setPixels(3.0f);
generator.getParameters().getBarcode().getBarHeight().setPixels(100);

X-dimension is the width of the smallest module or narrow bar. See the X-dimension overview.

Use auto-size modes

AutoSizeMode.NONE preserves explicitly configured dimensions. AutoSizeMode.NEAREST calculates a suitable barcode size for the image box. AutoSizeMode.INTERPOLATION scales the generated image to the requested bounds and may introduce resampling.

generator.getParameters().setAutoSizeMode(AutoSizeMode.NEAREST);
generator.getParameters().getImageWidth().setPixels(240);
generator.getParameters().getImageHeight().setPixels(240);

When NEAREST is enabled, explicit X-dimension and bar-height values are not the controlling dimensions.

Symbology-specific dimensions

Some barcode types expose additional size parameters:

  • AustralianPostShortBarHeight controls short bars in Australian Post symbols;
  • ITF-14 bearer-bar thickness and border type control the surrounding frame;
  • QR version determines the module matrix size;
  • Data Matrix version selects a fixed matrix configuration;
  • PDF417 columns, rows, and aspect ratio influence the rectangular layout.

See the QR Code standard overview, Data Matrix standard overview, and PDF417 standard overview for structural details.

Always verify the final dimensions against the applicable barcode specification, printer process, label material, and scanner environment.

Complete example

View CustomizingSizeExample.java

set