主要是使用google的zxing 生成二维码,可设置logo、二维码前景色/后景色、白边大小、二维码大小
1、用到jar包
<dependency>
<groupId></groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
2、生成二维码参数类
/**
* @copyright @2015-2016 Rights Reserved
*/
package ;
import ;
/**
* @description
* @author
* @date 2016年7月10日 下午4:41:41
*/
public class QRCodeParams {
private String txt; //二维码内容
private String qrCodeUrl; //二维码网络路径
private String filePath; //二维码生成物理路径
private String fileName; //二维码生成图片名称(包含后缀名)
private String logoPath; //logo图片
private Integer width = 300; //二维码宽度
private Integer height = 300; //二维码高度
private Integer onColor = 0xFF000000; //前景色
private Integer offColor = 0xFFFFFFFF; //背景色
private Integer margin = 1; //白边大小,取值范围0~4
private ErrorCorrectionLevel level = ; //二维码容错率
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
= txt;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
= filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
= fileName;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
= width;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
= height;
}
public String getQrCodeUrl() {
return qrCodeUrl;
}
public void setQrCodeUrl(String qrCodeUrl) {
= qrCodeUrl;
}
public String getLogoPath() {
return logoPath;
}
public void setLogoPath(String logoPath) {
= logoPath;
}
public Integer getOnColor() {
return onColor;
}
public void setOnColor(Integer onColor) {
= onColor;
}
public Integer getOffColor() {
return offColor;
}
public void setOffColor(Integer offColor) {
= offColor;
}
public ErrorCorrectionLevel getLevel() {
return level;
}
public void setLevel(ErrorCorrectionLevel level) {
= level;
}
/**
* 返回文件后缀名
* @return
*/
public String getSuffixName(){
String imgName = ();
if(imgName != null && !"".equals(imgName)){
String suffix=((".")+1);
return suffix;
}
return "";
}
public Integer getMargin() {
return margin;
}
public void setMargin(Integer margin) {
= margin;
}
}
3、生成二维码工具类
/**
* @copyright @2015-2016 Rights Reserved
*/
package ;
import ;
import .Graphics2D;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import .;
import ;
import ;
/**
* @description
* @author
* @date 2016年7月10日 下午4:41:41
*/
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 = ; //二维码容错率
/**
* 生成二维码
* @param params
* QRCodeParams属性:txt、fileName、filePath不得为空;
* @throws QRParamsException
*/
public static void generateQRImage(QRCodeParams params)throws QRParamsException{
if(params == null
|| () == null
|| () == null
|| () == null){
throw new QRParamsException("参数错误");
}
try{
initData(params);
String imgPath = ();
String imgName = ();
String txt = ();
if(() != null && !"".equals(().trim())){
generateQRImage(txt, (), imgPath, imgName, ());
}else{
generateQRImage(txt, imgPath, imgName, ());
}
}catch(Exception e){
();
}
}
/**
* 生成二维码
* @param txt //二维码内容
* @param imgPath //二维码保存物理路径
* @param imgName //二维码文件名称
* @param suffix //图片后缀名
*/
public static void generateQRImage(String txt, String imgPath, String imgName, String suffix){
File filePath = new File(imgPath);
if(!()){
();
}
File imageFile = new File(imgPath,imgName);
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
(EncodeHintType.ERROR_CORRECTION, level);
// 指定编码格式
(EncodeHintType.CHARACTER_SET, "UTF-8");
(, margin); //设置白边
try {
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter().encode(txt,BarcodeFormat.QR_CODE, width, height, hints);
// bitMatrix = deleteWhite(bitMatrix);
(bitMatrix, suffix, (), config);
} catch (Exception e) {
();
}
}
/**
* 生成带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 imgName, String suffix) throws Exception{
File filePath = new File(imgPath);
if(!()){
();
}
if(("/")){
imgPath += imgName;
}else{
imgPath += "/"+imgName;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
(EncodeHintType.CHARACTER_SET, "utf-8");
(EncodeHintType.ERROR_CORRECTION, level);
(, 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 = ();
int ratioWidth = ()*2/10;
int ratioHeight = ()*2/10;
//载入logo
Image img = (new File(logoPath));
int logoWidth = (null)>ratioWidth?ratioWidth:(null);
int logoHeight = (null)>ratioHeight?ratioHeight:(null);
int x = (() - logoWidth) / 2;
int y = (() - logoHeight) / 2;
(img, x, y, logoWidth, logoHeight, null);
();
();
();
();
if(!(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = ();
int height = ();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
(x, y, (x, y) ? onColor : offColor);
}
}
return image;
}
public static BitMatrix deleteWhite(BitMatrix matrix){
int[] rec = ();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if ((i + rec[0], j + rec[1]))
(i, j);
}
}
return resMatrix;
}
private static void initData(QRCodeParams params){
if(() != null){
width = ();
}
if(() != null){
height = ();
}
if(() != null){
onColor = ();
}
if(() != null){
offColor = ();
}
if(() != null){
level = ();
}
}
}
4、自定义异常类
/**
* @copyright @2015-2016 Rights Reserved
*/
package ;
/**
* @description
* @author
* @date 2016年7月10日 下午4:41:41
*/
public class QRParamsException extends Exception {
private static final long serialVersionUID = 8837582301762730656L;
public QRParamsException() {} //用来创建无参数对象
public QRParamsException(String message) { //用来创建指定参数对象
super(message); //调用超类构造器
}
}
项目源码下载:/detail/rongku/9572265