import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateQRCode {
public static void main(String[] args) throws IOException {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//纠错等级(分为L、M、H三个等级)
qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符
//低像素版本
//qrcode.setQrcodeVersion(7);//版本
//为了让像素变高,我们把版本调高,调到20
qrcode.setQrcodeVersion(10);//版本
//生成二维码中要存储的信息
String qrData = "http://www.baidu.com";
//设置一下二维码的像素
//这样不行
/*int width = 300;
int height = 300;*/
//上面的代码中设置的版本qrcode.setQrcodeVersion(7);为7,而我们定义的像素300与之不相符。这就需要我们使用公式来定义宽和高
/* int width = 67 + 12*(7-1);
int height = 67 + 12*(7-1);*/
int width = 67 + 12*(10-1);
int height = 67 + 12*(10-1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘图
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//清除下画板内容
//设置下偏移量,如果不加偏移量,有时会导致出错。
int pixoff = 2;
byte[] d = qrData.getBytes("gb2312");
if (d.length > 0 && d.length < 120) {
boolean[][] s = qrcode.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 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcode.png"));
}
}
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateQRCode {
public static void main(String[] args) throws IOException {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//纠错等级(分为L、M、H三个等级)
qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符
//低像素版本
//qrcode.setQrcodeVersion(7);//版本
//为了让像素变高,我们把版本调高,调到20
qrcode.setQrcodeVersion(10);//版本
//生成二维码中要存储的信息
String qrData = "http://www.baidu.com";
//设置一下二维码的像素
//这样不行
/*int width = 300;
int height = 300;*/
//上面的代码中设置的版本qrcode.setQrcodeVersion(7);为7,而我们定义的像素300与之不相符。这就需要我们使用公式来定义宽和高
/* int width = 67 + 12*(7-1);
int height = 67 + 12*(7-1);*/
int width = 67 + 12*(10-1);
int height = 67 + 12*(10-1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘图
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//清除下画板内容
//设置下偏移量,如果不加偏移量,有时会导致出错。
int pixoff = 2;
byte[] d = qrData.getBytes("gb2312");
if (d.length > 0 && d.length < 120) {
boolean[][] s = qrcode.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 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcode.png"));
}
}