完成整张图片生成功能,数据库图片URL数据正确,阿里云上存储的图片路径也正确了

This commit is contained in:
wen 2021-09-15 10:32:10 +08:00
parent 4096947015
commit 2508b6f829
9 changed files with 193 additions and 166 deletions

View File

@ -1,11 +1,13 @@
package com.xhpc.charging.station.controller;
import com.xhpc.charging.station.pojo.XhpcImg;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.charging.station.service.IXhpcChargingPileService;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -94,4 +96,14 @@ public class XhpcChargingPileController extends BaseController {
return toAjax(xhpcChargingPileService.updateXhpcChargingPileById(chargingPileId));
}
/**
* 下载对应终端的图片
*/
@PostMapping(value = "/downloadsTerminalImgs")
public AjaxResult downloadsTerminalImgs(@RequestBody List<Integer> terminalIds){
List<Map<String,Object>> xhpcImgs = xhpcChargingPileService.downloadsTerminalImgs(terminalIds);
return AjaxResult.success(xhpcImgs);
}
}

View File

@ -1,11 +1,15 @@
package com.xhpc.charging.station.mapper;
import com.xhpc.charging.station.pojo.XhpcImg;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 上传图片信息
* 图片表
*
* @author
* @author WH
*/
public interface XhpcImgMapper {
@ -19,4 +23,10 @@ public interface XhpcImgMapper {
*/
void insert(@Param("imgUrl") String imgUrl,@Param("terminalId") Long terminalId);
/**
*
* @param terminalIds 终端id集合
* @return 返回带有url和对应终端id编号的集合实体类
*/
List<Map<String,Object>> selectImgUrlByTerminal_id(List<Integer> terminalIds);
}

View File

@ -1,7 +1,9 @@
package com.xhpc.charging.station.service;
import com.xhpc.charging.station.pojo.XhpcImg;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import java.util.List;
import java.util.Map;
@ -63,4 +65,14 @@ public interface IXhpcChargingPileService {
*/
int updateXhpcChargingPileById(Long chargingPileId);
/**
* @Author WH
* @Date 2021/9/9 16:39
* @Description
* @Param [terminalIds]
* @Return List<XhpcImg>
* @Since version-1.0
*/
List<Map<String,Object>> downloadsTerminalImgs(List<Integer> terminalIds);
}

View File

@ -1,13 +1,11 @@
package com.xhpc.charging.station.service;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.xhpc.charging.station.mapper.XhpcChargingPileMapper;
import com.xhpc.charging.station.mapper.XhpcChargingStationMapper;
import com.xhpc.charging.station.mapper.XhpcImgMapper;
import com.xhpc.charging.station.mapper.XhpcTerminalMapper;
import com.xhpc.charging.station.utils.QrImgUtils;
import com.xhpc.charging.station.utils.img.QrImgUtils;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
@ -40,6 +38,19 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
//上传图片Mapper
@Autowired
private XhpcImgMapper xhpcImgMapper;
@Autowired
XhpcChargingStationMapper xhpcChargingStationMapper;
//字母集合
private static Map letterMap;
static{
Character letter = 'A';
letterMap = new HashMap<Integer,Character>();
for (int i = 1;i<=26;i++){
letterMap.put(i, letter);
letter++;
}
}
@ -92,64 +103,21 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
xhpcTerminal.setWorkStatus(2);
xhpcTerminal.setStatus(0);
xhpcTerminal.setRateModelId(rateModelId);
xhpcTerminal.setNumber(99);
xhpcTerminalMapper.addXhpcTerminal(xhpcTerminal);
//生成二维码图片并上传
QrImgUtils.uploadImg(xhpcTerminal,String.valueOf(chargingPileId),environment,xhpcImgMapper);
//获取场站名称
String chargingStationName = xhpcChargingStationMapper.selectXhpcChargingStationById(xhpcTerminal.getChargingStationId()).getName();
//生成最终的完整图片并上传
QrImgUtils.uploadImg(xhpcTerminal, xhpcTerminal.getSerialNumber(),environment,xhpcImgMapper,chargingStationName,i,letterMap);
// //生成桩的二维码图片
// //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);
// powerPileService.addPileWhitelist(chargingStationId, noSet);
return AjaxResult.success();
}
@ -181,15 +149,15 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
xhpcTerminal.setChargingStationId(chargingStationId);
xhpcTerminal.setChargingPileId(chargingPileId);
xhpcTerminal.setName(serialNumber + "-" + (i));
xhpcTerminal.setSerialNumber(serialNumber + "0" + i);
String num = serialNumber + "0" + i;
xhpcTerminal.setSerialNumber(num);
xhpcTerminal.setPileSerialNumber(serialNumber);
xhpcTerminal.setWorkStatus(2);
xhpcTerminal.setStatus(0);
xhpcTerminal.setRateModelId(rateModelId);
xhpcTerminalMapper.addXhpcTerminal(xhpcTerminal);
//生成二维码图片并上传
QrImgUtils.uploadImg(xhpcTerminal,String.valueOf(chargingPileId),environment,xhpcImgMapper);
// //生成二维码图片并上传
// QrImgUtils.uploadImg(xhpcTerminal,num,environment,xhpcImgMapper);
}
@ -240,4 +208,14 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
return 0;
}
/**
* 下载对应终端的二维码图片
* @param terminalIds
* @return
*/
@Override
public List<Map<String,Object>> downloadsTerminalImgs(List<Integer> terminalIds) {
return xhpcImgMapper.selectImgUrlByTerminal_id(terminalIds);
}
}

