4

我正在使用 ZXing 码创建 QR 条码。我从这里得到了示例代码:http: //www.vineetmanohar.com/2010/09/java-barcode-api/并且 PNG 图像看起来不错,如预期的那样是黑白的。

在示例代码注释中,它提到我可以使用 tiff 或 jpeg 格式,因此我将 imgeFormat 变量更改为 jpeg。创建了一个外观正确的 QR 码图像,但“白色”部分不是黑白,“白色”部分是桃色,“黑色”部分是蓝色。

我将 ZXing MatrixToImageWriter 代码剪切并传递到我的代码中并设置颜色而不是使用 int BLACK = 0xFF000000; 和 int WHITE = 0xFFFFFFFF; 我发现通过用 0x00000000 替换 BLACK 变量,我的图像变黑了,但是我无法找到 WHITE 变量的值,它给我一个白色。

附上奇怪的彩色二维码。哎呀,我太新的用户来附加图像。希望这个指向 imgur.com 的链接有效:http: //imgur.com/QnNXO

这是Java代码:

package zxing_qr;

import java.io.File;
import java.io.FileOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

// by passing MatrixToImageWriter call
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;


public class Main {


public static void main(String[] args) {
    //String text = "98376373783"; // this is the text that we want to encode
    String text = "blahblahblah"; // this is the text that we want to encode

    int width = 400;
    int height = 300; // change the height and width as per your requirement

    // (ImageIO.getWriterFormatNames() returns a list of supported formats)
    String imageFormat = "jpg"; // could be "gif", "tiff", "jpeg"

    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE,     width, height);
        MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("qrcode_97802017507991.jpg")));             
    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} // end main function
} // end main class
4

3 回答 3

3

I have stumbled upon the answer. Inside the ZXing class MatrixToImageWriter a BufferedImage object is created with BufferedImage.TYPE_INT_ARGB as the 3rd argument. By changing this to BufferedImage.TYPE_INT_RGB the colors for jpeg are turning out black and white as expected.

于 2011-05-10T22:10:04.260 回答
0

除了破解 ZXing 类,您还可以通过编程方式将图像像素转换为 RGB:

      ...
        final BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
        final BufferedImage rgbImage = convertARGBtoRGB(bufferedImage);
      ...

  private static BufferedImage convertARGBtoRGB(final BufferedImage argb) {
    assert argb.getType() == BufferedImage.TYPE_INT_ARGB;
    final int width = argb.getWidth();
    final int height = argb.getHeight();
    final BufferedImage rgbImage= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        rgbImage.setRGB(x, y, argb.getRGB(x, y));
      }
    }
    return rgbImage;
  }
于 2014-08-14T08:07:58.230 回答
0

或者您可以自己转换BitMatrix为缓冲图像

private static BufferedImage drawCodeOnImage(BitMatrix bitMatrix) {

    if (bitMatrix == null) {
        throw new IllegalArgumentException("BitMatrix cannot be null");
    }

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

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
    image.createGraphics();

    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, width, height);
    graphics.setColor(Color.BLACK);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            if (bitMatrix.get(i, j)) {
                graphics.fillRect(i, j, 1, 1);
            }
        }
    }

    return image;
}

BufferedImage.TYPE_BYTE_INDEXED之所以使用 GIF 格式,是因为它可以更快地写入 OutputStream。

定义为 0x00.. 的颜色是透明的,定义为 0xFF... 的颜色根据Android 颜色是不透明的。这些颜色已在通话中使用

MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("qrcode_97802017507991.jpg")));

作为默认配置。

于 2015-06-19T07:12:14.140 回答