需要jar包:http://pan.baidu.com/s/1o7V3bxK
package com.dx.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CodeUtil {
public static void main(String[] args) {
try{
//定义一个二维码对象
Qrcode qcode = new Qrcode();
//设置纠错率L,M,Q,H依次递增
qcode.setQrcodeErrorCorrect('M');
//设置编码模式
qcode.setQrcodeEncodeMode('B');
//设置版本号
qcode.setQrcodeVersion(7);
//画图
int width = 140;
int height = 140;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphics2D gs = (Graphics2D)img.getGraphics();
gs.setBackground(Color.white);
gs.setColor(Color.black);
gs.clearRect(0, 0, width, height);
String content = "我爱你";
boolean[][] datas = qcode.calQrcode(content.getBytes());
for(int i = 0; i < datas.length; i++){
for(int j = 0; j < datas[i].length; j++){
if(datas[i][j]){
gs.fillRect(i*3 +2, j*3+2, 3, 3);
}
}
}
//将图片写入本地文件
ImageIO.write(img, "jpeg", new File("D:\\1.jpg"));
System.out.println("success");
}catch(Exception e){
e.printStackTrace();
}
}
}