View File

@ -1,9 +1,6 @@
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.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@ -22,55 +19,73 @@ public class ImageUtil {
private BufferedImage tempImg = null;
public static void main(String[] args) throws Exception {
ImageUtil img = new ImageUtil();
img.createXhImg();
public static void main(String[] args){
}
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);
/**
*
* @param bottomFile 底图位置
* @param qrcode 二维码位置
* @param logo 底部Logo位置
* @param serialNumber 终端编码
* @param fullImgdestPath 最终生成的图片的存放位置
* @param stationName 场站名称
* @param pileNumber 桩号
* @param letter 枪上的字母
* @return boolean
* @throws IOException
*/
public boolean createXhImg(File bottomFile, File qrcode, File logo, String serialNumber, File fullImgdestPath, String stationName, String pileNumber, String letter){
return true;
try {
tempImg = this.createBottom(bottomFile);
tempImg = this.addQrCodeToBottom(tempImg,qrcode);
tempImg = this.addBottomLogo(tempImg,logo);
tempImg = this.writeSerialNumber(tempImg,serialNumber);
this.addHeader(tempImg,fullImgdestPath,stationName,pileNumber,letter);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
*
* @param source 合成了二维码底部Logo终端编号头部的最终图
* @param fullImgdestPath 最终生成的底图的存放位置
* @param stationName 场站名称
* @param pileNumber 终端编码
* @param letter 枪上的字母
* @return 添加了顶部文字的最终图片
* @throws IOException
*/
private BufferedImage addHeader(BufferedImage source) throws IOException {
private void addHeader(BufferedImage source,File fullImgdestPath,String stationName,String pileNumber,String letter) throws IOException {
Graphics2D g2 = source.createGraphics();
g2.setColor(Color.BLACK);
// //顶部占位矩形
// g2.fillRect(0,0,400,190);
//写入桩部分
writePile(source);
writePile(source,stationName,pileNumber);
//写入枪部分
writeGun(source);
writeGun(source,letter);
//生成的图片的存放路径
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha8.png");
// File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha8.png");
File dest = fullImgdestPath;
ImageIO.write(source, "png", dest);
return source;
}
/**
* 顶部文字右边部分
* @param source 合成了二维码底部Logo终端编号枪部分部分的底图
* @param source 合成了二维码底部Logo终端编号场站名称桩号枪部分的底图
* @param letter 字母
* @return 返回最终合成的图
*/
private BufferedImage writeGun(BufferedImage source) throws IOException {
private BufferedImage writeGun(BufferedImage source,String letter){
Graphics2D g2 = source.createGraphics();
g2.setColor(Color.WHITE);
@ -78,57 +93,54 @@ public class ImageUtil {
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(new Font("黑体",Font.BOLD,130));
g2.drawString("A",250,180);
g2.drawString(letter,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
* 顶部文字左边部分及其场站名称
* @param source 合成了二维码底部Logo终端编码的底图
* @param StationName 场站名称
* @param PileNumber 桩号数字
* @return source 合成了二维码底部Logo终端编码场站名称桩号的底图
* @throws IOException
*/
private BufferedImage writePile(BufferedImage source) throws IOException{
private BufferedImage writePile(BufferedImage source,String StationName,String PileNumber){
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));
//在图片中居中显示文字
String realStationName = StationName; //定义文字内容
Font font=new Font("楷体",Font.PLAIN,24); //设置文字大小
g2.setFont(font); //设置到画笔
// 计算文字长度计算居中的x点坐标
FontMetrics fm = g2.getFontMetrics(font); //获取该大小字体的字体度量单位
int textWidth = fm.stringWidth(realStationName); //传入文字内容让它计算出文字内容宽度
int widthX = (400 - textWidth) / 2; //此处的400是图片的宽度用于计算出文字居中后的width
// 表示这段文字在图片上的位置(x,y) 第一个是你设置的内容
g2.drawString(realStationName,widthX,70); //此处的70是指放置居中后的文字的水平位置
//后期改成居中不写死
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);
g2.drawString(PileNumber+"号桩",25,154);
//生成的图片的存放路径
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha6.png");
ImageIO.write(source, "png", dest);
return source;
}
/**
*
* @param source 合成了二维码底部Logo的底图
* @return 添加了终端编号的底图
* @param SerialNumber 终端编码
* @return 添加了终端编码的底图
* @throws IOException
*/
private BufferedImage writeSerialNumber(BufferedImage source) throws IOException {
private BufferedImage writeSerialNumber(BufferedImage source,String SerialNumber){
Graphics2D g2 = source.createGraphics();
@ -140,12 +152,8 @@ public class ImageUtil {
g2.drawRect(10,210,380,70);
g2.drawString("终端编码:6985214589545801",27,255);
g2.drawString("终端编码:"+SerialNumber,17,255);
//生成的图片的存放路径
File dest = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\haha5.png");
ImageIO.write(source, "png", dest);
return source;
}
@ -153,37 +161,35 @@ public class ImageUtil {
/**
*
* @param source 合成了二维码的底图
* @param Logo 底图位置
* @return 添加了底部Logo的底图
* @throws IOException
*/
private BufferedImage addBottomLogo(BufferedImage source) throws IOException {
private BufferedImage addBottomLogo(BufferedImage source,File Logo) throws IOException {
//底部Logo存放位置
File QrCodeImgPath = new File("D:\\Enterprise_Resources\\XiaoHuaLogoImg\\BottomLogo.png");
File LogoImgPath = Logo;
//使用ImageIO将logo图片加载到内存中
BufferedImage QrCodeImg = ImageIO.read(QrCodeImgPath);
BufferedImage QrCodeImg = ImageIO.read(LogoImgPath);
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 底图
* @param source 底图图片
* @param QrCode 二维码存放位置
* @return 添加了二维码的底图
* @throws IOException
*/
private BufferedImage addQrCodeToBottom(BufferedImage source) throws IOException {
private BufferedImage addQrCodeToBottom(BufferedImage source,File QrCode) throws IOException {
//二维码图片存放位置
File QrCodeImgPath = new File("D:\\Enterprise_Resources\\XiaoHuaQrImgs\\2021091011050707669852145895458.png");
File QrCodeImgPath = QrCode;
//使用ImageIO将图片加载到内存中
BufferedImage QrCodeImg = ImageIO.read(QrCodeImgPath);
@ -191,44 +197,30 @@ public class ImageUtil {
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;
}
/**
* 创建图片底图
* @param bottomFile 底图文件
* @return
*/
private BufferedImage createBottom(File bottomFile) throws IOException {
int width = 400; //底图的宽
int height = 800; //底图的高
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();
//生成图片画布
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//生成用于该画布的画笔对象
Graphics2D g2 = bi.createGraphics();
//载入生成的二维码图片
BufferedImage bottomImg = ImageIO.read(bottomFile);
g2.drawImage(bottomImg,0,0,null);
//设置画笔颜色为蓝色
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;
return bi;
}
}

View File

@ -8,6 +8,7 @@ import org.springframework.core.env.Environment;
import java.io.File;
import java.util.Date;
import java.util.Map;
public class QrImgUtils {
@ -19,42 +20,55 @@ public class QrImgUtils {
* @Return void
* @Since version-1.0
*/
public static void uploadImg(XhpcTerminal xhpcTerminal, String serialNumber, Environment environment, XhpcImgMapper xhpcImgMapper) {
public static void uploadImg(XhpcTerminal xhpcTerminal, String serialNumber, Environment environment, XhpcImgMapper xhpcImgMapper, String chargingStationName, int forIndex, Map<Integer,Character> letterMap) {
//生成桩的二维码图片
//1.生成二维码链接内容
StringBuilder prefix = new StringBuilder("https://xhpc.scxhua.com?pNum=");
String QrContent = prefix.append(xhpcTerminal.getSerialNumber()).toString();
//2.生成图片
try {
//2.生成二维码图片
//2.1生成指定规则的图片名
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + serialNumber + ".png";
String qrFileName = 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();
File QrCode = QRCodeUtil.encode(QrContent, environment.getProperty("oss.imgPath"), environment.getProperty("oss.destPath"), true, qrFileName);
//3.上传图片至服务器
//3.拼接成完整图片
//3.1生成指定规则的图片名
String finallyImgFileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + serialNumber + ".png";
new ImageUtil().createXhImg(
new File(environment.getProperty("oss.bottomImg")),
QrCode,
new File(environment.getProperty("oss.bottomLogoImg")),
serialNumber,
new File(environment.getProperty("oss.fullImgDestPath")+finallyImgFileName),
chargingStationName,
String.valueOf(xhpcTerminal.getNumber()),
String.valueOf(letterMap.get(forIndex)));
//4.上传图片至服务器
// 创建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.putObject(environment.getProperty("oss.bucket-name"), xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName, new File(environment.getProperty("oss.destPath") + "\\" + finallyImgFileName));
ossClient.shutdown();
//4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
//5.将放在阿里云上的生成的图片的路径和图片所对应的终端的id放入数据库xhpc_img表中
Long terminalId = xhpcTerminal.getTerminalId();
xhpcImgMapper.insert(fileName, terminalId);
xhpcImgMapper.insert(xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName, terminalId);
//4.删除生成的二维码图片
// File QrImg = new File(environment.getProperty("oss.destPath") + "\\" + fileName);
// //6.删除生成的二维码图片
// File QrImg = new File(environment.getProperty("oss.destPath") + "\\" + qrFileName);
// //7.删除生成本地生成的完整图片
// File finallyImg = new File(environment.getProperty("oss.destPath") + "\\" + finallyImgFileName);
// if (QrImg.exists()) {
// QrImg.delete();
// } else {
// throw new RuntimeException("名字为" + fileName + "的二维码不存在");
// }
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());

View File

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

View File

@ -353,4 +353,5 @@
set del_flag = 2
where charging_pile_id = #{chargingPileId}
</update>
</mapper>

View File

@ -8,4 +8,16 @@
INSERT INTO xhpc_img(url,terminal_id) VALUES(#{imgUrl},#{terminalId})
</insert>
<select id="selectImgUrlByTerminal_id" parameterType="int" resultType="map">
SELECT url,terminal_id as terminalId
FROM xhpc_img
where 1=1
<if test="list!=null">
and terminal_id in
<foreach collection="list" item="terminalId" open="(" close=")" separator=",">
#{terminalId}
</foreach>
</if>
</select>
</mapper>