修改LED绘图
parent
ad8d77576d
commit
a505e17a3d
|
|
@ -3,6 +3,7 @@ package com.yanzhu.led.service.impl;
|
||||||
import com.yanzhu.led.config.LedProperties;
|
import com.yanzhu.led.config.LedProperties;
|
||||||
import com.yanzhu.led.service.ILedScreenService;
|
import com.yanzhu.led.service.ILedScreenService;
|
||||||
import com.yanzhu.led.utils.LedFileUtils;
|
import com.yanzhu.led.utils.LedFileUtils;
|
||||||
|
import com.yanzhu.led.utils.Uni2LedDrawer;
|
||||||
import com.yanzhu.led.utils.UniLedDrawer;
|
import com.yanzhu.led.utils.UniLedDrawer;
|
||||||
import com.yanzhu.manage.domain.ProMobileAttendanceData;
|
import com.yanzhu.manage.domain.ProMobileAttendanceData;
|
||||||
import com.yanzhu.manage.mapper.ProMobileAttendanceDataMapper;
|
import com.yanzhu.manage.mapper.ProMobileAttendanceDataMapper;
|
||||||
|
|
@ -140,6 +141,9 @@ public class LedScreenServiceImpl implements ILedScreenService {
|
||||||
// 获取考勤数据
|
// 获取考勤数据
|
||||||
List<ProMobileAttendanceData> attendanceDataList = getAttendanceDataByWorkAreaId(ledScreen);
|
List<ProMobileAttendanceData> attendanceDataList = getAttendanceDataByWorkAreaId(ledScreen);
|
||||||
return UniLedDrawer.drawLedScreen(ledScreen, ledProperties, attendanceDataList);
|
return UniLedDrawer.drawLedScreen(ledScreen, ledProperties, attendanceDataList);
|
||||||
|
}else if (ledScreen.getDrawType().intValue()==2){
|
||||||
|
List<ProMobileAttendanceData> attendanceDataList = getAttendanceDataByWorkAreaId(ledScreen);
|
||||||
|
return Uni2LedDrawer.drawLedScreen(ledScreen, ledProperties, attendanceDataList);
|
||||||
}
|
}
|
||||||
return new String[]{}; // 实际使用时应从配置或数据库获取
|
return new String[]{}; // 实际使用时应从配置或数据库获取
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,160 @@
|
||||||
|
package com.yanzhu.led.utils;
|
||||||
|
|
||||||
|
import com.yanzhu.led.config.LedProperties;
|
||||||
|
import com.yanzhu.manage.domain.ProMobileAttendanceData;
|
||||||
|
import com.yanzhu.system.domain.SysLedscreen;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Uni2LedDrawer extends BaseDrawer{
|
||||||
|
|
||||||
|
|
||||||
|
public static String[] drawLedScreen(SysLedscreen ledScreen, LedProperties ledProperties, List<ProMobileAttendanceData> attendanceDataList) {
|
||||||
|
String ledSavePath=ledProperties.getSavePath()+ledScreen.getDeviceSn()+"/";
|
||||||
|
//检查目录及子目录是否已存在,不存在创建目
|
||||||
|
LedFileUtils.createDir(ledSavePath);
|
||||||
|
|
||||||
|
|
||||||
|
int screenHeight=ledScreen.getHeight().intValue();
|
||||||
|
int dataLine = Math.max(3, (screenHeight-32)/16-1); // 确保dataLine至少为1
|
||||||
|
List<String> imagePaths = new ArrayList<>();
|
||||||
|
//根据dataLine来对list进行分页,调用绘制方法
|
||||||
|
if (!attendanceDataList.isEmpty() && dataLine > 0) { // 检查列表不为空且dataLine大于0
|
||||||
|
for (int i = 0; i < attendanceDataList.size(); i += dataLine) {
|
||||||
|
List<ProMobileAttendanceData> subList = attendanceDataList.subList(i, Math.min(i + dataLine, attendanceDataList.size()));
|
||||||
|
String imagePath=ledSavePath + "image" + (i / dataLine + 1) + ".bmp";
|
||||||
|
drawImage(ledScreen, subList, imagePath);
|
||||||
|
imagePaths.add(imagePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
imagePaths.clear();
|
||||||
|
return imagePaths.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void drawImage(SysLedscreen ledScreen, List<ProMobileAttendanceData> subList, String imagePath) {
|
||||||
|
int screenWidth=ledScreen.getWidth().intValue();
|
||||||
|
int lineHeight = 16;
|
||||||
|
int headerHeight = lineHeight;
|
||||||
|
int rowHeight = lineHeight;
|
||||||
|
|
||||||
|
int margin = 3;
|
||||||
|
int drawWidth = screenWidth - 2 * margin;
|
||||||
|
int totalHeight = headerHeight + subList.size() * rowHeight;
|
||||||
|
|
||||||
|
int imageWidth = screenWidth;
|
||||||
|
int imageHeight = totalHeight + 2 * margin;
|
||||||
|
|
||||||
|
// 创建4位16色的BMP图片
|
||||||
|
BufferedImage image = create4BitBmpImage(imageWidth, imageHeight);
|
||||||
|
Graphics2D g2d = image.createGraphics();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 设置背景色为黑色
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
g2d.fillRect(0, 0, imageWidth, imageHeight);
|
||||||
|
|
||||||
|
// 设置字体
|
||||||
|
Font font = new Font("宋体", Font.PLAIN, 12);
|
||||||
|
g2d.setFont(font);
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
// 设置文字颜色为白色
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
|
||||||
|
// 表头:姓名 班组 进入时间
|
||||||
|
String[] headers = {"姓名", "班组", "进入时间"};
|
||||||
|
int[] columnWidths = {drawWidth / 3, drawWidth / 3, drawWidth / 3};
|
||||||
|
|
||||||
|
// 绘制表头
|
||||||
|
int x = margin;
|
||||||
|
for (int i = 0; i < headers.length; i++) {
|
||||||
|
g2d.drawString(headers[i], x + 2, margin + headerHeight - 4);
|
||||||
|
x += columnWidths[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制表头下划线
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
g2d.drawLine(margin, margin + headerHeight, margin + drawWidth, margin + headerHeight);
|
||||||
|
|
||||||
|
// 绘制数据行
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
for (int i = 0; i < subList.size(); i++) {
|
||||||
|
ProMobileAttendanceData data = subList.get(i);
|
||||||
|
int rowY = margin + headerHeight + i * rowHeight;
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
String userName = data.getUserName() != null ? data.getUserName() : "";
|
||||||
|
String subDeptGroupName = data.getSubDeptGroupName() != null ? data.getSubDeptGroupName() : "";
|
||||||
|
|
||||||
|
// 格式化考勤时间
|
||||||
|
String attTime = "";
|
||||||
|
if (data.getAttDate() != null) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
||||||
|
attTime = sdf.format(data.getAttDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制数据行
|
||||||
|
x = margin;
|
||||||
|
g2d.drawString(userName, x + 2, rowY + rowHeight - 4);
|
||||||
|
x += columnWidths[0];
|
||||||
|
g2d.drawString(subDeptGroupName, x + 2, rowY + rowHeight - 4);
|
||||||
|
x += columnWidths[1];
|
||||||
|
g2d.drawString(attTime, x + 2, rowY + rowHeight - 4);
|
||||||
|
|
||||||
|
// 绘制行下划线
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
g2d.drawLine(margin, rowY + rowHeight, margin + drawWidth, rowY + rowHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制垂直分割线
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
int x1 = margin + columnWidths[0];
|
||||||
|
int x2 = margin + columnWidths[0] + columnWidths[1];
|
||||||
|
g2d.drawLine(x1, margin, x1, margin + totalHeight);
|
||||||
|
g2d.drawLine(x2, margin, x2, margin + totalHeight);
|
||||||
|
|
||||||
|
// 绘制表格外边框
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
g2d.drawRect(margin, margin, drawWidth, totalHeight);
|
||||||
|
|
||||||
|
// 保存图片为BMP格式
|
||||||
|
File outputFile = new File(imagePath);
|
||||||
|
ImageIO.write(image, "bmp", outputFile);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
g2d.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SysLedscreen led=new SysLedscreen();
|
||||||
|
led.setWidth(256l);
|
||||||
|
led.setHeight(160l);
|
||||||
|
led.setDeviceSn("B06M3P2501160132");
|
||||||
|
led.setTitle("abc");
|
||||||
|
LedProperties ledProperties=new LedProperties();
|
||||||
|
List<ProMobileAttendanceData> attendanceDataList =
|
||||||
|
Arrays.stream("1,2,3,4,5,6,7,8,9,10".split(",")).map(id -> {
|
||||||
|
ProMobileAttendanceData d=new ProMobileAttendanceData();
|
||||||
|
d.setId(Long.parseLong(id));
|
||||||
|
d.setUserName("张三李四");
|
||||||
|
d.setSubDeptGroupName("爆破组");
|
||||||
|
d.setAttDate(new Date());
|
||||||
|
return d;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
drawLedScreen(led, ledProperties, attendanceDataList);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -47,11 +47,12 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
int lineHeight = 16;
|
int lineHeight = 16;
|
||||||
int headerHeight = lineHeight;
|
int headerHeight = lineHeight;
|
||||||
int rowHeight = lineHeight;
|
int rowHeight = lineHeight;
|
||||||
|
|
||||||
|
int margin = 3;
|
||||||
|
int drawWidth = screenWidth - 2 * margin;
|
||||||
int totalHeight = headerHeight + subList.size() * rowHeight;
|
int totalHeight = headerHeight + subList.size() * rowHeight;
|
||||||
|
|
||||||
// 添加边距
|
int imageWidth = screenWidth;
|
||||||
int margin = 3;
|
|
||||||
int imageWidth = screenWidth + 2 * margin;
|
|
||||||
int imageHeight = totalHeight + 2 * margin;
|
int imageHeight = totalHeight + 2 * margin;
|
||||||
|
|
||||||
// 创建4位16色的BMP图片
|
// 创建4位16色的BMP图片
|
||||||
|
|
@ -73,20 +74,20 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
|
|
||||||
// 表头:姓名 班组 进入时间
|
// 表头:姓名 班组 进入时间
|
||||||
String[] headers = {"姓名", "班组", "进入时间"};
|
String[] headers = {"姓名", "班组", "进入时间"};
|
||||||
int[] columnWidths = {screenWidth / 3, screenWidth / 3, screenWidth / 3};
|
int[] columnWidths = {drawWidth / 3, drawWidth / 3, drawWidth / 3};
|
||||||
|
|
||||||
// 绘制表头,考虑左边距
|
// 绘制表头
|
||||||
int x = margin;
|
int x = margin;
|
||||||
for (int i = 0; i < headers.length; i++) {
|
for (int i = 0; i < headers.length; i++) {
|
||||||
g2d.drawString(headers[i], x + 2, margin + headerHeight - 4);
|
g2d.drawString(headers[i], x + 2, margin + headerHeight - 4);
|
||||||
x += columnWidths[i];
|
x += columnWidths[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制表头下划线,考虑边距
|
// 绘制表头下划线
|
||||||
g2d.setColor(Color.WHITE);
|
g2d.setColor(Color.WHITE);
|
||||||
g2d.drawLine(margin, margin + headerHeight, margin + screenWidth, margin + headerHeight);
|
g2d.drawLine(margin, margin + headerHeight, margin + drawWidth, margin + headerHeight);
|
||||||
|
|
||||||
// 绘制数据行,考虑边距
|
// 绘制数据行
|
||||||
g2d.setColor(Color.WHITE);
|
g2d.setColor(Color.WHITE);
|
||||||
for (int i = 0; i < subList.size(); i++) {
|
for (int i = 0; i < subList.size(); i++) {
|
||||||
ProMobileAttendanceData data = subList.get(i);
|
ProMobileAttendanceData data = subList.get(i);
|
||||||
|
|
@ -103,7 +104,7 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
attTime = sdf.format(data.getAttDate());
|
attTime = sdf.format(data.getAttDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制数据行,考虑左边距
|
// 绘制数据行
|
||||||
x = margin;
|
x = margin;
|
||||||
g2d.drawString(userName, x + 2, rowY + rowHeight - 4);
|
g2d.drawString(userName, x + 2, rowY + rowHeight - 4);
|
||||||
x += columnWidths[0];
|
x += columnWidths[0];
|
||||||
|
|
@ -111,12 +112,12 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
x += columnWidths[1];
|
x += columnWidths[1];
|
||||||
g2d.drawString(attTime, x + 2, rowY + rowHeight - 4);
|
g2d.drawString(attTime, x + 2, rowY + rowHeight - 4);
|
||||||
|
|
||||||
// 绘制行下划线,考虑边距
|
// 绘制行下划线
|
||||||
g2d.setColor(Color.WHITE);
|
g2d.setColor(Color.WHITE);
|
||||||
g2d.drawLine(margin, rowY + rowHeight, margin + screenWidth, rowY + rowHeight);
|
g2d.drawLine(margin, rowY + rowHeight, margin + drawWidth, rowY + rowHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制垂直分割线,考虑边距
|
// 绘制垂直分割线
|
||||||
g2d.setColor(Color.WHITE);
|
g2d.setColor(Color.WHITE);
|
||||||
int x1 = margin + columnWidths[0];
|
int x1 = margin + columnWidths[0];
|
||||||
int x2 = margin + columnWidths[0] + columnWidths[1];
|
int x2 = margin + columnWidths[0] + columnWidths[1];
|
||||||
|
|
@ -125,7 +126,7 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
|
|
||||||
// 绘制表格外边框
|
// 绘制表格外边框
|
||||||
g2d.setColor(Color.WHITE);
|
g2d.setColor(Color.WHITE);
|
||||||
g2d.drawRect(margin, margin, screenWidth, totalHeight);
|
g2d.drawRect(margin, margin, drawWidth, totalHeight);
|
||||||
|
|
||||||
// 保存图片为BMP格式
|
// 保存图片为BMP格式
|
||||||
File outputFile = new File(imagePath);
|
File outputFile = new File(imagePath);
|
||||||
|
|
@ -140,7 +141,7 @@ public class UniLedDrawer extends BaseDrawer{
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SysLedscreen led=new SysLedscreen();
|
SysLedscreen led=new SysLedscreen();
|
||||||
led.setWidth(230l);
|
led.setWidth(192l);
|
||||||
led.setHeight(128l);
|
led.setHeight(128l);
|
||||||
led.setDeviceSn("B06M3P2501160132");
|
led.setDeviceSn("B06M3P2501160132");
|
||||||
led.setTitle("abc");
|
led.setTitle("abc");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue