最近研究了Zxing的源码,以及它的一些常用的用法,感触也是很深,也提高了不少,网上也找了各种写法,要真正的理解这一整套东西还是要花费些经历的,涉及的知识面还是有一些的,下面就上一下我简单封装的源码。以方便大家做参考,封装的不完善,只是简单的写了个,大家可以自己进行封装.....
1. 下载Zxing所需要的依赖包:core-2.2.jar、javase-2.2.jar ,因为只使用到了javase-2.2 Jar包的部分内容,可以将javase进行简化下
<dependency>
<groupId>core</groupId>
<artifactId>core-2.2</artifactId>
<version>2.2</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/core-2.2.jar
</systemPath>
</dependency>
<!-- <dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency> -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>1.7</version>
</dependency>
MatrixToImageWriterEx.java : 生成二维码logo扩展类,此类是在zxing的基础上进行扩展的,用于在二维码上定制自己的logo
package com.yanshu.wechat;
import java.awt.BasicStroke;
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.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 生成二维码的工具类
* @author Administrator
*
*/
public class MatrixToImageWriterEx {
private static final MatrixToLogoImageConfig DEFAULT_CONFIG = new MatrixToLogoImageConfig();
/**
* 把生成的二位码直接加载到页面上
* @param contents
* @param width
* @param height
* @param os
*/
public static void generateQR(String contents, int width, int height, OutputStream os) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把二维码生成电脑的那个盘中
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void generateQR(String contents, int width, int height, String imgPath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
File file=new File("e://a.png");
MatrixToImageWriter.writeToFile(bitMatrix, "png", file);
//bitMatrix.
//writeToFile(bitMatrix, "png", new File(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据内容生成二维码数据
* @param content 二维码文字内容[为了信息安全性,一般都要先进行数据加密]
* @param width 二维码照片宽度
* @param height 二维码照片高度
* @return
*/
public static BitMatrix createQRCode(String content,int width,int height)
{
Hashtable<EncodeHintType, Object> hints=new Hashtable<>();
//设置字符编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix=null;
try {
bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
} catch (WriterException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return bitMatrix;
}
/**
* 写入二维码、以及将照片logo写入二维码中
* @param matrix 要写入的二维码
* @param format 二维码照片格式
* @param imagePath 二维码照片保存路径
* @param logoPath logo路径
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix,String format,String imagePath,String logoPath) throws IOException
{
// MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath), new MatrixToImageConfig());
MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath));
//添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
BufferedImage image=ImageIO.read(new File(imagePath));
MatrixToImageWriterEx.overlapImage(image, format, imagePath, logoPath, DEFAULT_CONFIG);
}
/**
* 写入二维码、以及将照片logo写入二维码中
* @param matrix 要写入的二维码
* @param format 二维码照片格式
* @param imagePath 二维码照片保存路径
* @param logoPath logo路径
* @param logoConfig logo配置对象
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath, MatrixToLogoImageConfig logoConfig) throws IOException
{
MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath));
//添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
BufferedImage img = ImageIO.read(new File(imagePath));
MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, logoConfig);
}
/**
* 将照片logo添加到二维码中间
* @param image 生成的二维码照片对象
* @param imagePath 照片保存路径
* @param logoPath logo照片路径
* @param formate 照片格式
*/
public static void overlapImage(BufferedImage image,String formate,String imagePath,String logoPath,MatrixToLogoImageConfig logoImageConfig)
{
try {
BufferedImage logo=ImageIO.read(new File(logoPath));
Graphics2D g=image.createGraphics();
//考虑到logo照片粘贴到二位码中,建议大小不要超过二维码的1/5;
int width=image.getWidth()/logoImageConfig.getLogoPart();
int height=image.getHeight()/logoImageConfig.getLogoPart();
//logo起始位置,此目前是为logo居中显示
int x=(image.getWidth()-width)/2;
int y=(image.getHeight()-height)/2;
//绘制图
g.drawImage(logo, x, y, width,height,null);
//给logo画边框
//构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke
g.setStroke(new BasicStroke(logoImageConfig.getBorder()));
g.setColor(logoImageConfig.getBorderColor());
g.drawRect(x, y, width, height);
g.dispose();
//写入logo照片到二维码
ImageIO.write(image, formate, new File(imagePath));
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
int width = 200;
int height = 200;
String content = "http://www.taobao.com";
BitMatrix matrix = MatrixToImageWriterEx.createQRCode(content, width, height);
MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4);
MatrixToImageWriterEx.writeToFile(matrix, "jpg", "f:/imgQRCode.jpg", "f:/logo.jpg", logoConfig);
System.out.println("生成二维码结束!");
} catch (Exception e) {
e.printStackTrace();
}
generateQR("weixin://wxpay/bizpayurl?pr=Nv4NVb8", 500, 500, "ss");
}
}
MatrixToLogoImageConfig.java : 定制logo属性类
package com.yanshu.wechat;
import java.awt.Color;
/**
* 定义logo属性
* @author Administrator
*
*/
public class MatrixToLogoImageConfig {
//logo默认边框颜色
public static final Color DEFAULT_BORDERCOLOR = Color.RED;
//logo默认边框宽度
public static final int DEFAULT_BORDER = 2;
//logo大小默认为照片的1/5
public static final int DEFAULT_LOGOPART = 5;
private final int border=DEFAULT_BORDER;
private final Color borderColor;
private final int logoPart;
public MatrixToLogoImageConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
public MatrixToLogoImageConfig(Color borderColor, int logoPart) {
this.borderColor = borderColor;
this.logoPart = logoPart;
}
public Color getBorderColor() {
return borderColor;
}
public int getBorder() {
return border;
}
public int getLogoPart() {
return logoPart;
}
}