Skip to content

Commit 4aa8ff2

Browse files
author
Aleksander Grinin
committed
Add articles to /java/developer-guide/barcode-generation section
1 parent d945a02 commit 4aa8ff2

1 file changed

Lines changed: 33 additions & 62 deletions

File tree

  • java/developer-guide/barcode-generation/visual-parameters/backgrounds
Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
---
22
title: "Customize Barcode Backgrounds"
3-
description: "Learn how to configure barcode background colors, transparency, padding, and contrast in Aspose.BarCode for Java."
3+
description: "Learn how to configure barcode background colors, padding, and foreground-to-background contrast in Aspose.BarCode for Java."
44
type: docs
55
weight: 10
66
url: /java/developer-guide/barcode-generation/visual-parameters/backgrounds/
77
---
88

99
# Customize Barcode Backgrounds
1010

11-
Aspose.BarCode for Java allows you to configure the background of a generated barcode image, including solid colors, transparent backgrounds, and semi-transparent backgrounds.
11+
Aspose.BarCode for Java allows you to configure the background color, barcode color, and padding of a generated barcode image.
1212

13-
Background settings do not change the encoded data, but they can affect recognition reliability.
13+
Background and foreground settings do not change the encoded data, but they can affect recognition reliability.
1414

1515
The complete source code for the examples in this article is available on GitHub:
1616

1717
<a href="https://github.com/aspose-barcode/Aspose.BarCode-for-Java/blob/master/src/test/java/com/aspose/barcode/guide/generation/visual_parameters/BackgroundsExample.java" target="_blank">View BackgroundsExample.java</a>
1818

19-
set
20-
2119
## Set a solid background color
2220

23-
Use `setBackColor` to configure the image background.
21+
Use `setBackColor` to configure the image background and `setBarColor` to configure the bars or modules.
2422

2523
```java
2624
BarcodeGenerator generator = new BarcodeGenerator(
@@ -46,13 +44,17 @@ A light background with dark modules usually provides reliable recognition.
4644

4745
## Use a dark background
4846

47+
You can also generate light modules on a dark background.
48+
4949
```java
5050
BarcodeGenerator generator = new BarcodeGenerator(
5151
EncodeTypes.QR,
5252
"DARK-BACKGROUND"
5353
);
5454

55-
generator.getParameters().setBackColor(Color.BLACK);
55+
generator.getParameters().setBackColor(
56+
Color.BLACK
57+
);
5658

5759
generator.getParameters()
5860
.getBarcode()
@@ -66,58 +68,9 @@ generator.save(
6668

6769
Inverted barcodes can be less compatible with some readers. Use dark bars or modules on a light background when maximum interoperability is required.
6870

69-
## Create a transparent background
70-
71-
PNG supports an alpha channel. A fully transparent background can be created with an alpha value of `0`.
72-
73-
```java
74-
BarcodeGenerator generator = new BarcodeGenerator(
75-
EncodeTypes.QR,
76-
"TRANSPARENT-QR"
77-
);
78-
79-
generator.getParameters().setBackColor(
80-
new Color(255, 255, 255, 0)
81-
);
82-
83-
generator.save(
84-
"qr_transparent.png",
85-
BarCodeImageFormat.PNG
86-
);
87-
```
88-
89-
The barcode modules remain visible, while the configured background area is transparent.
90-
91-
## Create a semi-transparent background
92-
93-
Use an alpha value between `0` and `255` to create a semi-transparent background.
94-
95-
```java
96-
BarcodeGenerator generator = new BarcodeGenerator(
97-
EncodeTypes.QR,
98-
"SEMI-TRANSPARENT-QR"
99-
);
100-
101-
generator.getParameters().setBackColor(
102-
new Color(255, 255, 255, 128)
103-
);
104-
105-
generator.save(
106-
"qr_semi_transparent.png",
107-
BarCodeImageFormat.PNG
108-
);
109-
```
110-
111-
The alpha value ranges from:
112-
113-
- `0` — fully transparent;
114-
- `255` — fully opaque.
115-
116-
Not all image formats preserve transparency. JPEG does not support an alpha channel.
117-
11871
## Configure padding around the barcode
11972

120-
Padding adds blank space around the generated barcode image.
73+
Padding adds blank image space around the generated barcode.
12174

12275
```java
12376
BarcodeGenerator generator = new BarcodeGenerator(
@@ -157,17 +110,35 @@ generator.save(
157110

158111
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.
159112

160-
## Validate transparency in tests
113+
## Verify the generated background color
114+
115+
The related Java example reads the generated image with `ImageIO` and checks the RGB values of a background pixel.
161116

162-
The related Java example checks generated PNG images with `ImageIO` and confirms that transparent or semi-transparent alpha values are actually present in the output.
117+
```java
118+
BufferedImage image = ImageIO.read(
119+
new File("qr_custom_background.png")
120+
);
121+
122+
Color actualColor = new Color(
123+
image.getRGB(0, 0),
124+
true
125+
);
126+
127+
if (actualColor.getRed() != 245
128+
|| actualColor.getGreen() != 248
129+
|| actualColor.getBlue() != 252) {
130+
throw new IllegalStateException(
131+
"Unexpected background color."
132+
);
133+
}
134+
```
163135

164-
This verifies that the selected image format preserved the requested transparency instead of checking only that a file was created.
136+
This confirms that the expected solid background color was written to the image.
165137

166138
## Recommendations
167139

168140
- Maintain strong contrast between the barcode and its final background.
169-
- Test transparent images after placing them on the actual destination background.
170-
- Use an output format that preserves the required alpha channel.
141+
- Prefer dark bars or modules on a light background for maximum interoperability.
171142
- Keep enough blank space around the symbol.
172143
- Do not treat arbitrary padding values as universal quiet-zone dimensions.
173144
- Validate the final image with the intended scanner and rendering pipeline.

0 commit comments

Comments
 (0)