Take the 2-minute tour ×

I'm using zxing to generate barcodes with different types (EAN, 2of5 and DataMatrix). Generating in general works fine. The only problem I currently have is that zxing only generates a 14x14 pixel bitmap which is way too small. But only when using DataMatrix! EAN13, 2of5/ITF and QR-Codes work perfect with the same code.

My code:

BitMatrix bitMatrix = new DataMatrixWriter().encode(message, BarcodeFormat.DATA_MATRIX, 1080, 1080, null);
int height = bitMatrix.getHeight(); //height is always 14, it doesn't matter what value I pass to the encoder

As you can imagine this looks pretty shitty on a 1080p screen like the nexus 5. Am I getting something wrong? Do I have to do some special settings for DataMatrix?

Google and Stackoverflow couldn't help me so far as I can't find any examples for the usage of DataMatrix

App screenshot of DataMatrix barcode

Update This is how I convert the bitmatrix to a bitmap

    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    // All are 0, or black, by default
        int offset = y * width;
            pixels[offset + x] = bitMatrix.get(x, y) ? BLACK : WHITE;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

If I use any other values for the height I get an OutOfBoundsException which is pretty obvious (I didn't expect anything else)...

When I try to scale the imageview and set a fixed width and height, the barcode is scannable but looks like shit. This is obvious too, as the bitmatrix is only 14x14 instead of the size I specified.

enter image description here

Is there a simple way to somehow scale a bitmatrix? Because it only consists of dots so it should be possible but I don't want to calculate it myself. I couldn't find any documentation for bitmatrix besides stackoverflow and this didn't help me at all.

If I pass a MinWidth or MaxWidth to the encoder via HintMap the app always crashes with an Exception. HintMap (mWidth is the display width of the device but I tried several values): Hashtable hintMap = new Hashtable();

hintMap.put(EncodeHintType.MIN_SIZE, new Dimension(mWidth, mWidth));
hintMap.put(EncodeHintType.MAX_SIZE, new Dimension(mWidth, mWidth));
hintMap.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);


java.lang.IllegalArgumentException: Can't find a symbol arrangement that matches the message. Data codewords: 7

This last issue seems to me like a bug in zxing. I don't get it why the generating doesn't work if I change the size.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Here is a small example how you can change your method for the conversion from BitMatrix to Bitmap. The method does the scaling of the BitMatrix.

int BLACK = 0xFF000000;

// change the values to your needs
int requestedWidth = 300;
int requestedHeight = 300;

int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();

// calculating the scaling factor
int pixelsize = requestedWidth/width;
if (pixelsize > requestedHeight/height)
  pixelsize = requestedHeight/height;

int[] pixels = new int[requestedWidth * requestedHeight];
// All are 0, or black, by default
  int offset = y * requestedWidth * pixelsize;

  // scaling pixel height
  for (int pixelsizeHeight = 0; pixelsizeHeight < pixelsize; pixelsizeHeight++, offset+=requestedWidth) {
      int color = bitMatrix.get(x, y) ? BLACK : WHITE;

      // scaling pixel width
      for (int pixelsizeWidth = 0; pixelsizeWidth < pixelsize; pixelsizeWidth++) {
        pixels[offset + x * pixelsize + pixelsizeWidth] = color;
Bitmap bitmap = Bitmap.createBitmap(requestedWidth, requestedHeight, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, requestedWidth, 0, 0, requestedWidth, requestedHeight);

// I could only test it with BufferedImage and a modified version of the zxing J2SE client
// BufferedImage bitmap = new BufferedImage(requestedWidth, requestedHeight, BufferedImage.TYPE_INT_ARGB);
// bitmap.getRaster().setDataElements(0, 0, requestedWidth, requestedHeight, pixels);
share|improve this answer
Thanks this is what I needed! Calculating the scale and creating the bitmap just in the resultion I need. –  joshplusa Apr 3 at 8:00

As I remember, the Data Matrix encoder is kind of the odd man out since the code came from a different place (barcode4j). It will use the minimum appropriate dimensions and assumes the caller will scale the graphic as desired.

You can do that here -- just set the ImageView to scale its contents. That shouldn't hurt for any of the encoded barcodes, as long as you do not enable anti-aliasing.

There is also a EncodeHintType.MIN_SIZE hint which will set the minimum size, just for Data Matrix.

share|improve this answer
Thanks. Scaling the ImageView doesn't work as it destroys the barcode. As I said, the bitmatrix is only 14x14 pixels. I'm messing with the EncodeHintType for a long time now. When I set a min size I always get an IllegalArgumentException with the message "Can't find a symbol arrangement that matches the message. Data codewords: 1". There's no documentation what exactly this means... –  joshplusa Apr 1 at 15:34
How would it destroy the barcode? It is pure squares. You definitely can't use antialiasing in scaling though. –  Sean Owen Apr 1 at 16:48
Other could be a bug. What are you encoding and exactly? –  Sean Owen Apr 1 at 16:49
I've updated my original post to show more code. In the screenshot you can see what I'm trying to encode. But it doesn't matter what I want to encode, it doesn't work with other values. –  joshplusa Apr 2 at 9:19
You should only set one of the encoding hints MIN_SIZE or MAX_SIZE. In your case set only MIN_SIZE to avoid the exception. But I think the result isn't what you expect. The encoding hint doesn't scale the BitMatrix. It leads to another symbol shape. –  Michael Apr 2 at 19:28

Your Answer


