Browse Source

情报板图片生成工具

develop
lau572 9 months ago
parent
commit
4d80287e17
  1. 8
      ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java
  2. 80
      zc-business/src/main/java/com/zc/business/domain/DcBoardImgLane.java
  3. 150
      zc-business/src/main/java/com/zc/business/domain/ImageDTO.java
  4. 62
      zc-business/src/main/java/com/zc/business/enums/BoardIconEnum.java
  5. 195
      zc-business/src/main/java/com/zc/business/utils/BoardImageUtils.java
  6. 2
      zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml

8
ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java

@ -138,4 +138,12 @@ public class RuoYiConfig
{
return getProfile() + "/protocol";
}
/**
* 获取情报板下发图片路径
*/
public static String getBoardPath()
{
return getProfile() + "/boardTemplate";
}
}

80
zc-business/src/main/java/com/zc/business/domain/DcBoardImgLane.java

@ -0,0 +1,80 @@
package com.zc.business.domain;
import com.zc.business.enums.BoardIconEnum;
/**
* @Description
*
* @author liuwenge
* @date 2024/2/27 17:07
*/
public class DcBoardImgLane {
//第一车道
private BoardIconEnum firstLane;
//第二车道
private BoardIconEnum secondLane;
//第三车道
private BoardIconEnum thirdLane;
//第四车道
private BoardIconEnum fourthLane;
//应急车道
private BoardIconEnum emergencyLane;
public BoardIconEnum getFirstLane() {
return firstLane;
}
public void setFirstLane(BoardIconEnum firstLane) {
this.firstLane = firstLane;
}
public BoardIconEnum getSecondLane() {
return secondLane;
}
public void setSecondLane(BoardIconEnum secondLane) {
this.secondLane = secondLane;
}
public BoardIconEnum getThirdLane() {
return thirdLane;
}
public void setThirdLane(BoardIconEnum thirdLane) {
this.thirdLane = thirdLane;
}
public BoardIconEnum getFourthLane() {
return fourthLane;
}
public void setFourthLane(BoardIconEnum fourthLane) {
this.fourthLane = fourthLane;
}
public BoardIconEnum getEmergencyLane() {
return emergencyLane;
}
public void setEmergencyLane(BoardIconEnum emergencyLane) {
this.emergencyLane = emergencyLane;
}
public DcBoardImgLane(){};
public DcBoardImgLane(BoardIconEnum firstLane, BoardIconEnum secondLane, BoardIconEnum thirdLane, BoardIconEnum fourthLane, BoardIconEnum emergencyLane){
this.firstLane = firstLane;
this.secondLane = secondLane;
this.thirdLane = thirdLane;
this.fourthLane = fourthLane;
this.emergencyLane = emergencyLane;
};
}

150
zc-business/src/main/java/com/zc/business/domain/ImageDTO.java

@ -0,0 +1,150 @@
package com.zc.business.domain;
import sun.font.FontDesignMetrics;
import java.awt.*;
/**
* @Description:ruoyi
* @auther: Lenovo
* @date: 2024/2/2616:43
* @param: param
* @return: 结果
*/
public class ImageDTO {
//类型(0:文字,1:矩形)
private int type;
//文字内容
private String text;
//字体颜色和透明度
private Color color;
//字体和大小
private Font font;
//左上角起始点的x坐标
private int x;
//左上角起始点的y坐标
private int y;
//矩形宽度
private int rectWidth;
//矩形高度
private int rectHeight;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRectWidth() {
return rectWidth;
}
public void setRectWidth(int rectWidth) {
this.rectWidth = rectWidth;
}
public int getRectHeight() {
return rectHeight;
}
public void setRectHeight(int rectHeight) {
this.rectHeight = rectHeight;
}
/**
* 创建文字ImageDTO, 每一个对象,代表在该图片中要插入的一段文字内容:
* @param text : 文本内容;
* @param color : 字体颜色(前三位)和透明度(第4位,值越小,越透明);
* @param font : 字体的样式和字体大小;
* @param x : 当前字体在该图片位置的横坐标;
* @param y : 当前字体在该图片位置的纵坐标;
* @return
*/
public static ImageDTO createImageDTO(String text,Color color,Font font,int x,int y){
ImageDTO imageDTO = new ImageDTO();
imageDTO.setType(0);
imageDTO.setText(text);
imageDTO.setColor(color);
imageDTO.setFont(font);
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
y = y + metrics.getAscent() - (metrics.getAscent() + metrics.getDescent() - font.getSize()) / 2;
imageDTO.setX(x);
imageDTO.setY(y);
return imageDTO;
}
public ImageDTO(){}
/**
* 创建ImageDTO, 每一个对象,代表在该图片中要插入的矩形:
* @param color : 颜色(前三位)和透明度(第4位,值越小,越透明);
* @param x : 当前矩形在该图片位置的横坐标;
* @param y : 当前矩形在该图片位置的纵坐标;
* @param rectWidth : 当前矩形的宽度;
* @param rectHeight : 当前矩形的高度;
* @return
*/
public static ImageDTO createRectImageDTO(Color color,int x,int y,int rectWidth,int rectHeight){
ImageDTO imageDTO = new ImageDTO();
imageDTO.setType(1);
imageDTO.setColor(color);
imageDTO.setX(x);
imageDTO.setY(y);
imageDTO.setRectWidth(rectWidth);
imageDTO.setRectHeight(rectHeight);
return imageDTO;
}
}

