为什么ZXing的jpeg二维码不是黑白的?

时间:2011-05-09 23:15:10

标签: java jpeg zxing

我正在使用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变量的值,它给了我一个白色。

附上奇怪的QR条形码。哎呀,我太新了用户附加图片了。希望这个链接到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

3 个答案:

答案 0 :(得分:3)

我偶然发现了答案。在ZXing类MatrixToImageWriter中,使用BufferedImage.TYPE_INT_ARGB作为第三个参数创建BufferedImage对象。通过将其更改为BufferedImage.TYPE_INT_RGB,jpeg的颜色将按预期变为黑白。

答案 1 :(得分:0)

您也可以通过编程方式将图像像素转换为RGB:

,而不是攻击ZXing类
      ...
        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;
  }

答案 2 :(得分: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 colors是不透明的。这些颜色已在通话中使用

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

作为默认配置。