Skip to content

Commit

Permalink
Merge pull request #696 from saifkhichi96/patch-1
Browse files Browse the repository at this point in the history
Added ability to customize colors in barcodes generated with BarcodeEncoder.
  • Loading branch information
rkistner authored Feb 21, 2022
2 parents 2324a54 + 8173d4a commit b6c6421
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ try {
} catch(Exception e) {

}
```

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

```

### Changing the orientation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@
* Licensed under the Apache License, Version 2.0.
*/
public class BarcodeEncoder {
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
private int bgColor = 0xFFFFFFFF;
private int fgColor = 0xFF000000;


public BarcodeEncoder() {
}

public void setBackgroundColor(int bgColor) {
this.bgColor = bgColor;
}

public void setForegroundColor(int fgColor) {
this.fgColor = fgColor;
}

public Bitmap createBitmap(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
pixels[offset + x] = matrix.get(x, y) ? fgColor : bgColor;
}
}

Expand Down

0 comments on commit b6c6421

Please sign in to comment.