BufferedImage bi = new BufferedImage(120, 120,
BufferedImage.TYPE_INT_RGB);
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');
x.setQrcodeEncodeMode('B');
x.setQrcodeVersion(3);
String str = "祝各位小盆友节日快乐!!";
byte[] d = str.getBytes();
// createGraphics
Graphics2D g = bi.createGraphics();
// set background
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 120, 120);
// 設定字型顏色 => BLACK
g.setColor(Color.BLACK);
if (d.length > 0 && d.length < 120) {
boolean[][] s = x.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();
try {
File f = new File(
"E://software//java//qrcode//qrcode//res//test7.jpg"); // 输
出到文件流
ImageIO.write(bi, "jpg", f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class decodeImage {
public static void main(String[] args) {
// TODO Auto-generated method stub
// if (args.length < 1) {
// System.out.println("Usage: QRCodeDecoderCUIExample imageFilePath");
// System.exit(0);
// }
String file = "E://software//java//qrcode//qrcode//res//test7" + ".jpg";
de(file);
}
private static void de(String file) {
QRCodeDecoder decoder = new QRCodeDecoder();
File imageFile = new File(file);
BufferedImage image = null;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
try {
String decodedData = new String(decoder
.decode(new J2SEImage(image)));
System.out.println(decodedData);
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
}
}
}
class J2SEImage implements QRCodeImage {
BufferedImage image;
public J2SEImage(BufferedImage image) {
this.image = image;
}
public int getWidth() {
return image.getWidth();
}
public int getHeight() {
return image.getHeight();
}
public int getPixel(int x, int y) {
return image.getRGB(x, y);
}
}
参考
http://www.swetake.com/qr/java/qr_java.html
http://qrcode.sourceforge.jp