2008年08月06日

JavaでQRコード生成

JAVAにてQRコードを生成できるらしいので実験。

このサイトよりqrcode_java0.50beta10.tar.gzを取得。

public static void main(String[] args) {

try {

String testString = "123456789";

// Constructor Qrcode Object
Qrcode testQrcode = new Qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);

// getBytes
byte[] d = testString.getBytes("Big5");

BufferedImage bi = new BufferedImage(140, 140,
BufferedImage.TYPE_INT_RGB);

// createGraphics
Graphics2D g = bi.createGraphics();

// set background
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 140, 140);

g.setColor(Color.BLACK);

if (d.length > 0 && d.length < 120) {
boolean[][] s = testQrcode.calQrcode(d);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
}

g.dispose();
bi.flush();

// 出力ファイル名
String FilePath = "D:\\TestQRCode.png";
File f = new File(FilePath);

// 書き出し
ImageIO.write(bi, "png", f);

} // end try
catch (Exception e) {
e.printStackTrace();
} // end catch

} // end main


出来た。世の中色々作ってくれてる人が居て助かります。
posted by hana at 23:25| Comment(0) | TrackBack(0) | JAVA | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。

この記事へのトラックバック