gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:12:51 24 4
gpt4 key购买 nike

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

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

我将 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

最佳答案

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

关于java - 为什么我的 ZXing 的 jpeg 二维码不是黑白的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5943543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com