Customize Barcode Backgrounds
Customize Barcode Backgrounds
Aspose.BarCode for Java allows you to configure the background color, barcode color, and padding of a generated barcode image.
Background and foreground settings do not change the encoded data, but they can affect recognition reliability.
The complete source code for the examples in this article is available on GitHub:
Set a solid background color
Use setBackColor to configure the image background and setBarColor to configure the bars or modules.
BarcodeGenerator generator = new BarcodeGenerator(
EncodeTypes.QR,
"BACKGROUND-EXAMPLE"
);
generator.getParameters().setBackColor(
new Color(245, 248, 252)
);
generator.getParameters()
.getBarcode()
.setBarColor(new Color(20, 45, 90));
generator.save(
"qr_custom_background.png",
BarCodeImageFormat.PNG
);
A light background with dark modules usually provides reliable recognition.
Use a dark background
You can also generate light modules on a dark background.
BarcodeGenerator generator = new BarcodeGenerator(
EncodeTypes.QR,
"DARK-BACKGROUND"
);
generator.getParameters().setBackColor(
Color.BLACK
);
generator.getParameters()
.getBarcode()
.setBarColor(Color.WHITE);
generator.save(
"qr_dark_background.png",
BarCodeImageFormat.PNG
);
Inverted barcodes can be less compatible with some readers. Use dark bars or modules on a light background when maximum interoperability is required.
Configure padding around the barcode
Padding adds blank image space around the generated barcode.
BarcodeGenerator generator = new BarcodeGenerator(
EncodeTypes.CODE_128,
"PADDING-EXAMPLE"
);
generator.getParameters()
.getBarcode()
.getPadding()
.getLeft()
.setPixels(20);
generator.getParameters()
.getBarcode()
.getPadding()
.getRight()
.setPixels(20);
generator.getParameters()
.getBarcode()
.getPadding()
.getTop()
.setPixels(10);
generator.getParameters()
.getBarcode()
.getPadding()
.getBottom()
.setPixels(10);
generator.save(
"code128_with_padding.png",
BarCodeImageFormat.PNG
);
Padding is an image-layout setting. Left and right padding can reserve space for required quiet zones, but the required quiet-zone width depends on the symbology, X-dimension, and applicable barcode specification.
Verify the generated background color
The related Java example reads the generated image with ImageIO and checks the RGB values of a background pixel.
BufferedImage image = ImageIO.read(
new File("qr_custom_background.png")
);
Color actualColor = new Color(
image.getRGB(0, 0),
true
);
if (actualColor.getRed() != 245
|| actualColor.getGreen() != 248
|| actualColor.getBlue() != 252) {
throw new IllegalStateException(
"Unexpected background color."
);
}
This confirms that the expected solid background color was written to the image.
Recommendations
- Maintain strong contrast between the barcode and its final background.
- Prefer dark bars or modules on a light background for maximum interoperability.
- Keep enough blank space around the symbol.
- Do not treat arbitrary padding values as universal quiet-zone dimensions.
- Validate the final image with the intended scanner and rendering pipeline.