最近做的项目中设计到了用java生成二维码的地方,查资料后了解到可以用Qrcode中的方法生成与解析,下面看实例
1、下载Qrcode的jar包
关于这个jar包,我在maven中央仓库找到后引入到我项目的pom.xml文件里,但一直无法将jar包下载到我本地的maven来,后来我直接找到了jar包,下载后copy到了我项目的lib文件夹下,我这里提供下载链接:
链接1:https://download.csdn.net/download/weixin_42687829/11250906
链接2:https://pan.baidu.com/s/1kNn5E2ZnBP0UEwFxEwYIqg
提取码:4gxu
2、生成二维码
/**
* 生成二维码
* @author LJ
* @Date 2019/6/20
* @Time 15:57
* @param content :二维码中的内容
* @param imgPath :二维码生成的位置
* @param version :版本
* @return void
*/
public static void encoderQRCode(String content, String imgPath,int version) {
try {
Qrcode qrcodeHandler = new Qrcode();
//设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcodeHandler.setQrcodeEncodeMode('B');
//版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵
qrcodeHandler.setQrcodeVersion(version);
int imgSize = 67 + 12 * (version- 1) ;
System.out.println("二维码中的内容:" + content);
byte[] contentBytes = content.getBytes("gb2312");
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;
//输出内容 > 二维码
System.out.println("二维码中内容的长度:" + contentBytes.length);
if (contentBytes.length > 0 && contentBytes.length < 130) {
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,130 ]. ");
}
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String content = "https://blog.csdn.net/weixin_42687829";
String imgPath = "D:/temp/picture/Test_Qrcode.png";
encoderQRCode(content,imgPath,8);
System.out.println("encoder QRcode success");
}
生成的二维码如下:
用手机扫描后可直接跳转进入我博客的主页:
3、解析二维码
/**
* 解析二维码中的内容
* @author LJ
* @Date 2019/6/20
* @Time 15:56
* @param imgPath :二维码的位置
* @return java.lang.String
*/
public static String decoderQRCode(String imgPath) {
// QRCode 二维码图片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String decodedData = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
decodedData = new String(decoder.decode(new J2SEImage(bufImg)),"gb2312");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
static class J2SEImage implements QRCodeImage {
BufferedImage bufImg;
public J2SEImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
public int getWidth() {
return bufImg.getWidth();
}
public int getHeight() {
return bufImg.getHeight();
}
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
}
public static void main(String[] args) {
String imgPath = "D:/temp/picture/Test_Qrcode.png";
System.out.println("二维码中的内容:" + decoderQRCode(imgPath));
System.out.println("decoderQRCode QRcode success");
}
解析结果如下: