|
1 | 1 | --- |
2 | 2 | title: "Customize Barcode Borders" |
3 | | -description: "Learn how to configure barcode image borders, colors, widths, line styles, and ITF-14 bearer bars in Aspose.BarCode for Java." |
| 3 | +description: "Learn how to configure barcode image borders, colors, widths, padding, and ITF-14 bearer bars in Aspose.BarCode for Java." |
4 | 4 | type: docs |
5 | 5 | weight: 30 |
6 | 6 | url: /java/developer-guide/barcode-generation/visual-parameters/borders/ |
7 | 7 | --- |
8 | 8 |
|
9 | 9 | # Customize Barcode Borders |
10 | 10 |
|
11 | | -Aspose.BarCode for Java can draw a border around the generated barcode image. You can configure border visibility, color, width, and line style. |
| 11 | +Aspose.BarCode for Java can draw a decorative border around the generated barcode image. You can configure border visibility, color, and width. |
12 | 12 |
|
13 | 13 | For ITF-14, separate bearer-bar settings are available because bearer bars are part of the symbology-specific layout rather than a decorative image border. |
14 | 14 |
|
15 | 15 | The complete source code for the examples in this article is available on GitHub: |
16 | 16 |
|
17 | 17 | <a href="https://github.com/aspose-barcode/Aspose.BarCode-for-Java/blob/master/src/test/java/com/aspose/barcode/guide/generation/visual_parameters/BordersExample.java" target="_blank">View BordersExample.java</a> |
18 | 18 |
|
19 | | -set |
20 | | - |
21 | 19 | ## Enable an image border |
22 | 20 |
|
| 21 | +Use `BorderParameters` to enable a border around the complete generated image. |
| 22 | + |
23 | 23 | ```java |
24 | 24 | BarcodeGenerator generator = new BarcodeGenerator( |
25 | 25 | EncodeTypes.CODE_128, |
26 | | - "BORDER-EXAMPLE" |
| 26 | + "BORDER-RED-2PT" |
27 | 27 | ); |
28 | 28 |
|
29 | | -generator.getParameters() |
30 | | - .getBorder() |
31 | | - .setVisible(true); |
| 29 | +BorderParameters border = |
| 30 | + generator.getParameters().getBorder(); |
32 | 31 |
|
33 | | -generator.save( |
34 | | - "code128_border.png", |
35 | | - BarCodeImageFormat.PNG |
36 | | -); |
| 32 | +border.setVisible(true); |
| 33 | +border.getWidth().setPoint(2.0f); |
| 34 | +border.setColor(new Color(0xE5, 0x2B, 0x2B)); |
37 | 35 | ``` |
38 | 36 |
|
39 | | -## Set border color and width |
| 37 | +The border width can be specified through the `Unit` returned by `getWidth()`. |
| 38 | + |
| 39 | +## Keep the border outside the barcode area |
| 40 | + |
| 41 | +A decorative border should remain visually separate from the barcode bars or modules. Add enough padding so that the border does not intrude into the required blank margins. |
40 | 42 |
|
41 | 43 | ```java |
42 | 44 | generator.getParameters() |
43 | | - .getBorder() |
44 | | - .setColor(Color.GRAY); |
| 45 | + .getBarcode() |
| 46 | + .getPadding() |
| 47 | + .getLeft() |
| 48 | + .setPixels(14); |
45 | 49 |
|
46 | 50 | generator.getParameters() |
47 | | - .getBorder() |
48 | | - .getWidth() |
49 | | - .setPixels(2); |
50 | | -``` |
| 51 | + .getBarcode() |
| 52 | + .getPadding() |
| 53 | + .getRight() |
| 54 | + .setPixels(14); |
51 | 55 |
|
52 | | -## Set the border line style |
| 56 | +generator.getParameters() |
| 57 | + .getBarcode() |
| 58 | + .getPadding() |
| 59 | + .getTop() |
| 60 | + .setPixels(10); |
53 | 61 |
|
54 | | -```java |
55 | 62 | generator.getParameters() |
56 | | - .getBorder() |
57 | | - .setDashStyle(BorderDashStyle.SOLID); |
| 63 | + .getBarcode() |
| 64 | + .getPadding() |
| 65 | + .getBottom() |
| 66 | + .setPixels(10); |
58 | 67 | ``` |
59 | 68 |
|
60 | | -Depending on the library version, additional dash styles can be available. |
| 69 | +Then save the image: |
61 | 70 |
|
62 | | -A decorative border should remain visually separate from the barcode bars or modules. Provide enough padding so that the border does not intrude into the required blank margins. |
| 71 | +```java |
| 72 | +generator.save( |
| 73 | + "c128_border_red_2pt_on_white.png", |
| 74 | + BarCodeImageFormat.PNG |
| 75 | +); |
| 76 | +``` |
63 | 77 |
|
64 | | -## Use a border with a custom background |
| 78 | +## Add a white border on a dark background |
| 79 | + |
| 80 | +A barcode image can use a dark background, light modules, and a contrasting border. |
65 | 81 |
|
66 | 82 | ```java |
67 | 83 | BarcodeGenerator generator = new BarcodeGenerator( |
68 | | - EncodeTypes.CODE_128, |
69 | | - "BORDER-STYLE" |
| 84 | + EncodeTypes.QR, |
| 85 | + "QR-WHITE-BORDER" |
70 | 86 | ); |
71 | 87 |
|
72 | | -generator.getParameters().setBackColor( |
73 | | - new Color(245, 248, 252) |
74 | | -); |
| 88 | +generator.getParameters() |
| 89 | + .setBackColor(Color.BLACK); |
| 90 | + |
| 91 | +generator.getParameters() |
| 92 | + .getBarcode() |
| 93 | + .setBarColor(Color.WHITE); |
| 94 | + |
| 95 | +BorderParameters border = |
| 96 | + generator.getParameters().getBorder(); |
| 97 | + |
| 98 | +border.setVisible(true); |
| 99 | +border.getWidth().setPoint(2.0f); |
| 100 | +border.setColor(Color.WHITE); |
75 | 101 |
|
76 | 102 | generator.getParameters() |
77 | 103 | .getBarcode() |
78 | 104 | .getPadding() |
79 | 105 | .getLeft() |
80 | | - .setPixels(20); |
| 106 | + .setPixels(16); |
81 | 107 |
|
82 | 108 | generator.getParameters() |
83 | 109 | .getBarcode() |
84 | 110 | .getPadding() |
85 | 111 | .getRight() |
86 | | - .setPixels(20); |
| 112 | + .setPixels(16); |
| 113 | + |
| 114 | +generator.getParameters() |
| 115 | + .getBarcode() |
| 116 | + .getPadding() |
| 117 | + .getTop() |
| 118 | + .setPixels(16); |
87 | 119 |
|
88 | 120 | generator.getParameters() |
89 | | - .getBorder() |
90 | | - .setVisible(true); |
| 121 | + .getBarcode() |
| 122 | + .getPadding() |
| 123 | + .getBottom() |
| 124 | + .setPixels(16); |
91 | 125 |
|
92 | 126 | generator.getParameters() |
93 | | - .getBorder() |
94 | | - .setColor(Color.DARK_GRAY); |
| 127 | + .getImageWidth() |
| 128 | + .setPixels(260); |
95 | 129 |
|
96 | 130 | generator.getParameters() |
97 | | - .getBorder() |
98 | | - .getWidth() |
99 | | - .setPixels(2); |
| 131 | + .getImageHeight() |
| 132 | + .setPixels(260); |
100 | 133 |
|
101 | 134 | generator.save( |
102 | | - "code128_border_complete.png", |
| 135 | + "qr_white_border_on_black.png", |
103 | 136 | BarCodeImageFormat.PNG |
104 | 137 | ); |
105 | 138 | ``` |
106 | 139 |
|
| 140 | +This example creates a white QR Code and a white solid border on a black background. |
| 141 | + |
| 142 | +Inverted barcodes are not supported equally by all scanners. A generated image can be visually correct even when a particular reader does not recognize it. Therefore, the related test verifies that the image is created but does not require successful barcode recognition. |
| 143 | + |
| 144 | +For maximum interoperability, use dark modules on a light background. |
| 145 | + |
| 146 | +## Set border width in physical units |
| 147 | + |
| 148 | +Border width can also be specified in millimeters for a target resolution. |
| 149 | + |
| 150 | +```java |
| 151 | +BarcodeGenerator generator = new BarcodeGenerator( |
| 152 | + EncodeTypes.EAN_13, |
| 153 | + "5901234123457" |
| 154 | +); |
| 155 | + |
| 156 | +BorderParameters border = |
| 157 | + generator.getParameters().getBorder(); |
| 158 | + |
| 159 | +border.setVisible(true); |
| 160 | +border.setColor(new Color(0x0B, 0x57, 0xD0)); |
| 161 | + |
| 162 | +Unit borderWidth = border.getWidth(); |
| 163 | + |
| 164 | +borderWidth.updateResolution(300f); |
| 165 | +borderWidth.setMillimeters(1.0f); |
| 166 | +``` |
| 167 | + |
| 168 | +In this example, the border width is configured as 1 mm at 300 DPI. |
| 169 | + |
107 | 170 | ## Configure ITF-14 bearer bars |
108 | 171 |
|
109 | 172 | ITF-14 uses bearer bars that are specific to the symbology. |
110 | 173 |
|
111 | 174 | ```java |
112 | 175 | BarcodeGenerator generator = new BarcodeGenerator( |
113 | 176 | EncodeTypes.ITF_14, |
114 | | - "12345678901231" |
| 177 | + "10012345000017" |
115 | 178 | ); |
116 | 179 |
|
117 | | -generator.getParameters() |
| 180 | +ITFParameters itfParameters = generator.getParameters() |
118 | 181 | .getBarcode() |
119 | | - .getITF() |
120 | | - .setItfBorderType(ITF14BorderType.FRAME); |
| 182 | + .getITF(); |
121 | 183 |
|
122 | | -generator.getParameters() |
| 184 | +itfParameters.setItfBorderType( |
| 185 | + ITF14BorderType.FRAME |
| 186 | +); |
| 187 | + |
| 188 | +Unit bearerThickness = |
| 189 | + itfParameters.getItfBorderThickness(); |
| 190 | + |
| 191 | +bearerThickness.updateResolution(300f); |
| 192 | +bearerThickness.setMillimeters(2.5f); |
| 193 | + |
| 194 | +itfParameters.setQuietZoneCoef(12); |
| 195 | +``` |
| 196 | + |
| 197 | +The `FRAME` option creates a rectangular bearer around the barcode. |
| 198 | + |
| 199 | +## Use an alternative ITF-14 bearer layout |
| 200 | + |
| 201 | +ITF-14 also supports alternative bearer layouts such as `BAR_OUT`. |
| 202 | + |
| 203 | +```java |
| 204 | +BarcodeGenerator generator = new BarcodeGenerator( |
| 205 | + EncodeTypes.ITF_14, |
| 206 | + "10012345000017" |
| 207 | +); |
| 208 | + |
| 209 | +ITFParameters itfParameters = generator.getParameters() |
123 | 210 | .getBarcode() |
124 | | - .getITF() |
125 | | - .getItfBorderThickness() |
126 | | - .setPixels(4); |
| 211 | + .getITF(); |
127 | 212 |
|
128 | | -generator.save( |
129 | | - "itf14_frame.png", |
130 | | - BarCodeImageFormat.PNG |
| 213 | +itfParameters.setItfBorderType( |
| 214 | + ITF14BorderType.BAR_OUT |
131 | 215 | ); |
| 216 | + |
| 217 | +Unit bearerThickness = |
| 218 | + itfParameters.getItfBorderThickness(); |
| 219 | + |
| 220 | +bearerThickness.updateResolution(300f); |
| 221 | +bearerThickness.setMillimeters(2.0f); |
| 222 | + |
| 223 | +itfParameters.setQuietZoneCoef(12); |
132 | 224 | ``` |
133 | 225 |
|
134 | | -Available bearer-bar layouts can include frame and bar variants. The exact physical dimensions should be validated against the applicable barcode specification and printing process. |
| 226 | +The exact bearer type, thickness, and quiet-zone dimensions should be validated against the applicable barcode specification and printing process. |
| 227 | + |
| 228 | +## Decorative borders and bearer bars |
| 229 | + |
| 230 | +A decorative image border and an ITF-14 bearer bar serve different purposes: |
| 231 | + |
| 232 | +- a decorative border surrounds the complete generated image; |
| 233 | +- an ITF-14 bearer bar is part of the barcode-specific layout; |
| 234 | +- padding separates a decorative border from the barcode; |
| 235 | +- `QuietZoneCoef` controls the ITF-14 quiet zone in multiples of X-dimension. |
| 236 | + |
| 237 | +Do not replace required ITF-14 bearer bars with a decorative image border. |
135 | 238 |
|
136 | 239 | ## Recommendations |
137 | 240 |
|
138 | 241 | - Keep decorative borders outside the required quiet zones. |
139 | 242 | - Use sufficient padding between the barcode and the border. |
140 | | -- Avoid thick borders near linear barcode bars. |
| 243 | +- Avoid thick borders close to linear barcode bars. |
| 244 | +- Prefer dark bars or modules on a light background for reliable recognition. |
| 245 | +- Do not assume that inverted barcodes are supported by every scanner. |
141 | 246 | - Treat ITF-14 bearer bars as symbology-specific elements. |
142 | | -- Validate the result with the intended printer and scanner. |
| 247 | +- Validate physical dimensions against the target resolution and printing process. |
| 248 | +- Test the final image with the intended scanner. |
0 commit comments