与zxing DataMatrix二维编码只生成14px的位图 [英] DataMatrix-encoding with zxing only generates 14px bitmap

查看:3189
本文介绍了与zxing DataMatrix二维编码只生成14px的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用zxing产生吧codeS不同类型(EAN,2of5和二维条码)。生成的一般工作正常。唯一的问题我现在有是,只有zxing生成一个14×14像素的位图是这样太小。但只有使用二维条码的时候! EAN13,2of5 / ITF和QR- codeS完美的工作与同code。

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.

我的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

正如你可以想像这看起来pretty的低劣样的联系5.我收到一些错误1080p的屏幕上?我必须做二维条码一些特殊的设置?

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?

谷歌和#1不能帮助我,只要我找不到二维条码的使用

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

更新 这就是我的bitmatrix转换为位图

Update This is how I convert the bitmatrix to a bitmap

    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);

如果我使用任何其他值高度我得到一个OutOfBoundsException是pretty的明显(我没想到的任何东西)...

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

当我尝试缩放的ImageView并设定一个固定的宽度和高度,酒吧code是可扫描,但看起来像狗屎。这是显而易见的也是如此,因为bitmatrix只有14×14,而不是我指定的大小。

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.

有没有一种简单的方法,以某种方式缩放bitmatrix?因为它只包含点,所以它应该是可能的,但我不想来计算它自己。我找不到计算器以外的任何文档bitmatrix,这并没有帮助我的人。

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.

如果我通过HintMap通过一个或了minWidth到了maxWidth的EN codeR应用程序总是崩溃与异常。 HintMap(mWidth是设备的显示宽度,但我试过几个值):     哈希表hintMap                         =新的Hashtable();

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

这最后一个问题我看来,像在zxing的错误。我不明白,为什么发生,如果我改变大小不起作用。

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.

推荐答案

下面是一个小例子,你可以如何改变你的方法,从BitMatrix转换为位图。该方法执行BitMatrix的缩放。

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;
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);

这篇关于与zxing DataMatrix二维编码只生成14px的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