I am trying to generate a Code128 barcode using Zxing:
try {
int width = (int) (barcode.getWidth() * 0.95f);
int height = (int) (width * 0.2f);
BitMatrix bitMatrix = writer.encode(code.number, BarcodeFormat.CODE_128, width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
bitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
}
}
barcode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
Where barcode
is an ImageView
, whose width is set to match_parent
(with 5dp margin) and height is wrap_content
. The image I am generating has an aspect ratio of 5:1, and I expected the black stripes to appear in the very beginning of the image, and finish at the very end.
This is what happens, though:
The ImageView
has the green background (note that the image width is 95% of the ImageView
width), and the white part belongs to the image. As you can see, there's a huge white area, maybe 40% of the image.
The question is why does the stripes not fill the full (or most of the) width I specified on the writer.encode()
method? How can I force this to happen?
Update:
Using different values for the scale type, I noticed that the white margin is present only on the sides of the barcode, and never above or below it.