You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
12
12
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.
14
14
15
15
The complete source code for the examples in this article is available on GitHub:
Inverted barcodes can be less compatible with some readers. Use dark bars or modules on a light background when maximum interoperability is required.
68
70
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 =newBarcodeGenerator(
75
-
EncodeTypes.QR,
76
-
"TRANSPARENT-QR"
77
-
);
78
-
79
-
generator.getParameters().setBackColor(
80
-
newColor(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 =newBarcodeGenerator(
97
-
EncodeTypes.QR,
98
-
"SEMI-TRANSPARENT-QR"
99
-
);
100
-
101
-
generator.getParameters().setBackColor(
102
-
newColor(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
-
118
71
## Configure padding around the barcode
119
72
120
-
Padding adds blank space around the generated barcode image.
73
+
Padding adds blank image space around the generated barcode.
121
74
122
75
```java
123
76
BarcodeGenerator generator =newBarcodeGenerator(
@@ -157,17 +110,35 @@ generator.save(
157
110
158
111
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.
159
112
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.
161
116
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
+
newFile("qr_custom_background.png")
120
+
);
121
+
122
+
Color actualColor =newColor(
123
+
image.getRGB(0, 0),
124
+
true
125
+
);
126
+
127
+
if (actualColor.getRed() !=245
128
+
|| actualColor.getGreen() !=248
129
+
|| actualColor.getBlue() !=252) {
130
+
thrownewIllegalStateException(
131
+
"Unexpected background color."
132
+
);
133
+
}
134
+
```
163
135
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.
165
137
166
138
## Recommendations
167
139
168
140
- 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.
171
142
- Keep enough blank space around the symbol.
172
143
- Do not treat arbitrary padding values as universal quiet-zone dimensions.
173
144
- Validate the final image with the intended scanner and rendering pipeline.
0 commit comments