Java 为什么我的ZXing的jpeg二维码不是黑白的?
我正在使用ZXing代码创建二维条码。我从这里得到了示例代码:PNG图像看起来很好,黑白如预期 在示例代码注释中,它提到我可以使用tiff或jpeg格式,因此我将imgeFormat变量更改为jpeg。创建了外观正确的二维码图像,但“白色”部分为桃红色,“黑色”部分为蓝色,而非黑白 我将ZXing MatrixToImageWriter代码剪切并转换为我的代码,并设置颜色,而不是使用int BLACK=0xFF000000;和int-WHITE=0xFFFFFFFF;我发现,通过用0x00000000替换黑色变量,我的图像变为黑色,但我无法找到白色变量的值,该值为白色 附加的是奇色QR条形码。哎呀,我是一个新的用户,无法附加图像。希望这个到imgur.com的链接能起作用: 以下是Java代码:Java 为什么我的ZXing的jpeg二维码不是黑白的?,java,jpeg,zxing,Java,Jpeg,Zxing,我正在使用ZXing代码创建二维条码。我从这里得到了示例代码:PNG图像看起来很好,黑白如预期 在示例代码注释中,它提到我可以使用tiff或jpeg格式,因此我将imgeFormat变量更改为jpeg。创建了外观正确的二维码图像,但“白色”部分为桃红色,“黑色”部分为蓝色,而非黑白 我将ZXing MatrixToImageWriter代码剪切并转换为我的代码,并设置颜色,而不是使用int BLACK=0xFF000000;和int-WHITE=0xFFFFFFFF;我发现,通过用0x00000
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
我偶然发现了答案。在ZXing类MatrixToImageWriter中,创建了一个BuffereImage对象,其中BuffereImage.TYPE_INT_ARGB作为第三个参数。通过将其更改为BuffereImage.TYPE_INT_RGB,jpeg的颜色将按预期变成黑白 除了破解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;
}
。。。
最终BuffereImage BuffereImage=MatrixToImageWriter.toBufferedImage(矩阵);
最终BuffereImage rgbImage=convertARGBtoRGB(BuffereImage);
...
专用静态缓冲区映像转换器argbTorgb(最终缓冲区映像argb){
断言argb.getType()==BuffereImage.TYPE\u INT\u argb;
final int width=argb.getWidth();
最终整数高度=argb.getHeight();
最终BuffereImage RGB图像=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
对于(int x=0;x
或者您可以自行将位矩阵转换为缓冲图像
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;
}
作为默认配置。此处为开发者。很高兴你找到了答案,但是,这对我来说没有意义。四字节值是ARGB值。这就是为什么第一个字节,即alpha(不透明度)必须是FF。这在我见过的任何其他平台上都不会发生,所以我想知道您的Java实现是否有些奇怪?不知道…谢谢你的评论肖恩。我在这方面的经验很少,所以我不确定应该看什么来更好地理解这个问题。现在我的解决方案对我有效。如果你介意的话,我在Windows XP上使用NetBeans 6.9.1。使用Java 1.6.0_24。对于ZXing,我使用了core-1.6-SNAPSHOT.jar和javase-1.6-SNAPSHOT.jar。我在Firefox 3.6.17、Gimp和Windows Picture and Fax viewer中查看了JPG。我们可以用userinput十六进制ARGB自己设置颜色吗?这几乎是5年前的事了(该死…),我只是用setRGB
强制将每个像素强制设置为所需的颜色,有关详细信息,请参阅BuffereImage
类的文档。
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("qrcode_97802017507991.jpg")));