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

Android 使用zxing的DataMatrix编码仅生成14px位图,android,barcode,zxing,datamatrix,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); i

我使用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
可以想象,在Nexus5这样的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);
例外情况:

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

最后一期在我看来像是中兴的一个bug。我不明白,如果我改变大小,为什么生成不起作用

我记得,数据矩阵编码器有点古怪,因为代码来自不同的地方(条形码4j)。它将使用最小的适当尺寸,并假设调用者将根据需要缩放图形

您可以在这里执行此操作--只需将
ImageView
设置为缩放其内容。这对任何编码的条形码都不会有影响,只要不启用抗锯齿


还有一个
EncodeHintType.MIN_SIZE
提示,它将设置最小大小,仅用于数据矩阵。

下面是一个小示例,说明如何更改从位矩阵到位图的转换方法。该方法对位矩阵进行缩放

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);
int BLACK=0xFF000000;
int白色=0xFFFFFF;
//根据您的需要更改值
int requestedWidth=300;
int requestedHeight=300;
int-width=bitMatrix.getWidth();
int height=bitMatrix.getHeight();
//计算比例因子
int pixelsize=请求的宽度/宽度;
如果(像素大小>请求的高度/高度)
{
像素大小=请求的高度/高度;
}
int[]像素=新int[requestedWidth*requestedHeight];
//默认情况下,全部为0或黑色
对于(int y=0;y
我知道这个问题是关于Android的,但我认为可以归类为通用问题。
我在和斯威夫特和包装器ZXingObjC合作。我使用DataMatrix编码获得此错误:

'找不到与消息匹配的符号排列。资料 代号:5'

所以,在调试代码时,我来到了类ZXDataMatrixSymbolInfo,关于方法:

+ (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords shape:(ZXDataMatrixSymbolShapeHint)shape minSize:(ZXDimension *)minSize
什么时候发生

我使用以下方法为我的项目编写自定义swift包装器(CoderCoder):

static func encode(string code: String, encoding: CoderEncorderType, size: CGSize = CGSizeMake(200, 200)) -> UIImage? {
    let writer: ZXWriter = ZXMultiFormatWriter.writer() as! ZXWriter
    do {
        let format = CodeEncoder.getEncoding(type: encoding) 
        let result = try writer.encode(code, format: format, width: Int32(size.width), height: Int32(size.height))
        let cgImage = ZXImage(matrix: result, onColor: UIColor.blackColor().CGColor, offColor: UIColor.clearColor().CGColor).cgimage
        return UIImage(CGImage: cgImage)
    } catch let error as NSError  {
        ERLog(what: error)
    }
    return nil
}
所以,当我打电话时:

CoderEncoder.encode("foo", .DataMatrix)
发生错误

基本上,在ZXDataMatrixWriter中,maxSize为零,因此需要指定10到144之间的维度

或者!!或指定形状:

ZXDataMatrixSymbolShapeHintForceSquare
ZXDataMatrixSymbolShapeHintForceRectangle

有没有一种简单的方法来缩放位矩阵

是的,有。问题是使用Android自动缩放会遇到一些问题:

  • Android在缩放时使用双线性过滤
  • 我采用的方法是通过位图的整数因子手动缩放。以下是如何将位矩阵转换为未缩放位图:

     /**
     * Create an unscaled bitmap (1 square = 1 pixel) from a bitmatrix
     *
     * @param bitMatrix the bitmatrix to convert
     * @return a unscaled bitmap
     */
    public Bitmap drawUnscaledBitmap(BitMatrix bitMatrix) {
        final int bmWidth = bitMatrix.getWidth();
        final int bmHeight = bitMatrix.getHeight();
        final Bitmap bitmap = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
        int[] pixels = new int[bmWidth * bmHeight];
        for (int y = 0; y < bmHeight; y++) {
            int offset = y * bmWidth;
            for (int x = 0; x < bmWidth; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }
        bitmap.setPixels(pixels, 0, bmWidth, 0, 0, bmWidth, bmHeight);
        return bitmap;
    }
    

    谢谢缩放ImageView不起作用,因为它会破坏条形码。正如我所说,位矩阵只有14x14像素。很长一段时间以来,我一直在使用EncodeHintType。当我设置最小大小时,我总是得到一个IllegalArgumentException,消息是“找不到与消息匹配的符号排列。数据码字:1”。没有文档说明这到底意味着什么…它将如何销毁条形码?它是纯正方形。不过,在缩放时绝对不能使用抗锯齿。其他可能是一个bug。你到底在编码什么?我已经更新了我原来的帖子来显示更多的代码。在屏幕截图中,你可以看到我试图编码的内容。但我想编码什么并不重要,它不适用于其他值。您应该只设置一个编码提示MIN_SIZE或MAX_SIZE。在您的情况下,仅设置最小大小以避免异常。但我认为结果不是你所期望的。编码提示不会缩放位矩阵。它导致另一个符号形状。谢谢这是我需要的!计算比例并创建位图,这对我来说很有效,非常感谢。无论您做什么,库总是返回一个非常小的数据矩阵,因此唯一的解决方案是对其进行缩放。
     /**
     * Create an unscaled bitmap (1 square = 1 pixel) from a bitmatrix
     *
     * @param bitMatrix the bitmatrix to convert
     * @return a unscaled bitmap
     */
    public Bitmap drawUnscaledBitmap(BitMatrix bitMatrix) {
        final int bmWidth = bitMatrix.getWidth();
        final int bmHeight = bitMatrix.getHeight();
        final Bitmap bitmap = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
        int[] pixels = new int[bmWidth * bmHeight];
        for (int y = 0; y < bmHeight; y++) {
            int offset = y * bmWidth;
            for (int x = 0; x < bmWidth; x++) {
                pixels[offset + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }
        bitmap.setPixels(pixels, 0, bmWidth, 0, 0, bmWidth, bmHeight);
        return bitmap;
    }
    
    /**
     * Upscale a bitmap (maintaining ratio) by the biggest integer scaling factor that allows the bitmap to still fit in the given size
     *
     * @param bitmap the bitmap to upscale
     * @param size   the size that the output scaled bitmap needs to fit in
     * @return the scaled bitmap if the integer scaling factor is > 1; the original bitmap otherwise
     */
    public Bitmap drawScaledBitmap(Bitmap bitmap, Size size) {
        final int bmWidth = bitmap.getWidth();
        final int bmHeight = bitmap.getHeight();
        final int wScalingFactor = size.getWidth() / bmWidth;
        final int hScalingFactor = size.getHeight() / bmHeight;
        final int scalingFactor = Math.min(wScalingFactor, hScalingFactor);
        return scalingFactor > 1 ? Bitmap.createScaledBitmap(bitmap, bmWidth * scalingFactor, bmHeight * scalingFactor, false) : bitmap;
    }