62
zc-business/src/main/java/com/zc/business/enums/BoardIconEnum.java

@ -0,0 +1,62 @@
package com.zc.business.enums;
/**
* 情报板车道图标枚举
* @author
*/
public enum BoardIconEnum {
// 直行标志
STRAIGHT_TRAVEL(1,"/zhixing.png"),
// 左侧绕行标志
LEFT_DETOUR(2,"/raozuo.png"),
// 右侧绕行标志
RIGHT_DETOUR(3,"/raoyou.png"),
// 禁行标志
LANE_PROHIBITION(4,"/jinxing.png"),
// 货车禁行
TRUCK_PROHIBITION(5,"/huochejinxing.png"),
// 应急车道标志
EMERGENCY_LANE(6,"/yingji.png"),
// 限速120
SPEED_LIMIT_120(7,"/xiansu120.png"),
// 限速100
SPEED_LIMIT_100(8,"/xiansu100.png"),
// 限速80
SPEED_LIMIT_80(9,"/xiansu80.png"),
// 限速60
SPEED_LIMIT_60(10,"/xiansu60.png"),
// 限速40
SPEED_LIMIT_40(11,"/xiansu40.png"),
// 限速30
SPEED_LIMIT_30(12,"/xiansu30.png");
private final int value;
private final String filePath;
BoardIconEnum(int value,String filePath) {
this.value = value;
this.filePath = filePath;
}
public int getValue() {
return this.value;
}
public String getFilePath() {
return this.filePath;
}
}

195
zc-business/src/main/java/com/zc/business/utils/BoardImageUtils.java

