二维码生成及源码

private static void D1() {

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值