设备行为
This commit is contained in:
@ -6,7 +6,7 @@ package com.fuyuanshen.web.enums;
|
|||||||
*/
|
*/
|
||||||
public enum InstructType6170 {
|
public enum InstructType6170 {
|
||||||
|
|
||||||
EQUIPMENT_REPORTING(0, "设备上报"),
|
EQUIPMENT_REPORTING(0, "设备启动"),
|
||||||
LIGHT_MODE(1, "灯光模式"),
|
LIGHT_MODE(1, "灯光模式"),
|
||||||
/**
|
/**
|
||||||
* 设备信息
|
* 设备信息
|
||||||
|
@ -7,6 +7,8 @@ import com.fuyuanshen.equipment.domain.Device;
|
|||||||
import com.fuyuanshen.equipment.domain.DeviceLog;
|
import com.fuyuanshen.equipment.domain.DeviceLog;
|
||||||
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
|
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
|
||||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||||
|
import com.fuyuanshen.equipment.utils.map.GetAddressFromLatUtil;
|
||||||
|
import com.fuyuanshen.equipment.utils.map.LngLonUtil;
|
||||||
import com.fuyuanshen.web.enums.InstructType6170;
|
import com.fuyuanshen.web.enums.InstructType6170;
|
||||||
import com.fuyuanshen.web.enums.LightModeEnum6170;
|
import com.fuyuanshen.web.enums.LightModeEnum6170;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@ -63,16 +65,20 @@ public class DeviceReceiverMessageHandler implements MessageHandler {
|
|||||||
try {
|
try {
|
||||||
JsonNode root = objectMapper.readTree(payload);
|
JsonNode root = objectMapper.readTree(payload);
|
||||||
|
|
||||||
|
DeviceLog record = new DeviceLog();
|
||||||
|
// 手动设置租户ID
|
||||||
|
record.setTenantId(device.getTenantId()); // 从设备信息中获取租户ID
|
||||||
|
// 设备ID
|
||||||
|
record.setDeviceId(device.getId());
|
||||||
|
// 设备名称
|
||||||
|
record.setDeviceName(device.getDeviceName());
|
||||||
|
|
||||||
// 2. 处理instruct消息
|
// 2. 处理instruct消息
|
||||||
if (root.has("instruct")) {
|
if (root.has("instruct")) {
|
||||||
JsonNode instructNode = root.get("instruct");
|
JsonNode instructNode = root.get("instruct");
|
||||||
if (instructNode.isArray()) {
|
if (instructNode.isArray()) {
|
||||||
DeviceLog record = parseInstruct(device, instructNode);
|
boolean b = receivedTopicName.startsWith("B/");
|
||||||
// 手动设置租户ID
|
record = parseInstruct(device, instructNode, b);
|
||||||
record.setTenantId(device.getTenantId()); // 从设备信息中获取租户ID
|
|
||||||
// 设备ID
|
|
||||||
record.setDeviceId(device.getId());
|
|
||||||
|
|
||||||
// 根据不同主题进行不同处理
|
// 根据不同主题进行不同处理
|
||||||
if (receivedTopicName.startsWith("A/")) {
|
if (receivedTopicName.startsWith("A/")) {
|
||||||
// 处理A主题的消息(设备上传)
|
// 处理A主题的消息(设备上传)
|
||||||
@ -81,10 +87,19 @@ public class DeviceReceiverMessageHandler implements MessageHandler {
|
|||||||
// 处理B主题的消息 (手动上传)
|
// 处理B主题的消息 (手动上传)
|
||||||
record.setDataSource("客户端操作");
|
record.setDataSource("客户端操作");
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceLogMapper.insert(record);
|
|
||||||
}
|
}
|
||||||
|
deviceLogMapper.insert(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (root.has("imei")) {
|
||||||
|
// 设备行为
|
||||||
|
record.setDeviceAction(InstructType6170.fromCode(0).getDescription());
|
||||||
|
record.setDataSource("设备上报");
|
||||||
|
record.setContent("设备启动");
|
||||||
|
deviceLogMapper.insert(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 3. 处理state消息
|
// 3. 处理state消息
|
||||||
// else if (root.has("state")) {
|
// else if (root.has("state")) {
|
||||||
// JsonNode stateNode = root.get("state");
|
// JsonNode stateNode = root.get("state");
|
||||||
@ -126,9 +141,10 @@ public class DeviceReceiverMessageHandler implements MessageHandler {
|
|||||||
*
|
*
|
||||||
* @param device
|
* @param device
|
||||||
* @param array
|
* @param array
|
||||||
|
* @param b
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private DeviceLog parseInstruct(Device device, JsonNode array) {
|
private DeviceLog parseInstruct(Device device, JsonNode array, boolean b) {
|
||||||
DeviceLog record = new DeviceLog();
|
DeviceLog record = new DeviceLog();
|
||||||
record.setDeviceName(device.getDeviceName());
|
record.setDeviceName(device.getDeviceName());
|
||||||
// 设备行为
|
// 设备行为
|
||||||
@ -173,10 +189,21 @@ public class DeviceReceiverMessageHandler implements MessageHandler {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 11: // 定位数据
|
case 11: // 定位数据
|
||||||
// record.setLatitudeDeg(array.get(1).asInt());
|
if (b) {
|
||||||
// record.setLatitudeMin(new BigDecimal(array.get(2).asDouble()));
|
break;
|
||||||
// record.setLongitudeDeg(array.get(3).asInt());
|
}
|
||||||
// record.setLongitudeMin(new BigDecimal(array.get(4).asDouble()));
|
int i1 = array.get(1).asInt();
|
||||||
|
int i2 = array.get(2).asInt();
|
||||||
|
int i3 = array.get(3).asInt();
|
||||||
|
int i4 = array.get(4).asInt();
|
||||||
|
|
||||||
|
// 优雅的转换方式 Longitude and latitude
|
||||||
|
double latitude = i1 + i2 / 10.0;
|
||||||
|
double Longitude = i3 + i4 / 10.0;
|
||||||
|
// 84 ==》 高德
|
||||||
|
double[] doubles = LngLonUtil.gps84_To_Gcj02(latitude, Longitude);
|
||||||
|
String address = GetAddressFromLatUtil.getAdd(String.valueOf(doubles[1]), String.valueOf(doubles[0]));
|
||||||
|
record.setContent(address);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return record;
|
return record;
|
||||||
|
@ -8,6 +8,8 @@ import lombok.Data;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import jakarta.validation.constraints.*;
|
import jakarta.validation.constraints.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备日志业务对象 device_log
|
* 设备日志业务对象 device_log
|
||||||
*
|
*
|
||||||
@ -22,9 +24,14 @@ public class DeviceLogBo extends BaseEntity {
|
|||||||
/**
|
/**
|
||||||
* ID
|
* ID
|
||||||
*/
|
*/
|
||||||
@NotNull(message = "ID不能为空", groups = { EditGroup.class })
|
@NotNull(message = "ID不能为空", groups = {EditGroup.class})
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备编号
|
||||||
|
*/
|
||||||
|
private List<Long> deviceIds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备行为
|
* 设备行为
|
||||||
*/
|
*/
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
package com.fuyuanshen.equipment.service.impl;
|
package com.fuyuanshen.equipment.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.fuyuanshen.common.core.utils.MapstructUtils;
|
import com.fuyuanshen.common.core.utils.MapstructUtils;
|
||||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||||
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
|
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||||
|
import com.fuyuanshen.equipment.domain.DeviceAssignments;
|
||||||
|
import com.fuyuanshen.equipment.mapper.DeviceAssignmentsMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -19,6 +24,7 @@ import com.fuyuanshen.equipment.service.IDeviceLogService;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备日志Service业务层处理
|
* 设备日志Service业务层处理
|
||||||
@ -33,6 +39,9 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
|
|
||||||
private final DeviceLogMapper baseMapper;
|
private final DeviceLogMapper baseMapper;
|
||||||
|
|
||||||
|
private final DeviceAssignmentsMapper deviceAssignmentsMapper;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询设备日志
|
* 查询设备日志
|
||||||
*
|
*
|
||||||
@ -40,7 +49,7 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
* @return 设备日志
|
* @return 设备日志
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DeviceLogVo queryById(Long id){
|
public DeviceLogVo queryById(Long id) {
|
||||||
return baseMapper.selectVoById(id);
|
return baseMapper.selectVoById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +62,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public TableDataInfo<DeviceLogVo> queryPageList(DeviceLogBo bo, PageQuery pageQuery) {
|
public TableDataInfo<DeviceLogVo> queryPageList(DeviceLogBo bo, PageQuery pageQuery) {
|
||||||
|
|
||||||
|
|
||||||
LambdaQueryWrapper<DeviceLog> lqw = buildQueryWrapper(bo);
|
LambdaQueryWrapper<DeviceLog> lqw = buildQueryWrapper(bo);
|
||||||
Page<DeviceLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
Page<DeviceLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||||
return TableDataInfo.build(result);
|
return TableDataInfo.build(result);
|
||||||
@ -71,6 +82,15 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private LambdaQueryWrapper<DeviceLog> buildQueryWrapper(DeviceLogBo bo) {
|
private LambdaQueryWrapper<DeviceLog> buildQueryWrapper(DeviceLogBo bo) {
|
||||||
|
|
||||||
|
Long userId = LoginHelper.getUserId();
|
||||||
|
List<DeviceAssignments> assignments = deviceAssignmentsMapper.selectList(new QueryWrapper<DeviceAssignments>().eq("assignee_id", userId));
|
||||||
|
List<Long> deviceIds = assignments.stream().map(DeviceAssignments::getDeviceId).collect(Collectors.toList());
|
||||||
|
if (deviceIds.isEmpty()) {
|
||||||
|
deviceIds.add(-1L);
|
||||||
|
}
|
||||||
|
bo.setDeviceIds(deviceIds);
|
||||||
|
|
||||||
Map<String, Object> params = bo.getParams();
|
Map<String, Object> params = bo.getParams();
|
||||||
LambdaQueryWrapper<DeviceLog> lqw = Wrappers.lambdaQuery();
|
LambdaQueryWrapper<DeviceLog> lqw = Wrappers.lambdaQuery();
|
||||||
lqw.orderByDesc(DeviceLog::getCreateTime);
|
lqw.orderByDesc(DeviceLog::getCreateTime);
|
||||||
@ -78,6 +98,7 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), DeviceLog::getDeviceName, bo.getDeviceName());
|
lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), DeviceLog::getDeviceName, bo.getDeviceName());
|
||||||
lqw.eq(StringUtils.isNotBlank(bo.getDataSource()), DeviceLog::getDataSource, bo.getDataSource());
|
lqw.eq(StringUtils.isNotBlank(bo.getDataSource()), DeviceLog::getDataSource, bo.getDataSource());
|
||||||
lqw.like(StringUtils.isNotBlank(bo.getContent()), DeviceLog::getContent, bo.getContent());
|
lqw.like(StringUtils.isNotBlank(bo.getContent()), DeviceLog::getContent, bo.getContent());
|
||||||
|
lqw.in(CollectionUtil.isNotEmpty(bo.getDeviceIds()), DeviceLog::getDeviceId, bo.getDeviceIds());
|
||||||
return lqw;
|
return lqw;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,8 +136,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
/**
|
/**
|
||||||
* 保存前的数据校验
|
* 保存前的数据校验
|
||||||
*/
|
*/
|
||||||
private void validEntityBeforeSave(DeviceLog entity){
|
private void validEntityBeforeSave(DeviceLog entity) {
|
||||||
//TODO 做一些数据校验,如唯一约束
|
// TODO 做一些数据校验,如唯一约束
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,8 +149,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||||||
if(isValid){
|
if (isValid) {
|
||||||
//TODO 做一些业务上的校验,判断是否需要校验
|
// TODO 做一些业务上的校验,判断是否需要校验
|
||||||
}
|
}
|
||||||
return baseMapper.deleteByIds(ids) > 0;
|
return baseMapper.deleteByIds(ids) > 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,204 @@
|
|||||||
|
package com.fuyuanshen.equipment.utils.c;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class DotMatrixDisplaySimulator extends JFrame {
|
||||||
|
|
||||||
|
private static final int WIDTH = 160;
|
||||||
|
private static final int HEIGHT = 80;
|
||||||
|
private static final int SCALE = 4; // 显示缩放比例
|
||||||
|
|
||||||
|
private JComboBox<Integer> fontSizeCombo;
|
||||||
|
private JComboBox<String> exampleCombo;
|
||||||
|
private JTextArea textInput;
|
||||||
|
private DotMatrixPanel displayPanel;
|
||||||
|
|
||||||
|
private final String[] examples = {
|
||||||
|
"紧急通知:现场危险,请立即撤离!",
|
||||||
|
"警告:高温区域,禁止入内!",
|
||||||
|
"系统故障:请立即联系技术人员处理",
|
||||||
|
"安全提示:请佩戴防护装备进入作业区",
|
||||||
|
"欢迎使用点阵显示屏模拟系统",
|
||||||
|
"现场危险,停止救援,紧急撤离至安全区域!"
|
||||||
|
};
|
||||||
|
|
||||||
|
public DotMatrixDisplaySimulator() {
|
||||||
|
super("160x80点阵显示屏模拟器");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setSize(800, 600);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// 创建控制面板
|
||||||
|
JPanel controlPanel = new JPanel(new GridLayout(1, 4, 10, 10));
|
||||||
|
controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||||
|
|
||||||
|
// 字体大小选择
|
||||||
|
controlPanel.add(new JLabel("字体大小:"));
|
||||||
|
fontSizeCombo = new JComboBox<>(new Integer[]{8, 10, 12, 14, 16, 18, 20, 24});
|
||||||
|
fontSizeCombo.setSelectedItem(14);
|
||||||
|
controlPanel.add(fontSizeCombo);
|
||||||
|
|
||||||
|
// 示例选择
|
||||||
|
controlPanel.add(new JLabel("示例文本:"));
|
||||||
|
exampleCombo = new JComboBox<>(examples);
|
||||||
|
exampleCombo.addActionListener(e -> textInput.setText(examples[exampleCombo.getSelectedIndex()]));
|
||||||
|
controlPanel.add(exampleCombo);
|
||||||
|
|
||||||
|
// 文本输入
|
||||||
|
textInput = new JTextArea(examples[0]);
|
||||||
|
textInput.setLineWrap(true);
|
||||||
|
textInput.setWrapStyleWord(true);
|
||||||
|
JScrollPane textScroll = new JScrollPane(textInput);
|
||||||
|
textScroll.setBorder(BorderFactory.createTitledBorder("输入文本"));
|
||||||
|
|
||||||
|
// 点阵显示面板
|
||||||
|
displayPanel = new DotMatrixPanel();
|
||||||
|
|
||||||
|
// 添加组件
|
||||||
|
add(controlPanel, BorderLayout.NORTH);
|
||||||
|
add(textScroll, BorderLayout.CENTER);
|
||||||
|
add(displayPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// 添加事件监听器
|
||||||
|
fontSizeCombo.addActionListener(e -> displayPanel.repaint());
|
||||||
|
textInput.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
|
||||||
|
public void changedUpdate(javax.swing.event.DocumentEvent e) { update(); }
|
||||||
|
public void removeUpdate(javax.swing.event.DocumentEvent e) { update(); }
|
||||||
|
public void insertUpdate(javax.swing.event.DocumentEvent e) { update(); }
|
||||||
|
private void update() {
|
||||||
|
displayPanel.repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DotMatrixPanel extends JPanel {
|
||||||
|
private final int panelWidth = WIDTH * SCALE;
|
||||||
|
private final int panelHeight = HEIGHT * SCALE;
|
||||||
|
|
||||||
|
public DotMatrixPanel() {
|
||||||
|
setPreferredSize(new Dimension(panelWidth, panelHeight + 50));
|
||||||
|
setBorder(BorderFactory.createTitledBorder("点阵预览 (160x80)"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
|
||||||
|
// 绘制点阵背景
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.fillRect(0, 0, panelWidth, panelHeight);
|
||||||
|
|
||||||
|
// 绘制网格
|
||||||
|
g.setColor(new Color(240, 240, 240));
|
||||||
|
for (int x = 0; x <= WIDTH; x++) {
|
||||||
|
int xPos = x * SCALE;
|
||||||
|
g.drawLine(xPos, 0, xPos, panelHeight);
|
||||||
|
}
|
||||||
|
for (int y = 0; y <= HEIGHT; y++) {
|
||||||
|
int yPos = y * SCALE;
|
||||||
|
g.drawLine(0, yPos, panelWidth, yPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制文本
|
||||||
|
String text = textInput.getText();
|
||||||
|
int fontSize = (Integer) fontSizeCombo.getSelectedItem();
|
||||||
|
Font font = new Font("黑体", Font.PLAIN, fontSize);
|
||||||
|
|
||||||
|
// 创建虚拟点阵图像
|
||||||
|
BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_BINARY);
|
||||||
|
Graphics2D imgG = img.createGraphics();
|
||||||
|
imgG.setColor(Color.WHITE);
|
||||||
|
imgG.fillRect(0, 0, WIDTH, HEIGHT);
|
||||||
|
imgG.setColor(Color.BLACK);
|
||||||
|
imgG.setFont(font);
|
||||||
|
imgG.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
// 绘制文本到虚拟点阵
|
||||||
|
FontMetrics metrics = imgG.getFontMetrics();
|
||||||
|
int lineHeight = metrics.getHeight();
|
||||||
|
int yPos = metrics.getAscent();
|
||||||
|
String[] lines = text.split("\n");
|
||||||
|
|
||||||
|
int maxLines = HEIGHT / lineHeight;
|
||||||
|
int actualLines = Math.min(lines.length, maxLines);
|
||||||
|
int totalChars = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < actualLines; i++) {
|
||||||
|
String line = lines[i];
|
||||||
|
int lineWidth = metrics.stringWidth(line);
|
||||||
|
int maxChars = WIDTH / metrics.stringWidth("字"); // 估算每行字数
|
||||||
|
|
||||||
|
// 截断超过宽度的文本
|
||||||
|
if (lineWidth > WIDTH) {
|
||||||
|
while (metrics.stringWidth(line) > WIDTH) {
|
||||||
|
line = line.substring(0, line.length() - 1);
|
||||||
|
}
|
||||||
|
line += "...";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 居中显示文本
|
||||||
|
int xPos = (WIDTH - metrics.stringWidth(line)) / 2;
|
||||||
|
imgG.drawString(line, xPos, yPos);
|
||||||
|
|
||||||
|
totalChars += line.length();
|
||||||
|
yPos += lineHeight;
|
||||||
|
|
||||||
|
if (yPos + lineHeight > HEIGHT) break;
|
||||||
|
}
|
||||||
|
imgG.dispose();
|
||||||
|
|
||||||
|
// 绘制到预览面板
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
int rgb = img.getRGB(x, y) & 0xFFFFFF;
|
||||||
|
if (rgb != 0xFFFFFF) { // 黑色像素
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.fillRect(x * SCALE, y * SCALE, SCALE, SCALE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示统计信息
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.setFont(new Font("宋体", Font.PLAIN, 12));
|
||||||
|
String info = String.format("字体大小: %dpt | 显示行数: %d/%d | 显示字数: %d",
|
||||||
|
fontSize, actualLines, maxLines, totalChars);
|
||||||
|
g.drawString(info, 10, panelHeight + 20);
|
||||||
|
|
||||||
|
String capacity = String.format("容量分析: %d×80点阵可显示%d-%d个汉字(%dpt字体)",
|
||||||
|
WIDTH,
|
||||||
|
(int)(WIDTH*HEIGHT/(fontSize*fontSize*1.2)),
|
||||||
|
(int)(WIDTH*HEIGHT/(fontSize*fontSize*0.8)),
|
||||||
|
fontSize);
|
||||||
|
g.drawString(capacity, 10, panelHeight + 35);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
DotMatrixDisplaySimulator frame = new DotMatrixDisplaySimulator();
|
||||||
|
frame.setVisible(true);
|
||||||
|
|
||||||
|
// 显示使用提示
|
||||||
|
String message = "<html><div style='text-align:center;'><b>160×80点阵显示屏文字容量分析</b><br><br>"
|
||||||
|
+ "• 使用上方下拉菜单选择字体大小和示例文本<br>"
|
||||||
|
+ "• 在文本区域输入自定义内容<br>"
|
||||||
|
+ "• 点阵预览区域实时显示效果<br><br>"
|
||||||
|
+ "<b>容量参考:</b><br>"
|
||||||
|
+ "8-9pt:约20字/行 × 10行 = 200字<br>"
|
||||||
|
+ "10-11pt:约16字/行 × 8行 = 128字<br>"
|
||||||
|
+ "12-13pt:约13字/行 × 6行 = 78字<br>"
|
||||||
|
+ "14-15pt:约11字/行 × 5行 = 55字<br>"
|
||||||
|
+ "16-18pt:约9字/行 × 4行 = 36字<br>"
|
||||||
|
+ "20-24pt:约7字/行 × 3行 = 21字</div></html>";
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(frame, message, "使用说明", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,185 @@
|
|||||||
|
package com.fuyuanshen.equipment.utils.c;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TextToDotMatrix {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String text = "现场危险,停止救援,\n紧急撤离至安全区域!";
|
||||||
|
int width = 160; // 点阵宽度
|
||||||
|
int height = 80; // 点阵高度
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 创建点阵图片
|
||||||
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
|
||||||
|
Graphics2D g = image.createGraphics();
|
||||||
|
|
||||||
|
// 设置背景为白色
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.fillRect(0, 0, width, height);
|
||||||
|
|
||||||
|
// 设置字体并居中显示文本
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
// 自动调整字体大小以适应区域
|
||||||
|
Font font = findBestFitFont(text, width, height, g);
|
||||||
|
g.setFont(font);
|
||||||
|
|
||||||
|
// 计算文本位置并绘制
|
||||||
|
drawCenteredText(g, text, width, height);
|
||||||
|
g.dispose();
|
||||||
|
|
||||||
|
// 2. 生成二进制数组
|
||||||
|
List<Byte> byteList = new ArrayList<>();
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x += 8) {
|
||||||
|
byte b = 0;
|
||||||
|
for (int bit = 0; bit < 8; bit++) {
|
||||||
|
int xPos = x + bit;
|
||||||
|
if (xPos < width) {
|
||||||
|
int rgb = image.getRGB(xPos, y) & 0xFFFFFF;
|
||||||
|
if (rgb != 0xFFFFFF) { // 黑色像素
|
||||||
|
b |= (1 << (7 - bit));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byteList.add(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换为字节数组
|
||||||
|
byte[] dotMatrixData = new byte[byteList.size()];
|
||||||
|
for (int i = 0; i < byteList.size(); i++) {
|
||||||
|
dotMatrixData[i] = byteList.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 保存预览图片
|
||||||
|
ImageIO.write(image, "png", new File("warning_display.png"));
|
||||||
|
System.out.println("预览图片已生成: warning_display.png");
|
||||||
|
|
||||||
|
// 4. 打印点阵数据信息
|
||||||
|
System.out.println("\n点阵数据信息:");
|
||||||
|
System.out.println("尺寸: " + width + "x" + height + " 像素");
|
||||||
|
System.out.println("数据大小: " + dotMatrixData.length + " 字节");
|
||||||
|
System.out.println("每行字节数: " + (width / 8));
|
||||||
|
System.out.println("总行数: " + height);
|
||||||
|
|
||||||
|
// 5. 打印二进制数组(十六进制格式)
|
||||||
|
System.out.println("\n点阵数据字节数组 (HEX):");
|
||||||
|
System.out.print("byte[] dotMatrixData = {");
|
||||||
|
for (int i = 0; i < dotMatrixData.length; i++) {
|
||||||
|
System.out.printf("0x%02X", dotMatrixData[i] & 0xFF);
|
||||||
|
if (i < dotMatrixData.length - 1) System.out.print(", ");
|
||||||
|
if (i % 16 == 15) System.out.println();
|
||||||
|
}
|
||||||
|
System.out.println("};\n");
|
||||||
|
|
||||||
|
// 6. 打印点阵图预览
|
||||||
|
System.out.println("点阵图预览 (缩小版):");
|
||||||
|
printTextPreview(image);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自动寻找最佳字体大小
|
||||||
|
private static Font findBestFitFont(String text, int width, int height, Graphics2D g) {
|
||||||
|
int fontSize = 20; // 初始字体大小
|
||||||
|
Font bestFont = null;
|
||||||
|
int bestHeight = 0;
|
||||||
|
|
||||||
|
while (fontSize > 8) {
|
||||||
|
Font font = new Font("黑体", Font.BOLD, fontSize);
|
||||||
|
g.setFont(font);
|
||||||
|
FontMetrics metrics = g.getFontMetrics();
|
||||||
|
|
||||||
|
// 计算文本所需高度
|
||||||
|
String[] lines = text.split("\n");
|
||||||
|
int textHeight = metrics.getHeight() * lines.length;
|
||||||
|
|
||||||
|
// 检查是否超出高度
|
||||||
|
if (textHeight < height * 0.8) {
|
||||||
|
bestFont = font;
|
||||||
|
bestHeight = textHeight;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fontSize--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有找到合适字体,使用最小字体
|
||||||
|
if (bestFont == null) {
|
||||||
|
bestFont = new Font("黑体", Font.BOLD, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("使用字体: " + bestFont.getSize() + "pt");
|
||||||
|
return bestFont;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 居中绘制文本
|
||||||
|
private static void drawCenteredText(Graphics g, String text, int width, int height) {
|
||||||
|
FontMetrics metrics = g.getFontMetrics();
|
||||||
|
String[] lines = text.split("\n");
|
||||||
|
|
||||||
|
// 计算总文本高度
|
||||||
|
int totalHeight = metrics.getHeight() * lines.length;
|
||||||
|
|
||||||
|
// 计算起始Y位置
|
||||||
|
int y = (height - totalHeight) / 2 + metrics.getAscent();
|
||||||
|
|
||||||
|
for (String line : lines) {
|
||||||
|
// 计算X位置使文本居中
|
||||||
|
int x = (width - metrics.stringWidth(line)) / 2;
|
||||||
|
g.drawString(line, x, y);
|
||||||
|
y += metrics.getHeight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印文本预览
|
||||||
|
private static void printTextPreview(BufferedImage image) {
|
||||||
|
int width = image.getWidth();
|
||||||
|
int height = image.getHeight();
|
||||||
|
|
||||||
|
// 缩小预览比例
|
||||||
|
int scale = 4;
|
||||||
|
int previewWidth = width / scale;
|
||||||
|
int previewHeight = height / scale;
|
||||||
|
|
||||||
|
for (int y = 0; y < previewHeight; y++) {
|
||||||
|
for (int x = 0; x < previewWidth; x++) {
|
||||||
|
int pixelCount = 0;
|
||||||
|
for (int dy = 0; dy < scale; dy++) {
|
||||||
|
for (int dx = 0; dx < scale; dx++) {
|
||||||
|
int origX = x * scale + dx;
|
||||||
|
int origY = y * scale + dy;
|
||||||
|
if (origX < width && origY < height) {
|
||||||
|
int rgb = image.getRGB(origX, origY) & 0xFFFFFF;
|
||||||
|
if (rgb != 0xFFFFFF) { // 黑色像素
|
||||||
|
pixelCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据黑色像素密度选择字符
|
||||||
|
if (pixelCount > scale * scale * 0.7) {
|
||||||
|
System.out.print("██");
|
||||||
|
} else if (pixelCount > scale * scale * 0.4) {
|
||||||
|
System.out.print("▓▓");
|
||||||
|
} else if (pixelCount > scale * scale * 0.1) {
|
||||||
|
System.out.print("░░");
|
||||||
|
} else {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user