修改绘图算法
parent
0790d48df2
commit
70de0ffc14
|
|
@ -14,6 +14,7 @@ import java.text.SimpleDateFormat;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 考勤数据绘制类
|
||||
|
|
@ -24,7 +25,6 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
String ledSavePath=ledProperties.getSavePath()+ledScreen.getDeviceSn()+"/";
|
||||
//检查目录及子目录是否已存在,不存在创建目
|
||||
LedFileUtils.createDir(ledSavePath);
|
||||
|
||||
//获取在场人数
|
||||
long count = attendanceDataList.stream().filter(d -> d.getOutDate() == null).count();
|
||||
//日志打印在场人数
|
||||
|
|
@ -36,17 +36,35 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
List<String> imagePaths = new ArrayList<>();
|
||||
//根据dataLine来对list进行分页,调用绘制方法
|
||||
if (!attendanceDataList.isEmpty() && dataLine > 0) { // 检查列表不为空且dataLine大于0
|
||||
//按进场时间排序
|
||||
attendanceDataList=attendanceDataList.stream()
|
||||
.sorted((a,b) -> {
|
||||
if (a.getInDate() == null && b.getInDate() == null) return 0;
|
||||
if (a.getInDate() == null) return 1;
|
||||
if (b.getInDate() == null) return -1;
|
||||
return a.getInDate().compareTo(b.getInDate());
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
int totalPages = (int) Math.ceil((double) attendanceDataList.size() / dataLine);
|
||||
for (int i = 0; i < attendanceDataList.size(); i += dataLine) {
|
||||
List<UniAttendanceStartEndInfo> subList = attendanceDataList.subList(i, Math.min(i + dataLine, attendanceDataList.size()));
|
||||
String imagePath=ledSavePath + "image" + (i / dataLine + 1) + ".bmp";
|
||||
drawImage(ledScreen, subList, imagePath, dataLine, count);
|
||||
int currentPage = i / dataLine + 1;
|
||||
drawImage(ledScreen, subList, imagePath, dataLine, count, currentPage, totalPages);
|
||||
imagePaths.add(imagePath);
|
||||
}
|
||||
}
|
||||
return imagePaths;
|
||||
}
|
||||
|
||||
private static void drawImage(SysLedscreen ledScreen, List<UniAttendanceStartEndInfo> subList, String imagePath, int maxRows, long count) {
|
||||
private static void drawStringByChar(Graphics2D g2d, String text, int x, int y, FontMetrics fm) {
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
g2d.drawString(text.substring(i, i + 1), x, y);
|
||||
x += fm.charWidth(text.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawImage(SysLedscreen ledScreen, List<UniAttendanceStartEndInfo> subList, String imagePath, int maxRows, long count, int currentPage, int totalPages) {
|
||||
int screenWidth=ledScreen.getWidth().intValue();
|
||||
int screenHeight=ledScreen.getHeight().intValue();
|
||||
int lineHeight = 18;
|
||||
|
|
@ -61,10 +79,19 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
|
||||
int imageWidth = screenWidth;
|
||||
|
||||
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_BYTE_BINARY);
|
||||
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_BYTE_GRAY);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
|
||||
try {
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
// g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
|
||||
|
||||
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
|
||||
|
|
@ -74,31 +101,37 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
g2d.setColor(new Color(0, 0, 0));
|
||||
g2d.fillRect(0, 0, imageWidth, imageHeight);
|
||||
|
||||
Font font = new Font("宋体", Font.PLAIN, 13);
|
||||
Font font = new Font("微软雅黑", Font.PLAIN, 13);
|
||||
g2d.setFont(font);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
|
||||
FontMetrics fm = g2d.getFontMetrics();
|
||||
String[] headers = {"姓名", "班组", "进入时间", "离开时间"};
|
||||
int[] columnWidths = {drawWidth / 5, drawWidth * 3 / 10, drawWidth / 4, drawWidth / 4};
|
||||
|
||||
int currentY = margin;
|
||||
g2d.drawString("当前在场人数:" + count + "人", margin + 2, currentY + countRowHeight - 4);
|
||||
currentY += countRowHeight;
|
||||
drawStringByChar(g2d, "当前在场人数:" + count + "人", 2, 14, fm);
|
||||
|
||||
String pageText = currentPage + "/" + totalPages;
|
||||
int pageTextWidth = fm.stringWidth(pageText);
|
||||
drawStringByChar(g2d, pageText, screenWidth - pageTextWidth - 2, 14, fm);
|
||||
|
||||
int currentY = margin + countRowHeight;
|
||||
|
||||
int x = margin;
|
||||
int xPos = margin;
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
g2d.drawString(headers[i], x + 2, currentY + headerHeight - 4);
|
||||
x += columnWidths[i];
|
||||
drawStringByChar(g2d, headers[i], xPos + 2, currentY + headerHeight - 4, fm);
|
||||
xPos += columnWidths[i];
|
||||
}
|
||||
|
||||
currentY += headerHeight;
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
g2d.drawLine(margin, currentY, margin + drawWidth, currentY);
|
||||
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
for (int i = 0; i < maxRows; i++) {
|
||||
int rowY = currentY + i * rowHeight;
|
||||
int lineY = rowY + rowHeight;
|
||||
|
|
@ -125,30 +158,21 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
outTime = sdf.format(data.getOutDate());
|
||||
}
|
||||
|
||||
x = margin;
|
||||
g2d.drawString(userName, x + 2, rowY + rowHeight - 4);
|
||||
int x = margin;
|
||||
drawStringByChar(g2d, userName, x + 2, rowY + rowHeight - 4, fm);
|
||||
x += columnWidths[0];
|
||||
g2d.drawString(subDeptGroupName, x + 2, rowY + rowHeight - 4);
|
||||
drawStringByChar(g2d, subDeptGroupName, x + 2, rowY + rowHeight - 4, fm);
|
||||
x += columnWidths[1];
|
||||
g2d.drawString(inTime, x + 2, rowY + rowHeight - 4);
|
||||
drawStringByChar(g2d, inTime, x + 2, rowY + rowHeight - 4, fm);
|
||||
x += columnWidths[2];
|
||||
g2d.drawString(outTime, x + 2, rowY + rowHeight - 4);
|
||||
} else {
|
||||
x = margin;
|
||||
g2d.drawString("", x + 2, rowY + rowHeight - 4);
|
||||
x += columnWidths[0];
|
||||
g2d.drawString("", x + 2, rowY + rowHeight - 4);
|
||||
x += columnWidths[1];
|
||||
g2d.drawString("", x + 2, rowY + rowHeight - 4);
|
||||
x += columnWidths[2];
|
||||
g2d.drawString("", x + 2, rowY + rowHeight - 4);
|
||||
drawStringByChar(g2d, outTime, x + 2, rowY + rowHeight - 4, fm);
|
||||
}
|
||||
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
g2d.drawLine(margin, lineY, margin + drawWidth, lineY);
|
||||
}
|
||||
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
int x1 = margin + columnWidths[0];
|
||||
int x2 = margin + columnWidths[0] + columnWidths[1];
|
||||
int x3 = margin + columnWidths[0] + columnWidths[1] + columnWidths[2];
|
||||
|
|
@ -156,7 +180,7 @@ public class UniAttendanceStartAndEndDrawer {
|
|||
g2d.drawLine(x2, margin + countRowHeight, x2, margin + totalHeight);
|
||||
g2d.drawLine(x3, margin + countRowHeight, x3, margin + totalHeight);
|
||||
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setColor(new Color(255, 255, 255));
|
||||
g2d.drawRect(margin, margin + countRowHeight, drawWidth, totalHeight - countRowHeight);
|
||||
|
||||
File outputFile = new File(imagePath);
|
||||
|
|
|
|||
Loading…
Reference in New Issue