1.完成添加桩生成桩所对应的终端时,生成其二维码图片,上传到阿里云,然后删除生成的二维码图片的功能

2.将该功能抽取为工具类QrImgUtils.uploadImg,参数在QrImgUtils.properties配置文件中
This commit is contained in:
wen 2021-09-08 17:31:32 +08:00
parent 88ff6b37a1
commit ce71a862e2
9 changed files with 383 additions and 7 deletions

View File

@ -73,10 +73,10 @@ public class SysFileController
}
}
/*
* TODO 阿里云图片上传
* @author fjd
* @date 2020-07-03 16:13
/**
* 阿里云图片上传
* @author
* @date 2021-09-01 17:15
*/
@PostMapping("photoupload")
@ApiOperation(value = "上传", notes = "上传")

View File

@ -30,6 +30,21 @@ public class XhpcImg extends BaseEntity {
*/
private Integer delFlag;
/**
* 生成的图片所对应的终端的终端号
*/
private Long serialNumber;
public Long getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(Long serialNumber) {
this.serialNumber = serialNumber;
}
public Long getImgId() {
return imgId;
}

View File

@ -22,6 +22,26 @@
<dependencies>
<!--单元测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--二维码生成工具类依赖-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
@ -102,6 +122,13 @@
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>

View File

@ -0,0 +1,22 @@
package com.xhpc.charging.station.mapper;
import org.apache.ibatis.annotations.Param;
/**
* 上传图片信息
*
* @author
*/
public interface XhpcImgMapper {
/**
* 新增 上传图片
*
*
* @param imgUrl 图片的url
* @param terminalId 图片所对应的终端的id
* @return
*/
void insert(@Param("imgUrl") String imgUrl,@Param("terminalId") Long terminalId);
}

View File

@ -5,13 +5,16 @@ import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import com.xhpc.charging.station.mapper.XhpcChargingPileMapper;
import com.xhpc.charging.station.mapper.XhpcImgMapper;
import com.xhpc.charging.station.mapper.XhpcTerminalMapper;
import com.xhpc.charging.station.utils.QrImgUtils;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -31,6 +34,14 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
@Autowired
private PowerPileService powerPileService;
//上传图片全局变量
@Autowired
private Environment environment;
//上传图片Mapper
@Autowired
private XhpcImgMapper xhpcImgMapper;
/**
* 桩列表
@ -82,13 +93,63 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
xhpcTerminal.setStatus(0);
xhpcTerminal.setRateModelId(rateModelId);
xhpcTerminalMapper.addXhpcTerminal(xhpcTerminal);
//生成二维码图片并上传
QrImgUtils.uploadImg(xhpcTerminal,String.valueOf(chargingPileId),environment,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") + chargingPileId+".png"; //2021090814171100766.png
//
// //2.2生成图片
// //2.1.1设置生成的图片二维码的存放位置
// String destPath = "C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电二维码";
// //2.1.2获取Logo图片位置
// String imgPath = "C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电logo.png";
//
// QRCodeUtil.encode(QrContent,
// imgPath,
// destPath,
// true,
// fileName
// );
//
// //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"), serialNumber+"/"+fileName, new File("C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电二维码"+"\\"+fileName));
// ossClient.shutdown();
//
// //4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
// Long terminalId = xhpcTerminal.getTerminalId();
// xhpcImgMapper.insert(fileName,terminalId);
//
// //4.删除生成的二维码图片
// File QrImg = new File(destPath +"\\"+ fileName);
// if(QrImg.exists()){
// QrImg.delete();
// }else {
// throw new RuntimeException("名字为" + fileName + "的二维码不存在");
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println(e.getMessage());
// }
}
}
//插入redis 桩的编号
HashSet<String> noSet = new HashSet<>();
noSet.add(serialNumber);
R r = powerPileService.addPileWhitelist(chargingStationId, noSet);
// HashSet<String> noSet = new HashSet<>();
// noSet.add(serialNumber);
// R r = powerPileService.addPileWhitelist(chargingStationId, noSet);
return AjaxResult.success();
}

View File

@ -0,0 +1,144 @@
package com.xhpc.charging.station.utils;
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 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
* 生成的图片名称
* @throws Exception
*/
public static void encode(String content, String imgPath, String destPath,
boolean needCompress,String fileName) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
mkdirs(destPath);
// String file = new Random().nextInt(99999999)+".png";
// String file = fileName+".png";
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+fileName));
}
/**
* 当文件夹不存在时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();
}
}
}

View File

@ -0,0 +1,92 @@
package com.xhpc.charging.station.utils;
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.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
public class QrImgUtils {
//生成的二维码存放位置
private static String destPath;
//获取Logo图片位置
private static String imgPath;
//进行工具初始化
static {
InputStream is = QrImgUtils.class.getClassLoader().getResourceAsStream("QrImgUtils.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
System.out.println("QrImgUtils无法加载配置文件");
}
destPath = properties.getProperty("destPath");
imgPath = properties.getProperty("imgPath");
}
/**
* @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 chargingPileId, 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") + chargingPileId + ".png"; //2021090814171100766.png
//2.2生成图片
QRCodeUtil.encode(QrContent,
imgPath,
destPath,
true,
fileName
);
//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(destPath + "\\" + fileName));
ossClient.shutdown();
//4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
Long terminalId = xhpcTerminal.getTerminalId();
xhpcImgMapper.insert(fileName, terminalId);
//4.删除生成的二维码图片
File QrImg = new File(destPath + "\\" + fileName);
if (QrImg.exists()) {
QrImg.delete();
} else {
throw new RuntimeException("名字为" + fileName + "的二维码不存在");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}

View File

@ -0,0 +1,4 @@
#生成的二维码存放位置:
destPath=D:\\Enterprise_Resources\\XiaoHuaQrImgs
#Logo图片获取位置
imgPath=D:\\Enterprise_Resources\\XiaoHuaLogoImg\\xhcd_logo.png

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhpc.charging.station.mapper.XhpcImgMapper">
<insert id="insert">
INSERT INTO xhpc_img(url,terminal_id) VALUES(#{imgUrl},#{terminalId})
</insert>
</mapper>