Skip to content

Commit b6c6421

Browse files
authored
Merge pull request #696 from saifkhichi96/patch-1
Added ability to customize colors in barcodes generated with BarcodeEncoder.
2 parents 2324a54 + 8173d4a commit b6c6421

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@ try {
172172
} catch(Exception e) {
173173

174174
}
175+
```
175176

176-
No customization of the image is currently supported, including changing colors or padding. If you
177-
require more customization, copy and modify the source for the encoder.
177+
To customize the generated barcode image, use the `setBackgroundColor` and `setForegroundColor` functions of the
178+
`BarcodeEncoder` class with a [`@ColorInt`](https://developer.android.com/reference/androidx/annotation/ColorInt)
179+
value to update the background and foreground colors of the barcode respectively. By default, the barcode has a
180+
white background and black foreground.
178181

179-
```
180182

181183
### Changing the orientation
182184

zxing-android-embedded/src/com/journeyapps/barcodescanner/BarcodeEncoder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@
1919
* Licensed under the Apache License, Version 2.0.
2020
*/
2121
public class BarcodeEncoder {
22-
private static final int WHITE = 0xFFFFFFFF;
23-
private static final int BLACK = 0xFF000000;
22+
private int bgColor = 0xFFFFFFFF;
23+
private int fgColor = 0xFF000000;
2424

2525

2626
public BarcodeEncoder() {
2727
}
2828

29+
public void setBackgroundColor(int bgColor) {
30+
this.bgColor = bgColor;
31+
}
32+
33+
public void setForegroundColor(int fgColor) {
34+
this.fgColor = fgColor;
35+
}
36+
2937
public Bitmap createBitmap(BitMatrix matrix) {
3038
int width = matrix.getWidth();
3139
int height = matrix.getHeight();
3240
int[] pixels = new int[width * height];
3341
for (int y = 0; y < height; y++) {
3442
int offset = y * width;
3543
for (int x = 0; x < width; x++) {
36-
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
44+
pixels[offset + x] = matrix.get(x, y) ? fgColor : bgColor;
3745
}
3846
}
3947

0 commit comments

Comments
 (0)