1.使用Q-code生成二维码
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect(‘M’);//纠错等级
x.setQrcodeEncodeMode(‘B’);//B其他数字
x.setQrcodeVersion(7);//版本
int width=67+12*(7-1);//图片的宽度
int height=67+12*(7-1);//图片的高度
String qrData = “www.baidu.com”;
BufferedImage buffreImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//设置图片的高度
Graphics2D gs = buffreImage.createGraphics();//图片画笔
gs.setBackground(Color.white);//白色背景
gs.setColor(Color.black);//字体的颜色
gs.clearRect(0, 0, width, height);//画矩形
byte[] d=new byte[120];//使用字节保存字符串
try {
d = qrData.getBytes(“utf-8”);//字节编码方式
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int pos = 5;
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]) {
gs.fillRect(j * 3 + pos, i * 3 + pos, 3, 3);//填充矩形
}
}
}
}
gs.dispose();
buffreImage.flush();//刷新流
try {
ImageIO.write(buffreImage, "png", new File("F:/qrcode.png"));//使用ImageIo进行生成
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用zxing1
int width =200;
int height=200;
String format="png";
String content="hbnb";
HashMap map=new HashMap();
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN, 2);
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Path file =new File("D:"+File.separator+"wangzhongyuan.png").toPath();
try {
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}