我正在使用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
解决方法:
我偶然发现了答案.在ZXing类MatrixToImageWriter中,使用BufferedImage.TYPE_INT_ARGB作为第三个参数创建BufferedImage对象.通过将其更改为BufferedImage.TYPE_INT_RGB,jpeg的颜色将按预期变为黑白.