完成二维码生成代码
This commit is contained in:
parent
7bd3fe22ca
commit
4096947015
@ -0,0 +1,234 @@
|
|||||||
|
package com.xhpc.charging.station.utils.img;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author WH
|
||||||
|
* @Date 2021/9/10 17:44
|
||||||
|
* @Description 图片工具拼接工具类
|
||||||
|
* @Param
|
||||||
|
* @Return
|
||||||
|
* @Since version-1.0
|
||||||
|
*/
|
||||||
|
public class ImageUtil {
|
||||||
|
|
||||||
|
private BufferedImage tempImg = null;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
ImageUtil img = new ImageUtil();
|
||||||
|
img.createXhImg();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean createXhImg() throws IOException {
|
||||||
|
|
||||||
|
tempImg = this.createBottom(new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\BottomImg.png"));
|
||||||
|
tempImg = this.addQrCodeToBottom(tempImg);
|
||||||
|
tempImg = this.addBottomLogo(tempImg);
|
||||||
|
tempImg = this.writeSerialNumber(tempImg);
|
||||||
|
tempImg = this.addHeader(tempImg);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param source 合成了二维码、底部Logo、终端编号、头部的最终图
|
||||||
|
* @return 添加了顶部文字的最终图片
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private BufferedImage addHeader(BufferedImage source) throws IOException {
|
||||||
|
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
// //顶部占位矩形
|
||||||
|
// g2.fillRect(0,0,400,190);
|
||||||
|
|
||||||
|
//写入桩部分
|
||||||
|
writePile(source);
|
||||||
|
//写入枪部分
|
||||||
|
writeGun(source);
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha8.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 顶部文字右边部分
|
||||||
|
* @param source 合成了二维码、底部Logo、终端编号、枪部分部分的底图
|
||||||
|
* @return 返回最终合成的图
|
||||||
|
*/
|
||||||
|
private BufferedImage writeGun(BufferedImage source) throws IOException {
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
|
||||||
|
//提前设置文字参数,防止生成的文字带有锯齿
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
g2.setFont(new Font("黑体",Font.BOLD,130));
|
||||||
|
g2.drawString("A",250,180);
|
||||||
|
g2.setFont(new Font("楷体",Font.BOLD,60));
|
||||||
|
g2.drawString("枪",320,174);
|
||||||
|
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha7.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 顶部文字左边部分
|
||||||
|
* @param source
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private BufferedImage writePile(BufferedImage source) throws IOException{
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
|
||||||
|
//提前设置文字参数,防止生成的文字带有锯齿
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
//在头顶设置场站名称:
|
||||||
|
g2.setFont(new Font("楷体",Font.BOLD,24));
|
||||||
|
|
||||||
|
//后期改成居中,不写死
|
||||||
|
g2.drawString("小华充电团结平安村充电站",40,70);
|
||||||
|
|
||||||
|
g2.setFont(new Font("楷体",Font.BOLD,48));
|
||||||
|
g2.drawString("99号桩",25,154);
|
||||||
|
g2.setFont(new Font("楷体",Font.BOLD,18));
|
||||||
|
// 桩号下方的编码:
|
||||||
|
// g2.drawString("69852145895458",34,163);
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha6.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param source 合成了二维码、底部Logo的底图
|
||||||
|
* @return 添加了终端编号的底图
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private BufferedImage writeSerialNumber(BufferedImage source) throws IOException {
|
||||||
|
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
|
||||||
|
//提前设置文字参数,防止生成的文字带有锯齿
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
g2.setFont(new Font("楷体",Font.BOLD,24));
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
g2.drawRect(10,210,380,70);
|
||||||
|
|
||||||
|
|
||||||
|
g2.drawString("终端编码:6985214589545801",27,255);
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha5.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param source 合成了二维码的底图
|
||||||
|
* @return 添加了底部Logo的底图
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private BufferedImage addBottomLogo(BufferedImage source) throws IOException {
|
||||||
|
|
||||||
|
//底部Logo存放位置
|
||||||
|
File QrCodeImgPath = new File("D:\\Enterprise_Resources\\XiaoHuaLogoImg\\BottomLogo.png");
|
||||||
|
|
||||||
|
//使用ImageIO,将logo图片加载到内存中
|
||||||
|
BufferedImage QrCodeImg = ImageIO.read(QrCodeImgPath);
|
||||||
|
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
g2.drawImage(QrCodeImg,10,700,380,80,null);
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha4.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param source 底图
|
||||||
|
* @return 添加了二维码的底图
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private BufferedImage addQrCodeToBottom(BufferedImage source) throws IOException {
|
||||||
|
|
||||||
|
//二维码图片存放位置
|
||||||
|
File QrCodeImgPath = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\2021091011050707669852145895458.png");
|
||||||
|
|
||||||
|
//使用ImageIO,将图片加载到内存中
|
||||||
|
BufferedImage QrCodeImg = ImageIO.read(QrCodeImgPath);
|
||||||
|
|
||||||
|
Graphics2D g2 = source.createGraphics();
|
||||||
|
g2.drawImage(QrCodeImg,10,300,380,380,null);
|
||||||
|
|
||||||
|
//生成的图片的存放路径
|
||||||
|
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha3.png");
|
||||||
|
|
||||||
|
ImageIO.write(source, "png", dest);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建图片底图
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private BufferedImage createBottom(File bottomFile) throws IOException {
|
||||||
|
|
||||||
|
int width = 400; //底图的宽
|
||||||
|
int height = 800; //底图的高
|
||||||
|
|
||||||
|
//设置生成的底图的存放路径
|
||||||
|
File file = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha2.png");
|
||||||
|
|
||||||
|
//生成图片画布
|
||||||
|
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
|
//生成用于该画布的画笔对象
|
||||||
|
Graphics2D g2 = bi.createGraphics();
|
||||||
|
|
||||||
|
//设置画笔颜色为蓝色
|
||||||
|
g2.setColor(Color.BLUE);
|
||||||
|
//填充指定矩形区域的颜色
|
||||||
|
g2.fillRect(0,0,400,800);
|
||||||
|
|
||||||
|
//载入生成的二维码图片
|
||||||
|
BufferedImage bottomImg = ImageIO.read(bottomFile);
|
||||||
|
g2.drawImage(bottomImg,0,0,null);
|
||||||
|
|
||||||
|
//生成底图
|
||||||
|
ImageIO.write(bi, "png", file);
|
||||||
|
|
||||||
|
return bi;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,150 @@
|
|||||||
|
package com.xhpc.charging.station.utils.img;
|
||||||
|
|
||||||
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.EncodeHintType;
|
||||||
|
import com.google.zxing.MultiFormatWriter;
|
||||||
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||||
|
import com.google.zxing.qrcode.encoder.QRCode;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.geom.RoundRectangle2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @ClassName: QRCodeUtil
|
||||||
|
* @Description: 二维码生成工具类
|
||||||
|
* @author w
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class QRCodeUtil {
|
||||||
|
|
||||||
|
private static final String CHARSET = "utf-8";
|
||||||
|
private static final String FORMAT_NAME = "PNG";
|
||||||
|
// 二维码尺寸
|
||||||
|
private static final int QRCODE_SIZE = 400;
|
||||||
|
// LOGO宽度
|
||||||
|
private static final int WIDTH = 100;
|
||||||
|
// LOGO高度
|
||||||
|
private static final int HEIGHT = 100;
|
||||||
|
|
||||||
|
private static BufferedImage createImage(String content, String imgPath,
|
||||||
|
boolean needCompress) throws Exception {
|
||||||
|
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
|
||||||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
||||||
|
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
|
||||||
|
hints.put(EncodeHintType.MARGIN, 1);
|
||||||
|
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
|
||||||
|
BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
|
||||||
|
int width = bitMatrix.getWidth();
|
||||||
|
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
|
||||||
|
: 0xFFFFFFFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (imgPath == null || "".equals(imgPath)) {
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
// 插入图片
|
||||||
|
QRCodeUtil.insertImage(image, imgPath, needCompress);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入LOGO
|
||||||
|
*
|
||||||
|
* @param source
|
||||||
|
* 二维码图片
|
||||||
|
* @param imgPath
|
||||||
|
* LOGO图片地址
|
||||||
|
* @param needCompress
|
||||||
|
* 是否压缩
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static void insertImage(BufferedImage source, String imgPath,
|
||||||
|
boolean needCompress) throws Exception {
|
||||||
|
File file = new File(imgPath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
System.err.println(""+imgPath+" 该文件不存在!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Image src = ImageIO.read(new File(imgPath));
|
||||||
|
int width = src.getWidth(null);
|
||||||
|
int height = src.getHeight(null);
|
||||||
|
if (needCompress) { // 压缩LOGO
|
||||||
|
if (width > WIDTH) {
|
||||||
|
width = WIDTH;
|
||||||
|
}
|
||||||
|
if (height > HEIGHT) {
|
||||||
|
height = HEIGHT;
|
||||||
|
}
|
||||||
|
Image image = src.getScaledInstance(width, height,
|
||||||
|
Image.SCALE_SMOOTH);
|
||||||
|
BufferedImage tag = new BufferedImage(width, height,
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics g = tag.getGraphics();
|
||||||
|
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
|
||||||
|
g.dispose();
|
||||||
|
src = image;
|
||||||
|
}
|
||||||
|
// 插入LOGO
|
||||||
|
Graphics2D graph = source.createGraphics();
|
||||||
|
int x = (QRCODE_SIZE - width) / 2;
|
||||||
|
int y = (QRCODE_SIZE - height) / 2;
|
||||||
|
graph.drawImage(src, x, y, width, height, null);
|
||||||
|
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
|
||||||
|
graph.setStroke(new BasicStroke(3f));
|
||||||
|
graph.draw(shape);
|
||||||
|
graph.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成二维码(内嵌LOGO)
|
||||||
|
*
|
||||||
|
* @param content
|
||||||
|
* 内容
|
||||||
|
* @param imgPath
|
||||||
|
* LOGO地址
|
||||||
|
* @param destPath
|
||||||
|
* 存放目录
|
||||||
|
* @param needCompress
|
||||||
|
* 是否压缩LOGO
|
||||||
|
* @param fileName
|
||||||
|
* 生成的图片名称
|
||||||
|
* @return QrCodeImg
|
||||||
|
* 生成好的二维码图片
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static File encode(String content, String imgPath, String destPath,
|
||||||
|
boolean needCompress,String fileName) throws Exception {
|
||||||
|
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
|
||||||
|
needCompress);
|
||||||
|
mkdirs(destPath);
|
||||||
|
|
||||||
|
//添加变量接收,以获取生成好的二维码图片
|
||||||
|
File QrCodeImg = new File(destPath + "/" + fileName);
|
||||||
|
ImageIO.write(image, FORMAT_NAME, QrCodeImg);
|
||||||
|
|
||||||
|
return QrCodeImg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
|
||||||
|
* @param destPath 存放目录
|
||||||
|
*/
|
||||||
|
public static void mkdirs(String destPath) {
|
||||||
|
File file =new File(destPath);
|
||||||
|
//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
|
||||||
|
if (!file.exists() && !file.isDirectory()) {
|
||||||
|
file.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package com.xhpc.charging.station.utils.img;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.aliyun.oss.OSSClient;
|
||||||
|
import com.xhpc.charging.station.mapper.XhpcImgMapper;
|
||||||
|
import com.xhpc.common.domain.XhpcTerminal;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class QrImgUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author WH
|
||||||
|
* @Date 2021/9/8 15:55
|
||||||
|
* @Description upload images to Server
|
||||||
|
* @Param [xhpcTerminal, chargingPileId, environment, xhpcImgMapper]
|
||||||
|
* @Return void
|
||||||
|
* @Since version-1.0
|
||||||
|
*/
|
||||||
|
public static void uploadImg(XhpcTerminal xhpcTerminal, String serialNumber, Environment environment, XhpcImgMapper xhpcImgMapper) {
|
||||||
|
//生成桩的二维码图片
|
||||||
|
//1.生成二维码链接内容:
|
||||||
|
StringBuilder prefix = new StringBuilder("https://xhpc.scxhua.com?pNum=");
|
||||||
|
String QrContent = prefix.append(xhpcTerminal.getSerialNumber()).toString();
|
||||||
|
|
||||||
|
//2.生成图片:
|
||||||
|
try {
|
||||||
|
//2.1生成指定规则的图片名
|
||||||
|
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + serialNumber + ".png";
|
||||||
|
|
||||||
|
//2.2获取生成的二维码图片
|
||||||
|
File QrCode = QRCodeUtil.encode(QrContent, environment.getProperty("oss.imgPath"), environment.getProperty("oss.destPath"), true, fileName);
|
||||||
|
//2.3拼接成完整图片
|
||||||
|
ImageUtil imageUtil = new ImageUtil();
|
||||||
|
boolean File = imageUtil.createXhImg();
|
||||||
|
|
||||||
|
//3.上传图片至服务器
|
||||||
|
// 创建OSSClient实例
|
||||||
|
OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
|
||||||
|
// 上传文件流
|
||||||
|
ossClient.putObject(environment.getProperty("oss.bucket-name"), xhpcTerminal.getPileSerialNumber() + "/" + fileName, new File(environment.getProperty("oss.destPath") + "\\" + fileName));
|
||||||
|
ossClient.shutdown();
|
||||||
|
|
||||||
|
//4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
|
||||||
|
Long terminalId = xhpcTerminal.getTerminalId();
|
||||||
|
xhpcImgMapper.insert(fileName, terminalId);
|
||||||
|
|
||||||
|
//4.删除生成的二维码图片
|
||||||
|
// File QrImg = new File(environment.getProperty("oss.destPath") + "\\" + fileName);
|
||||||
|
// if (QrImg.exists()) {
|
||||||
|
// QrImg.delete();
|
||||||
|
// } else {
|
||||||
|
// throw new RuntimeException("名字为" + fileName + "的二维码不存在");
|
||||||
|
// }
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user