一、背景
1.今天做了一个二维码的生成以及简单的跳转应用,结下来我们来演示下吧。
二、项目结构
1.项目图片
三、项目详解
1.pom.xml
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
2.QRCodeUtil
public class QRCodeUtil {
private static int width = 300; //二维码宽度
private static int height = 300; //二维码高度
private static int onColor = 0xFF000000; //前景色
private static int offColor = 0xFFFFFFFF; //背景色
private static int margin = 1; //白边大小,取值范围0~4
private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L; //二维码容错率
/**
* 生成二维码
* @param params
* QRCodeParams属性:txt、fileName、filePath不得为空;
*/
public static void generateQRImage(QRCodeParams params)throws QRParamsException{
if(params == null
|| params.getFilePath() == null
|| params.getTxt() == null){
throw new QRParamsException("参数错误");
}
try{
initData(params);
String imgPath = params.getFilePath();
String txt = params.getTxt();
if(params.getLogoPath() != null && !"".equals(params.getLogoPath().trim())){
generateQRImage(txt, params.getLogoPath(), imgPath, params.getSuffixName());
}else{
generateQRImage(txt, imgPath, params.getSuffixName());
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 生成二维码
* @param txt //二维码内容
* @param imgPath //二维码保存物理路径
* @param imgName //二维码文件名称
* @param suffix //图片后缀名
*/
public static void generateQRImage(String txt, String imgPath, String suffix){
File filePath = new File(imgPath);
if(!filePath.getParentFile().exists()){
filePath.getParentFile().mkdirs();
}
File imageFile = new File(imgPath);
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, level);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, margin); //设置白边
try {
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter().encode(txt,BarcodeFormat.QR_CODE, width, height, hints);
// bitMatrix = deleteWhite(bitMatrix);
MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath(), config);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成带logo的二维码图片
* @param txt //二维码内容
* @param logoPath //logo绝对物理路径
* @param imgPath //二维码保存绝对物理路径
* @param imgName //二维码文件名称
* @param suffix //图片后缀名
* @throws Exception
*/
public static void generateQRImage(String txt, String logoPath, String imgPath, String suffix) throws Exception{
File filePath = new File(imgPath);
if(!filePath.getParentFile().exists()){
filePath.getParentFile().mkdirs();
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, margin); //设置白边
BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints);
File qrcodeFile = new File(imgPath);
writeToFile(bitMatrix, suffix, qrcodeFile, logoPath);
}
/**
*
* @param matrix 二维码矩阵相关
* @param format 二维码图片格式
* @param file 二维码图片文件
* @param logoPath logo路径
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
Graphics2D gs = image.createGraphics();
int ratioWidth = image.getWidth()*2/10;
int ratioHeight = image.getHeight()*2/10;
//载入logo
Image img = ImageIO.read(new File(logoPath));
int logoWidth = img.getWidth(null)>ratioWidth?ratioWidth:img.getWidth(null);
int logoHeight = img.getHeight(null)>ratioHeight?ratioHeight:img.getHeight(null);
int x = (image.getWidth() - logoWidth) / 2;
int y = (image.getHeight() - logoHeight) / 2;
gs.drawImage(img, x, y, logoWidth, logoHeight, null);
gs.setColor(Color.black);
gs.setBackground(Color.WHITE);
gs.dispose();
img.flush();
if(!ImageIO.write(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
}
}
return image;
}
public static BitMatrix deleteWhite(BitMatrix matrix){
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
private static void initData(QRCodeParams params){
if(params.getWidth() != null){
width = params.getWidth();
}
if(params.getHeight() != null){
height = params.getHeight();
}
if(params.getOnColor() != null){
onColor = params.getOnColor();
}
if(params.getOffColor() != null){
offColor = params.getOffColor();
}
if(params.getLevel() != null){
level = params.getLevel();
}
}
}
3.QRCodeParams
public class QRCodeParams {
/**
* 二维码内容
*/
private String txt;
/**
* 二维码生成的路径,与fileName加起来为全路径
*/
private String filePath;
/**
* logo图片
*/
private String logoPath;
/**
* 二维码宽度
*/
private Integer width = 300;
/**
* 二维码高度
*/
private Integer height = 300;
/**
* 前景色
*/
private Integer onColor = 0xFF000000;
/**
* 背景色
*/
private Integer offColor = 0xFFFFFFFF;
/**
* 白边大小,取值范围0~4
*/
private Integer margin = 1;
/**
* 二维码容错率 ErrorCorrectionLevel枚举类型
*/
private ErrorCorrectionLevel level = ErrorCorrectionLevel.L;
/**
* 二维码内容
*
* @return
*/
public String getTxt() {
return txt;
}
/**
* 二维码内容
*
* @param txt
*/
public void setTxt(String txt) {
this.txt = txt;
}
/**
* 二维码生成的路径,与fileName加起来为全路径
*
* @return
*/
public String getFilePath() {
return filePath;
}
/**
* 二维码生成的路径,与fileName加起来为全路径
*
* @param filePath
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
}
/**
* 二维码宽度
*
* @return
*/
public Integer getWidth() {
return width;
}
/**
* 二维码宽度
*
* @param width
*/
public void setWidth(Integer width) {
this.width = width;
}
/**
* 二维码高度
*
* @return
*/
public Integer getHeight() {
return height;
}
/**
* 二维码高度
*
* @param height
*/
public void setHeight(Integer height) {
this.height = height;
}
/**
* logo图片
*
* @return
*/
public String getLogoPath() {
return logoPath;
}
/**
* logo图片
*
* @param logoPath
*/
public void setLogoPath(String logoPath) {
this.logoPath = logoPath;
}
/**
* 前景色
*
* @return
*/
public Integer getOnColor() {
return onColor;
}
/**
* 前景色
*
* @param onColor
*/
public void setOnColor(Integer onColor) {
this.onColor = onColor;
}
/**
* 背景色
*
* @return
*/
public Integer getOffColor() {
return offColor;
}
/**
* 背景色
*
* @param offColor
*/
public void setOffColor(Integer offColor) {
this.offColor = offColor;
}
/**
* 二维码容错率 ErrorCorrectionLevel枚举类型
*
* @return
*/
public ErrorCorrectionLevel getLevel() {
return level;
}
/**
* 二维码容错率 ErrorCorrectionLevel枚举类型
*
* @param level
*/
public void setLevel(ErrorCorrectionLevel level) {
this.level = level;
}
/**
* 白边大小,取值范围0~4
*
* @return
*/
public Integer getMargin() {
return margin;
}
/**
* 白边大小,取值范围0~4
*
* @param margin
*/
public void setMargin(Integer margin) {
this.margin = margin;
}
/**
* 返回文件后缀名
*
* @return
*/
public String getSuffixName() {
String imgName = this.getFilePath();
if (imgName != null && !"".equals(imgName)) {
String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
return suffix;
}
return "";
}
}
4.QRParamsException
public class QRParamsException extends Exception {
private static final long serialVersionUID = 8837582301762730656L;
public QRParamsException() {
} // 用来创建无参数对象
public QRParamsException(String message) { // 用来创建指定参数对象
super(message); // 调用超类构造器
}
}
5.testdemo1
public class testdemo1 {
public static void main(String[] args) throws QRParamsException {
QRCodeParams codeParams = new QRCodeParams();
codeParams.setFilePath("d://test//out.jpg");
codeParams.setLogoPath("E:\\test.jpg");
codeParams.setTxt("https://www.csdn.net");
codeParams.setHeight(500);
codeParams.setWidth(500);
QRCodeUtil.generateQRImage(codeParams);
}
}
四、效果
五、结束
1.这样就完成了二维码的制作和跳转了,共勉