GraphicsGenerationUtil.java
3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.pipihelper.project.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.List;
/**
* @Description: TODO
* @author: charles
* @date: 2022年10月15日 16:12
*/
public class GraphicsGenerationUtil {
/**
* 生成图片
* @param data 以二维数组形式存放 表格里面的值
*/
public static BufferedImage graphicsGeneration(List<List<String>> data) {
// 字体大小
int fontTitileSize = 15;
// 横线的行数
int totalrow = data.size()+1;
// 竖线的行数
int totalcol = 0;
if (data.get(0) != null) {
totalcol = data.get(0).size();
}
// 图片宽度
int imageWidth = 750;
// 行高
int rowheight = 40;
// 图片高度
int imageHeight = totalrow*rowheight+50;
// 起始高度
int startHeight = 10;
// 起始宽度
int startWidth = 10;
// 单元格宽度
int colwidth = (int)((imageWidth-20)/totalcol);
BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0, imageWidth, imageHeight);
graphics.setColor(new Color(220,240,240));
//画横线
for(int j=0;j<totalrow; j++){
graphics.setColor(Color.black);
graphics.drawLine(startWidth, startHeight+(j+1)*rowheight, startWidth+colwidth*totalcol, startHeight+(j+1)*rowheight);
}
//画竖线
for(int k=0;k<totalcol+1;k++){
graphics.setColor(Color.black);
graphics.drawLine(startWidth+k*colwidth, startHeight+rowheight, startWidth+k*colwidth, startHeight+rowheight*totalrow);
}
//设置字体
Font font = new Font("微软雅黑",Font.BOLD,fontTitileSize);
graphics.setFont(font);
//写标题
String title = "【按摩名单】";
graphics.drawString(title, startWidth, startHeight+rowheight-10);
//写入内容
for(int n=0;n< data.size();n++){
for(int l = 0; l< data.get(n).size(); l++){
if (n == 0) {
font = new Font("微软雅黑",Font.BOLD,fontTitileSize);
graphics.setFont(font);
}else if (n > 0 && l >0) {
font = new Font("微软雅黑",Font.PLAIN,fontTitileSize);
graphics.setFont(font);
graphics.setColor(Color.RED);
} else {
font = new Font("微软雅黑",Font.PLAIN,fontTitileSize);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
}
graphics.drawString(data.get(n).get(l).toString(), startWidth+colwidth*l+5, startHeight+rowheight*(n+2)-10);
}
}
return image;
}
/**
* 将图片保存到指定位置
* @param data 表格数据
* @param fileLocation 文件位置
*/
public static void createImage(List<List<String>> data, String fileLocation) {
BufferedImage image = graphicsGeneration(data);
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
ImageIO.write(image,"png", fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将图片转byte
* @param data 表格数据
*/
public static byte[] bufferedImageToByte(List<List<String>> data) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedImage image = graphicsGeneration(data);
try {
ImageIO.write(image,"png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
return null;
}
}
}