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; BLACK 変数を 0x00000000 に置き換えると、画像が黒くなることがわかりましたが、白を与える 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