使用zxing的DataMatrix编码仅生成14px位图

jos*_*usa 7 android barcode zxing datamatrix

我正在使用zxing生成不同类型的条形码(EAN,2of5和DataMatrix).生成一般工作正常.我目前唯一的问题是zxing只生成一个14x14像素的位图,这个位图太小了.但只有在使用DataMatrix时!EAN13,2of5/ITF和QR码与相同的代码完美配合.

我的代码:

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
Run Code Online (Sandbox Code Playgroud)

你可以想象这在像nexus 5这样的1080p屏幕上看起来很糟糕.我有什么问题吗?我是否必须为DataMatrix做一些特殊设置?

谷歌和Stackoverflow无法帮助我,因为我找不到任何使用DataMatrix的例子

DataMatrix条形码的应用截图

更新 这是我将bitmatrix转换为位图的方法

    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            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);
Run Code Online (Sandbox Code Playgroud)

如果我对高度使用任何其他值,我会得到一个非常明显的OutOfBoundsException(我没想到别的)...

当我尝试缩放imageview并设置固定的宽度和高度时,条形码是可扫描的,但看起来像狗屎.这也很明显,因为比特矩阵只有14x14而不是我指定的大小.

在此输入图像描述

是否有一种简单的方法以某种方式缩放比特矩阵?因为它只包含点,所以它应该是可能的,但我不想自己计算它.除了stackoverflow之外,我找不到任何关于bitmatrix的文档,这根本没有帮助我.

如果我通过HintMap将MinWidth或MaxWidth传递给编码器,应用程序总是会因异常而崩溃.HintMap(mWidth是设备的显示宽度,但我尝试了几个值):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);
Run Code Online (Sandbox Code Playgroud)

例外:

java.lang.IllegalArgumentException: Can't find a symbol arrangement that matches the message. Data codewords: 7
Run Code Online (Sandbox Code Playgroud)

这最后一个问题在我看来就像zxing中的一个bug.如果我改变大小,我不明白为什么生成不起作用.

小智 11

这是一个小例子,您可以如何更改从BitMatrix到Bitmap的转换方法.该方法进行BitMatrix的缩放.

int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;

// 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
for (int y = 0; y < height; y++) {
  int offset = y * requestedWidth * pixelsize;

  // scaling pixel height
  for (int pixelsizeHeight = 0; pixelsizeHeight < pixelsize; pixelsizeHeight++, offset+=requestedWidth) {
    for (int x = 0; x < width; x++) {
      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);
Run Code Online (Sandbox Code Playgroud)