• java图片生成


    标题,列头,背景颜色,合并单元格,先看图片效果:

     

    1. package cn.com.alerts.util;
    2. import sun.misc.BASE64Encoder;
    3. import javax.imageio.ImageIO;
    4. import java.awt.*;
    5. import java.awt.image.BufferedImage;
    6. import java.io.BufferedOutputStream;
    7. import java.io.ByteArrayOutputStream;
    8. import java.io.File;
    9. import java.io.FileOutputStream;
    10. import java.text.SimpleDateFormat;
    11. import java.util.Date;
    12. public class BuildImgsUtil {
    13. public final static BuildImgsUtil pictureUtil = new BuildImgsUtil();
    14. private BuildImgsUtil() {
    15. }
    16. public static BuildImgsUtil getInstance() {
    17. return pictureUtil;
    18. }
    19. /**
    20. * 通过传入的数据,进行图片生成。
    21. * @param
    22. * @return
    23. */
    24. public void createImagesByDefineData(double[] colWidthPercent, String firstTitle, String twoTitle, String[][] tableData, String filePath, String fileName) throws Exception{
    25. myGraphicsGenerationDefineData(colWidthPercent, firstTitle, twoTitle, tableData, filePath, fileName);
    26. }
    27. public void myGraphicsGenerationDefineData(double[] colWidthPercent, String firstTitle, String twoTitle, String cellsValue[][], String path, String fileName) throws Exception{
    28. // 字体大小
    29. int fontTitileSize = 15;
    30. // 横线的行数
    31. int totalrow = cellsValue.length + 1;
    32. // 竖线的行数
    33. int totalcol = 0;
    34. if (cellsValue[0] != null) {
    35. totalcol = cellsValue[0].length;
    36. }
    37. // 图片宽度
    38. int imageWidth = totalcol *260;
    39. // 行高
    40. int rowheight = 40;
    41. // 图片高度
    42. int imageHeight = totalrow * rowheight + 30;
    43. // 起始高度
    44. int startHeight = 20;
    45. // 起始宽度
    46. int startWidth = 10;
    47. // 单元格宽度
    48. int colwidth = (int) ((imageWidth - 20) / totalcol);
    49. BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    50. Graphics graphics = image.getGraphics();
    51. graphics.setColor(Color.WHITE);
    52. graphics.fillRect(0, 0, imageWidth, imageHeight);
    53. graphics.setColor(new Color(220, 240, 240));
    54. // 表头标题,背景填充
    55. int rowWidth = imageWidth;
    56. graphics.setColor(new Color(241, 239, 242));
    57. graphics.fillRect(startWidth+1, 60, rowWidth-20, rowheight);
    58. graphics.setColor(Color.black);
    59. //画顶部的横线
    60. graphics.drawLine(startWidth,1 , startWidth + colwidth * totalcol, 1 );
    61. //画顶部左边的坚线
    62. graphics.drawLine(startWidth, 1, startWidth, startHeight + rowheight );
    63. //画顶部右边的坚线
    64. graphics.drawLine(startWidth+ colwidth * totalcol, 1, startWidth+ colwidth * totalcol, startHeight + rowheight );
    65. //画横线
    66. for (int j = 0; j < totalrow; j++) {
    67. graphics.setColor(Color.black);
    68. graphics.drawLine(startWidth,
    69. startHeight + (j + 1) * rowheight,
    70. startWidth + colwidth * totalcol,
    71. startHeight + (j + 1) * rowheight);
    72. }
    73. // 竖线位置坐标
    74. int[] colLineWidth = new int[colWidthPercent.length];
    75. int startWidthInt = startWidth;
    76. for(int i = 1; i < colWidthPercent.length + 1; i++){
    77. startWidthInt += (int)(imageWidth * colWidthPercent[i - 1]);
    78. // 防止超出
    79. if(startWidthInt > imageWidth){
    80. startWidthInt = imageWidth - startWidth;
    81. }
    82. colLineWidth[i-1] = startWidthInt;
    83. }
    84. //画竖线
    85. for (int k = 0; k < colWidthPercent.length; k++) {
    86. graphics.setColor(Color.black);
    87. graphics.drawLine(colLineWidth[k], startHeight + rowheight, colLineWidth[k], startHeight + rowheight * totalrow);
    88. }
    89. graphics.setColor(Color.black);
    90. //设置字体
    91. Font font = new Font("微软雅黑", Font.BOLD, fontTitileSize);
    92. graphics.setFont(font);
    93. //写标题
    94. // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    95. // String title02 = "生成时间:" + simpleDateFormat.format(new Date());
    96. graphics.drawString(twoTitle, startWidth+5, startHeight + rowheight - 18);
    97. Font fonttile = new Font("微软雅黑", Font.BOLD, 18);
    98. graphics.setFont(fonttile);
    99. //写第一标题
    100. graphics.drawString(firstTitle, startWidth, 18);
    101. //写第二个标题
    102. //graphics.drawString(twoTitle, 790, 60);
    103. //写入内容
    104. for (int n = 0; n < cellsValue.length; n++) {
    105. for (int h = 0; h < cellsValue[n].length; h++) {
    106. if(cellsValue[n] == null || cellsValue[n][h] == null){
    107. // 合并单元格(横着的部分)
    108. graphics.setColor(Color.white);
    109. graphics.drawLine(colLineWidth[h] + 1, startHeight + (n + 1) * rowheight, colLineWidth[h == cellsValue[n].length ? h : (h+1)] - 1, startHeight + (n + 1) * rowheight);
    110. continue;
    111. }
    112. font = new Font("微软雅黑", Font.PLAIN, fontTitileSize);
    113. graphics.setFont(font);
    114. graphics.setColor(Color.BLACK);
    115. if (h > 0) {
    116. if (!cellsValue[n][h].equals(cellsValue[n][h - 1])) {
    117. //表头第2列开始
    118. if(n==0){
    119. font = new Font("微软雅黑", Font.BOLD, fontTitileSize);
    120. graphics.setFont(font);
    121. // graphics.setColor(Color.BLUE);
    122. }
    123. graphics.drawString(cellsValue[n][h], colLineWidth[h] + 35, startHeight + rowheight * (n + 2) - 10);
    124. }
    125. } else {
    126. //表头第一列
    127. if(n==0 && h==0){
    128. font = new Font("微软雅黑", Font.BOLD, fontTitileSize);
    129. graphics.setFont(font);
    130. //graphics.setColor(Color.BLUE);
    131. }
    132. graphics.drawString(cellsValue[n][h], colLineWidth[h] + 35, startHeight + rowheight * (n + 2) - 10);
    133. }
    134. }
    135. }
    136. // 保存图片
    137. createImage(image, path, fileName);
    138. }
    139. /**
    140. * 将图片保存到指定位置
    141. *
    142. * @param image 缓冲文件类
    143. * @param fileLocation 文件位置
    144. */
    145. public static void createImage(BufferedImage image, String fileLocation, String fileName) {
    146. try {
    147. File file = new File(fileLocation);
    148. if (!file.exists()) {
    149. file.mkdir();
    150. }
    151. FileOutputStream fos = new FileOutputStream(fileLocation + fileName);
    152. BufferedOutputStream bos = new BufferedOutputStream(fos);
    153. ImageIO.write(image, "jpg", fos);
    154. /* JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
    155. encoder.encode(image);*/
    156. bos.close();
    157. } catch (Exception e) {
    158. e.printStackTrace();
    159. }
    160. }
    161. /**
    162. * 图片转换为string
    163. *
    164. * @return
    165. */
    166. public static String fileToByteArray(String filePath) throws Exception {
    167. BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    168. File file = new File(filePath);
    169. BufferedImage bi = ImageIO.read(file);
    170. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    171. ImageIO.write(bi, "jpg", baos);
    172. byte[] bytes = baos.toByteArray();
    173. return encoder.encodeBuffer(bytes).trim();
    174. }
    175. public static void main(String[] args) {
    176. String firstTitle = "xx系统";
    177. String twoTitle = "xxx系统";
    178. String[] tableTile = {"序号", "模块", "指标", "结果"};
    179. // double[] colWidthPercent = {0,0.1, 0.3, 0.3, 0.3};
    180. // String[] tableTile = { "链路","IP", "指标", "结果"};
    181. double[] colWidthPercent = {0, 0.25,0.25, 0.25, 0.25};
    182. String[][] tableData2 = new String[5][tableTile.length];
    183. for (int i = 0; i < tableTile.length; i++) {
    184. tableData2[0][i] = tableTile[i];
    185. }
    186. tableData2[1][0] = "1";
    187. tableData2[2][0] = "2";
    188. tableData2[3][0] = "3";
    189. tableData2[4][0] = "4";
    190. tableData2[1][1] = "程序";
    191. tableData2[2][1] = "数据库1";
    192. tableData2[3][1] = "数据库2";
    193. tableData2[4][1] = "基础";
    194. tableData2[1][2] = "可用性";
    195. tableData2[2][2] = "192.168.243.3.11:222";
    196. tableData2[3][2] = "可用性";
    197. tableData2[1][3] = "1000";
    198. //tableData2[2][3] = "200";
    199. try {
    200. BuildImgsUtil.getInstance().createImagesByDefineData(colWidthPercent, firstTitle, twoTitle, tableData2, "E:\\", "test.jpg");
    201. } catch (Exception e) {
    202. e.printStackTrace();
    203. }
    204. }
    205. }

  • 相关阅读:
    [入门一]C# webApi创建、与发布、部署、api调用
    连接数据库时遇到的bug1号
    20. mediasoup服务器的布署与使用
    RabbitMQ
    SLA中QPS、TP999等概念
    并查集介绍和常用模板
    人工智能|机器学习——k-近邻算法(KNN分类算法)
    设计模式13-行为型设计模式-策略设计模式
    R语言查看对象的结构:class函数、mode函数、str函数、names函数
    济南申请ISO认证的条件和要求
  • 原文地址:https://blog.csdn.net/livening/article/details/125897499