我正在使用 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
你可以想象这在像nexus 5这样的1080p屏幕上看起来很糟糕。我有什么问题吗?我需要对 DataMatrix 做一些特殊的设置吗?
谷歌和 Stackoverflow 无法帮助我,因为我找不到任何使用 DataMatrix 的示例
更新 这就是我将位矩阵转换为位图的方式
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(我没想到会有其他任何东西)......
当我尝试缩放图像视图并设置固定的宽度和高度时,条形码是可扫描的,但看起来很糟糕。这也很明显,因为位矩阵只有 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);
例外:
java.lang.IllegalArgumentException: Can't find a symbol arrangement that matches the message. Data codewords: 7
在我看来,最后一个问题就像 zxing 中的一个错误。我不明白为什么如果我改变大小,生成不起作用。