From ce71a862e21a9424ba5af30552182ec3a6de1e99 Mon Sep 17 00:00:00 2001 From: wen <1455474577@qq.com> Date: Wed, 8 Sep 2021 17:31:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=8C=E6=88=90=E6=B7=BB=E5=8A=A0=E6=A1=A9?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A1=A9=E6=89=80=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E7=BB=88=E7=AB=AF=E6=97=B6=EF=BC=8C=E7=94=9F=E6=88=90=E5=85=B6?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E5=9B=BE=E7=89=87=EF=BC=8C=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=88=B0=E9=98=BF=E9=87=8C=E4=BA=91=EF=BC=8C=E7=84=B6?= =?UTF-8?q?=E5=90=8E=E5=88=A0=E9=99=A4=E7=94=9F=E6=88=90=E7=9A=84=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E7=A0=81=E5=9B=BE=E7=89=87=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=202.=E5=B0=86=E8=AF=A5=E5=8A=9F=E8=83=BD=E6=8A=BD=E5=8F=96?= =?UTF-8?q?=E4=B8=BA=E5=B7=A5=E5=85=B7=E7=B1=BBQrImgUtils.uploadImg,?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=9C=A8QrImgUtils.properties=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/controller/SysFileController.java | 8 +- .../java/com/xhpc/file/domain/XhpcImg.java | 15 ++ xhpc-modules/xhpc-charging-station/pom.xml | 27 ++++ .../station/mapper/XhpcImgMapper.java | 22 +++ .../service/XhpcChargingPileServiceImpl.java | 67 +++++++- .../charging/station/utils/QRCodeUtil.java | 144 ++++++++++++++++++ .../charging/station/utils/QrImgUtils.java | 92 +++++++++++ .../src/main/resources/QrImgUtils.properties | 4 + .../main/resources/mapper/XhpcImgMapper.xml | 11 ++ 9 files changed, 383 insertions(+), 7 deletions(-) create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties create mode 100644 xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java index 6b6306dc..249195ef 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java @@ -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 = "上传") diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java index 5f2710f2..cc33092f 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java @@ -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; } diff --git a/xhpc-modules/xhpc-charging-station/pom.xml b/xhpc-modules/xhpc-charging-station/pom.xml index 4cba97da..3976e61e 100644 --- a/xhpc-modules/xhpc-charging-station/pom.xml +++ b/xhpc-modules/xhpc-charging-station/pom.xml @@ -22,6 +22,26 @@ + + + junit + junit + 4.12 + + + + + com.google.zxing + core + 3.2.1 + + + + com.google.zxing + javase + 3.2.1 + + com.alibaba.cloud @@ -102,6 +122,13 @@ 3.0.0 compile + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + compile + + ${project.artifactId} diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java new file mode 100644 index 00000000..a2d0ffe1 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java @@ -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); + +} diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java index 428a8791..9bb78c12 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java @@ -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 noSet = new HashSet<>(); - noSet.add(serialNumber); - R r = powerPileService.addPileWhitelist(chargingStationId, noSet); +// HashSet noSet = new HashSet<>(); +// noSet.add(serialNumber); +// R r = powerPileService.addPileWhitelist(chargingStationId, noSet); return AjaxResult.success(); } diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java new file mode 100644 index 00000000..6a121e38 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java @@ -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 hints = new Hashtable(); + 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(); + } + } +} \ No newline at end of file diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java new file mode 100644 index 00000000..efeba076 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java @@ -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()); + } + + } + +} diff --git a/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties b/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties new file mode 100644 index 00000000..7ad06636 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties @@ -0,0 +1,4 @@ +#ɵĶάλã +destPath=D:\\Enterprise_Resources\\XiaoHuaQrImgs +#LogoͼƬȡλã +imgPath=D:\\Enterprise_Resources\\XiaoHuaLogoImg\\xhcd_logo.png \ No newline at end of file diff --git a/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml new file mode 100644 index 00000000..e419470d --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml @@ -0,0 +1,11 @@ + + + + + + INSERT INTO xhpc_img(url,terminal_id) VALUES(#{imgUrl},#{terminalId}) + + +