版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/sinat_34104446/article/details/82828261
目录
一、工程描述
1.二维码生成可以使用zxing和QRCode,本次使用Java+QRCode生成二维码
2.创建工程需要导入QRCode的开发jar包,下载链接:https://pan.baidu.com/s/1lUqrd7ooMu-DyhV_mTLjDg
提取码:rpmo
二、工程代码
package com.codecoord;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* @Copyright : www.codecoord.com Inc. All rights reserved.
* @Author : 田鑫
* @CreateDate : 2018/9/24 9:41
* @Version : V1.0
* @Description: 使用QRCode生成二维码
*/
class QrCodeAssis{
private String imgPath; // 二维码保存路径
private String content; // 二维码内容
/**
* @Description: 接收保存路径和内容
* @Date : 2018/9/24 10:02
* @Author : 田鑫
* @Param : [imgPath 二维码保存路径, content 二维码内容]
*/
QrCodeAssis(String imgPath, String content) {
this.imgPath = imgPath;
this.content = content;
}
/**
* @Description:
* @Date : 2018/9/24 10:14
* @Author : 田鑫
* @Param : [width 图片宽度>=100, height 图片高度>=100]
* @Return : boolean 二维码是否生成成功
* @Exception :
*/
boolean createCode(int width, int height) {
boolean flag = true;
try {
Qrcode qrcode = new Qrcode(); // 创建Qrcode对象
// 排错率可选(%)-L(7):M(15):Q(25):H(30)
qrcode.setQrcodeErrorCorrect('M');
// 编码模式-Numeric(M-数字):Binary(B-二进制):KanJi(K-汉字):Alphanumeric(A-英文字母)
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(3); // 设置版本(可选)
width = width >= 100 ? width : 100; // 宽度至少100
height = height >= 100 ? height: 100; // 高度至少100
// 创建画布和画图设备
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D draw = img.createGraphics();
draw.setBackground(Color.WHITE); // 设置背景色
draw.clearRect(0, 0, width, height); // 清空原始内容
draw.setColor(Color.BLACK); // 设置前景色
int posOff = 2; // 设置偏移量,避免输出点重叠
// 设置内容编码
byte[] codeContent = this.content.getBytes("utf-8");
// 生成二维数组,500是内容大小,根据自己的内容大小进行设定
if (codeContent.length > 0 && codeContent.length < 500) {
boolean[][] qrcodeOut = qrcode.calQrcode(codeContent);
// 将内容写入到图片中
for (int i = 0; i < qrcodeOut.length; i++) {
for (int j = 0; j < qrcodeOut.length; j++) {
// 如果当前位置有像素点
if (qrcodeOut[j][i]){
// 写入图片
draw.fillRect(j * 16 + posOff, i * 16 + posOff, 16, 16);
}
}
}
}
draw.dispose(); // 关闭画图设备
img.flush(); // 刷新缓冲区
File file = new File(imgPath);
ImageIO.write(img, "png", file); // 保存图片
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
}
/**
* @Copyright : www.codecoord.com Inc. All rights reserved.
* @Author : 田鑫
* @CreateDate : 2018/9/24 10:32
* @Version : V1.0
* @Description: 主方法
*/
public class QrCodeGenerator {
public static void main(String[] args) {
String imgPath = "E:/code.png"; // 二维码保存路径
// 跳转的内容,如果http://则当做文字处理
String content = "http://www.baidu.com";
// 创建封装类
QrCodeAssis assis = new QrCodeAssis(imgPath, content);
boolean result = assis.createCode(480, 480);
if (result) {
System.out.println("二维码生成成功!");
} else {
System.out.println("二维码生成失败!");
}
}
}