@ -0,0 +1,195 @@
package com.zc.business.utils;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.zc.business.domain.DcBoardImgLane;
import com.zc.business.domain.ImageDTO;
import com.zc.business.enums.BoardIconEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 情报板生成图片工具
*/
public class BoardImageUtils {
private static final Logger log = LoggerFactory.getLogger(BoardImageUtils.class);
//分辨率 宽度
private static final int width = 768;
//分辨率高度
private static final int height = 64;
//图标地址
private static final String templatePath = "D:\\ruoyi\\uploadPath\\boardTemplate";
// private static final String templatePath = RuoYiConfig.getBoardPath();
/**
* 事件场景
*
* @param list
*/
public static AjaxResult writeEventImage(List<ImageDTO> list) {
// String filePath = RuoYiConfig.getBoardPath() + "/boardImg.png";
String filePath = "C:/Users/Lenovo/Desktop/event.png";
FileOutputStream outImgStream = null;
try {
//创建图片
BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
//背景颜色
g.setBackground(Color.BLACK);
//编辑图片,插入要素
for (ImageDTO imgDTO : list) {
if (imgDTO.getType() == 0) {
//插入文本
g.setColor(imgDTO.getColor()); //根据图片的背景设置水印颜色
g.setFont(imgDTO.getFont()); //设置字体
g.drawString(imgDTO.getText(), imgDTO.getX(), imgDTO.getY()); //画出水印
} else if (imgDTO.getType() == 1){
//插入矩形
g.setColor(imgDTO.getColor());
g.fillRect(imgDTO.getX(),imgDTO.getY(),imgDTO.getRectWidth(),imgDTO.getRectHeight());
}
}
//事故图标
File srcImgFile = new File(templatePath + "/jinzhi.png");//得到文件
Image jinzhi = ImageIO.read(srcImgFile);//文件转化为图片
g.drawImage(jinzhi,534,17,30,30,null);
g.dispose();
// 输出图片
outImgStream = new FileOutputStream(filePath);
ImageIO.write(bufImg, "jpg", outImgStream);
} catch (Exception e) {
log.error("==== 系统异常::{} ====",e);
} finally {
try {
if (null != outImgStream){
outImgStream.flush();
outImgStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return AjaxResult.success(filePath);
}
/**
* 车道通行场景
*
* @param dcBoardImgLane
*/
public static AjaxResult writeLaneImage(DcBoardImgLane dcBoardImgLane) {
// String filePath = RuoYiConfig.getBoardPath() + "/boardImg.png";
String filePath = "C:/Users/Lenovo/Desktop/lane.png";
FileOutputStream outImgStream = null;
try {
//创建图片
BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
//背景颜色
g.setBackground(Color.BLACK);
//分割线图标
Image fenge = ImageIO.read(new File(templatePath + "/fenge.png"));//文件转化为图片
g.drawImage(fenge,150,4,8,56,null);
g.drawImage(fenge,304,4,8,56,null);
g.drawImage(fenge,458,4,8,56,null);
g.drawImage(fenge,612,4,8,56,null);
//第一车道
Image icon = ImageIO.read(new File(templatePath + dcBoardImgLane.getFirstLane().getFilePath()));
g.drawImage(icon,49,4,56,56,null);
//第二车道
icon = ImageIO.read(new File(templatePath + dcBoardImgLane.getSecondLane().getFilePath()));
g.drawImage(icon,203,4,56,56,null);
//第三车道
icon = ImageIO.read(new File(templatePath + dcBoardImgLane.getThirdLane().getFilePath()));
g.drawImage(icon,357,4,56,56,null);
//第四车道
icon = ImageIO.read(new File(templatePath + dcBoardImgLane.getFourthLane().getFilePath()));
g.drawImage(icon,511,4,56,56,null);
//应急车道
icon = ImageIO.read(new File(templatePath + dcBoardImgLane.getEmergencyLane().getFilePath()));
g.drawImage(icon,665,4,56,56,null);
g.dispose();
// 输出图片
outImgStream = new FileOutputStream(filePath);
ImageIO.write(bufImg, "jpg", outImgStream);
} catch (Exception e) {
log.error("==== 系统异常::{} ====",e);
} finally {
try {
if (null != outImgStream){
outImgStream.flush();
outImgStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return AjaxResult.success(filePath);
}
/**
* main方法:
* @param args
*/
public static void main(String[] args) {
//获取数据集合;
/*ArrayList<ImageDTO> list = new ArrayList<>();
list.add(ImageDTO.createRectImageDTO(new Color(0,255,0), 34, 26, 300,16));
list.add(ImageDTO.createRectImageDTO(new Color(255,255,0), 334, 26, 150,16));
list.add(ImageDTO.createRectImageDTO(new Color(255,0,0),484, 26,50,16));
list.add(ImageDTO.createRectImageDTO(new Color(0,255,0),534, 26,200,16));
list.add(ImageDTO.createImageDTO("前方5公里发生事故缓行10分钟",new Color(255,0,0),new Font("Default", Font.BOLD, 22), 34, 0));
list.add(ImageDTO.createImageDTO("长清收费站约5分钟到达",new Color(255,255,255),new Font("Default", Font.BOLD, 12 ), 296, 51));
//事件场景
BoardImageUtils.writeEventImage(list);*/
DcBoardImgLane boardImgLane = new DcBoardImgLane(BoardIconEnum.SPEED_LIMIT_120,BoardIconEnum.SPEED_LIMIT_120,BoardIconEnum.SPEED_LIMIT_100,BoardIconEnum.SPEED_LIMIT_100,BoardIconEnum.EMERGENCY_LANE);
//车道通行场景
BoardImageUtils.writeLaneImage(boardImgLane);
boardImgLane = new DcBoardImgLane(BoardIconEnum.STRAIGHT_TRAVEL,BoardIconEnum.LEFT_DETOUR,BoardIconEnum.LANE_PROHIBITION,BoardIconEnum.RIGHT_DETOUR,BoardIconEnum.EMERGENCY_LANE);
//车道通行场景
BoardImageUtils.writeLaneImage(boardImgLane);
//这句代码,自己项目中可以不用加,在这里防止main方法报错的;
System.exit(0);
}
}

2
zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml

@ -22,7 +22,7 @@
</select>
<select id="getEventListByType" resultType="java.util.Map" parameterType="String">
select t1.id,t1.stake_mark stakeMark,t1.direction, t2.longitude,t2.latitude
select t1.id,t1.stake_mark stakeMark,t1.direction,t1.lang, t2.longitude,t2.latitude
from dc_event t1 left join dc_stake_mark t2 on t1.stake_mark = t2.stake_mark and t1.direction = t2.direction
where t1.event_type = #{eventType} and t1.event_state != '2'
</select>

Loading…
Cancel
Save