您的位置:首页 > 编程语言 > Java开发

JAVA生成二维码图片代码

2015-03-14 00:27 525 查看
首先需要导入 QRCode.jar 包

下载地址看这里 http://pan.baidu.com/s/1o6qRFqM
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;

public class QrcodeUtil {
public static void main(String[] args) {

//生成二维码
String content = "http://user.qzone.qq.com/913240046";
String imgPath = "F:\\test.png";
String ccpath="F:\\center.png";
createQRCode(content, imgPath,ccpath);

   String content2 = "http://user.qzone.qq.com/913240046";
String imgPath2 = "F:\\test2.png";
QrcodeImg(content2,imgPath2);

}

/**
* @param content 内容
* @param imgPath 生成二维码图片的地址
* @param ccbPath 二维码中间图片
* @return
*/
public static int createQRCode(String content, String imgPath, String ccbPath) {
try {
Qrcode qrcodeHandler = new Qrcode();
//设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('Q');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcodeHandler.setQrcodeEncodeMode('B');
// 设置设置二维码版本,取值范围1-40,值越大尺寸越大,可存储的信息越大
int size = 12;
qrcodeHandler.setQrcodeVersion(size);
int imgSize = 67 + 12 * (size - 1);

byte[] contentBytes = content.getBytes("utf-8");
BufferedImage bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();

gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);

// 设定图像颜色 > BLACK
gs.setColor(Color.BLACK);

// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容 > 二维码
if (contentBytes.length > 0 && contentBytes.length <800) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = "
+ contentBytes.length + " not in [ 0,800]. ");
return -1;
}
Image img = ImageIO.read(new File(ccbPath));//实例化一个Image对象。
gs.drawImage(img, 75, 75, null);//设置二维码中间图片的位置

gs.dispose();
bufImg.flush();

// 生成二维码QRCode图片
File imgFile = new File(imgPath);
ImageIO.write(bufImg, "png", imgFile);

} catch (Exception e)
{
e.printStackTrace();
return -100;
}
return 0;
}

/**
* @param content 二维码中包含的内容
* @param imgPath 二维码生成保存路劲
*/
public static void QrcodeImg(String content,String imgPath){
try {

Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);

int width = 140;
int height = 140;

BufferedImage bufImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width, height);
gs.setColor(Color.BLACK);

byte[] contentBytes = content.getBytes("utf-8");
int pixoff = 2;

//长度限制
if(contentBytes.length>0 && contentBytes.length<120){
boolean[][] codeOut = qrcode.calQrcode(contentBytes);
for(int i = 0;i<codeOut.length;i++){
for(int j = 0;j<codeOut.length;j++){
if(codeOut[j][i]){
gs.fillRect(j*3 + pixoff, i*3 +pixoff, 3, 3);
}
}
}
}else{
System.out.println("出错了");
}
gs.dispose();
bufImg.flush();

File imgFile = new File(imgPath);
try {
ImageIO.write(bufImg, "png", imgFile);
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

}

//生成的二维码图片



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: