代码是生产上一直在用的,里面有main方法,方便大家自己测试;对于参数设定,可根据自身需要进行调整。
pom文件
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
package com.image;
/**
* Created by Liuxd on 2018-09-07.
*/
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* 二维码
*/
public class GenerateQrCode {
private static final int QRCOLOR = 0xFF000000; //默认是黑色
private static final int BGWHITE = 0xFFFFFFFF; //背景颜色
public static void main(String[] args) {
byte[] bytes = GenerateQrCode.getLogoQRCode("https://blog.csdn.net/jiahao1186", "博客");
//生成一个图片对象。
byte2image(bytes, "D:/1.jpg");
}
public static void byte2image(byte[] data, String path) {
if (data.length < 3 || path.equals("")) return;
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("图片已生成,请按如下地址,查找图片:" + path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
/**
* 生成带logo的二维码图片
*/
public static byte[] getLogoQRCode(String qrUrl, String License) {
String content = qrUrl;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FontImage.getImage(License, 65, 65, bos);
GenerateQrCode zp = new GenerateQrCode();
BufferedImage bim = zp.getQR_CODEBufferedImage(content, BarcodeFormat.QR_CODE, 258, 258, zp.getDecodeHintType());
return zp.addLogo_QRCode(bim, bos.toByteArray(), new LogoConfig());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 给二维码图片添加Logo
*
* @param logoPic
*/
public byte[] addLogo_QRCode(BufferedImage bim, byte[] logoPic, LogoConfig logoConfig) {
try {
/**
* 读取二维码图片,并构建绘图对象
*/
BufferedImage image = bim;
Graphics2D g = image.createGraphics();
/**
* 读取Logo图片
*/
ByteArrayInputStream in = new ByteArrayInputStream(logoPic);
BufferedImage logo = ImageIO.read(in);
/**
* 设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码
*/
int widthLogo = logo.getWidth(null) > image.getWidth() * 3 / 10 ? (image.getWidth() * 3 / 10) : logo.getWidth(null);
int heightLogo = logo.getHeight(null) > image.getHeight() * 3 / 10 ? (image.getHeight() * 3 / 10) : logo.getWidth(null);
/**
* logo放在中心
*/
int x = (image.getWidth() - widthLogo) / 2;
int y = (image.getHeight() - heightLogo) / 2;
/**
* logo放在右下角
* int x = (image.getWidth() - widthLogo);
* int y = (image.getHeight() - heightLogo);
*/
//开始绘制图片
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.dispose();
logo.flush();
image.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flush();
ImageIO.write(image, "png", baos);
//二维码生成的路径,但是实际项目中,我们是把这生成的二维码显示到界面上的,因此下面的折行代码可以注释掉
//可以看到这个方法最终返回的是这个二维码的imageBase64字符串
//前端用 <img src="data:image/png;base64,${imageBase64QRCode}"/> 其中${imageBase64QRCode}对应二维码的imageBase64字符串
// ImageIO.write(image, "png", new File("/Users/zhanglei/Downloads/" + new Date().getTime() + "test.png")); //TODO
//
// String imageBase64QRCode = Base64.encodeBase64URLSafeString(baos.toByteArray());
// baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 构建初始化二维码
*
* @param bm
* @return
*/
public BufferedImage fileToBufferedImage(BitMatrix bm) {
BufferedImage image = null;
try {
int w = bm.getWidth(), h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ? 0xFF000000 : 0xFFCCDDEE);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
/**
* 生成二维码bufferedImage图片
*
* @param content 编码内容
* @param barcodeFormat 编码类型
* @param width 图片宽度
* @param height 图片高度
* @param hints 设置参数
* @return
*/
public BufferedImage getQR_CODEBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height, Map<EncodeHintType, ?> hints) {
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try {
multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return image;
}
/**
* 设置二维码的格式参数
*
* @return
*/
public Map<EncodeHintType, Object> getDecodeHintType() {
// 用于设置QR二维码参数
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.MAX_SIZE, 350);
hints.put(EncodeHintType.MIN_SIZE, 100);
return hints;
}
}
package com.image;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
/**
* 创建文字图片
*
*/
public class FontImage {
// 默认格式
private static final String FORMAT_NAME = "JPG";
// 默认 宽度
private static final int WIDTH = 100;
// 默认 高度
private static final int HEIGHT = 100;
/**
* 创建图片
*
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @return
*/
private static BufferedImage createImage(String content, Font font, Integer width, Integer height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(content, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(content, (int) x, (int) baseY);
return bi;
}
/**
* 获取 图片
*
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @return
*/
public static BufferedImage getImage(String content, Font font, Integer width, Integer height) {
width = width == null ? WIDTH : width;
height = height == null ? HEIGHT : height;
if (null == font)
font = new Font("宋体", Font.BOLD, 13);
return createImage(content, font, width, height);
}
/**
* 获取 图片
*
* @param content 内容
* @param width 宽
* @param height 高
* @return
*/
public static BufferedImage getImage(String content, Integer width, Integer height) {
return getImage(content, null, width, height);
}
/**
* 获取图片
*
* @param content 内容
* @return
*/
public static BufferedImage getImage(String content) {
return getImage(content, null, null);
}
/**
* 获取图片
*
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @param destPath 输出路径
* @throws IOException
*/
public static void getImage(String content, Font font, Integer width, Integer height, String destPath) throws IOException {
mkdirs(destPath);
String file = UUID.randomUUID().toString() + ".jpg";
ImageIO.write(getImage(content, font, width, height), FORMAT_NAME, new File(destPath + "/" + file));
}
/**
* 获取图片
*
* @param content 内容
* @param font 字体
* @param width 宽
* @param height 高
* @param output 输出流
* @throws IOException
*/
public static void getImage(String content, Font font, Integer width, Integer height, OutputStream output) throws IOException {
ImageIO.write(getImage(content, font, width, height), FORMAT_NAME, output);
}
/**
* 获取图片
*
* @param content 内容
* @param width 宽
* @param height 高
* @param destPath 输出路径
* @throws IOException
*/
public static void getImage(String content, Integer width, Integer height, String destPath) throws IOException {
getImage(content, null, width, height, destPath);
}
/**
* 获取图片
*
* @param content 内容
* @param width 宽
* @param height 高
* @param output 输出流
* @throws IOException
*/
public static void getImage(String content, Integer width, Integer height, OutputStream output) throws IOException {
getImage(content, null, width, height, output);
}
/**
* 创建 目录
*
* @param destPath
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
public static void main(String[] args) throws Exception {
getImage("川A***V", 100, 100, "D:/");
}
}
package com.image;
import java.awt.*;
/**
* Created by Liuxd on 2018-09-07.
*/
public class LogoConfig {
// logo默认边框颜色
public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;
// 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;
/**
* Creates a default config with on color and off color
* generating normal black-on-white barcodes.
*/
public LogoConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
public LogoConfig(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;
}
}