1. package org.jeecg.modules.ppl.utils;
    2. import com.google.api.client.util.Base64;
    3. import javax.imageio.ImageIO;
    4. import java.awt.*;
    5. import java.awt.geom.AffineTransform;
    6. import java.awt.image.AffineTransformOp;
    7. import java.awt.image.BufferedImage;
    8. import java.io.ByteArrayOutputStream;
    9. import java.io.DataInputStream;
    10. import java.io.File;
    11. import java.io.IOException;
    12. import java.net.URISyntaxException;
    13. import java.net.URL;
    14. import java.util.ArrayList;
    15. import java.util.List;
    16. /**
    17. * 生成组合头像
    18. */
    19. public class GenerateGroupAvatarUtil {
    20. /**
    21. * 图片格式:JPG
    22. */
    23. public static final String PICTRUE_FORMATE_PNG = "PNG";
    24. /**
    25. * 生成组合头像 (返回Base64字符串)
    26. * @param urls 用户图像
    27. * @throws IOException
    28. */
    29. public String getCombinationOfHead(List<String> urls) throws IOException, URISyntaxException {
    30. List<URL> paths = new ArrayList<>();
    31. int size = 9;
    32. if (urls.size() < size) {
    33. size = urls.size();
    34. }
    35. for (int i = 0; i < size; i++) {
    36. paths.add(new URL(urls.get(i)));
    37. }
    38. List<BufferedImage> bufferedImages = new ArrayList<>();
    39. // 压缩图片所有的图片生成尺寸同意的 为 50x50
    40. int imageSize = 33;
    41. if (paths.size() <= 4) {
    42. imageSize = 50;
    43. }
    44. for (int i = 0; i < paths.size(); i++) {
    45. bufferedImages.add(resize2(paths.get(i), imageSize, imageSize, true));
    46. }
    47. int width = 112; // 这是画板的宽高
    48. int height = 112; // 这是画板的高度
    49. // BufferedImage.TYPE_INT_RGB可以自己定义可查看API
    50. BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    51. // 生成画布
    52. Graphics g = outImage.getGraphics();
    53. Graphics2D g2d = (Graphics2D) g;
    54. // 设置背景色
    55. g2d.setBackground(new Color(231, 231, 231));
    56. //g2d.setBackground(new Color(231, 0, 4));
    57. // 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
    58. g2d.clearRect(0, 0, width, height);
    59. // 开始拼凑 根据图片的数量判断该生成那种样式的组合头像目前为4中
    60. int j = 1;
    61. int k = 1;
    62. for (int i = 1; i <= bufferedImages.size(); i++) {
    63. if (bufferedImages.size() == 9) {
    64. if (i <= 3) {
    65. g2d.drawImage(bufferedImages.get(i - 1), 33 * i + 3 * i - 33, 4, null);
    66. } else if (i <= 6) {
    67. g2d.drawImage(bufferedImages.get(i - 1), 33 * j + 3 * j - 33, 41, null);
    68. j++;
    69. } else {
    70. g2d.drawImage(bufferedImages.get(i - 1), 33 * k + 3 * k - 33, 77, null);
    71. k++;
    72. }
    73. } else if (bufferedImages.size() == 8) {
    74. if (i <= 2) {
    75. g2d.drawImage(bufferedImages.get(i - 1), 33 * i + 4 * i - 18, 4, null);
    76. } else if (i <= 5) {
    77. g2d.drawImage(bufferedImages.get(i - 1), 33 * j + 3 * j - 33, 41, null);
    78. j++;
    79. } else {
    80. g2d.drawImage(bufferedImages.get(i - 1), 33 * k + 3 * k - 33, 77, null);
    81. k++;
    82. }
    83. } else if (bufferedImages.size() == 7) {
    84. if (i <= 1) {
    85. g2d.drawImage(bufferedImages.get(i - 1), 39, 4, null);
    86. } else if (i <= 4) {
    87. g2d.drawImage(bufferedImages.get(i - 1), 33 * j + 3 * j - 33, 41, null);
    88. j++;
    89. } else {
    90. g2d.drawImage(bufferedImages.get(i - 1), 33 * k + 3 * k - 33, 77, null);
    91. k++;
    92. }
    93. } else if (bufferedImages.size() == 6) {
    94. if (i <= 3) {
    95. g2d.drawImage(bufferedImages.get(i - 1), 33 * i + 3 * i - 33, 15, null);
    96. } else {
    97. g2d.drawImage(bufferedImages.get(i - 1), 33 * j + 3 * j - 33, 58, null);
    98. j++;
    99. }
    100. } else if (bufferedImages.size() == 5) {
    101. if (i <= 2) {
    102. g2d.drawImage(bufferedImages.get(i - 1), 33 * i + 4 * i - 18, 15, null);
    103. } else {
    104. g2d.drawImage(bufferedImages.get(i - 1), 33 * j + 3 * j - 33, 58, null);
    105. j++;
    106. }
    107. } else if (bufferedImages.size() == 4) {
    108. if (i <= 2) {
    109. g2d.drawImage(bufferedImages.get(i - 1), 50 * i + 4 * i - 50, 4, null);
    110. } else {
    111. g2d.drawImage(bufferedImages.get(i - 1), 50 * j + 4 * j - 50, 58, null);
    112. j++;
    113. }
    114. } else if (bufferedImages.size() == 3) {
    115. if (i <= 1) {
    116. g2d.drawImage(bufferedImages.get(i - 1), 31, 4, null);
    117. } else {
    118. g2d.drawImage(bufferedImages.get(i - 1), 50 * j + 4 * j - 50, 58, null);
    119. j++;
    120. }
    121. } else if (bufferedImages.size() == 2) {
    122. g2d.drawImage(bufferedImages.get(i - 1), 50 * i + 4 * i - 50, 31, null);
    123. } else if (bufferedImages.size() == 1) {
    124. g2d.drawImage(bufferedImages.get(i - 1), 31, 31, null);
    125. }
    126. // 需要改变颜色的话在这里绘上颜色。可能会用到AlphaComposite类
    127. }
    128. // TODO 也可以保存到本地路径
    129. /*String outPath = "E:\\b.jpg";
    130. ImageIO.write(outImage, PICTRUE_FORMATE_PNG, new File(outPath));*/
    131. // 生成Base64字符串
    132. String imageString = null;
    133. ByteArrayOutputStream bos = new ByteArrayOutputStream();
    134. try {
    135. ImageIO.write(outImage, PICTRUE_FORMATE_PNG, bos);
    136. byte[] imageBytes = bos.toByteArray();
    137. imageString = Base64.encodeBase64String(imageBytes);
    138. bos.close();
    139. } catch (IOException e) {
    140. e.printStackTrace();
    141. }
    142. return "data:image/png;base64," + imageString;
    143. }
    144. /**
    145. * 图片缩放
    146. *
    147. * @param filePath 图片路径
    148. * @param height 高度
    149. * @param width 宽度
    150. * @param bb 比例不对时是否需要补白
    151. */
    152. private BufferedImage resize2(URL filePath, int height, int width, boolean bb) throws URISyntaxException {
    153. try {
    154. double ratio = 0; // 缩放比例
    155. DataInputStream dis = new DataInputStream(filePath.openStream());
    156. //File f = new File(dis);
    157. BufferedImage bi = ImageIO.read(dis);
    158. Image itemp = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    159. // 计算比例
    160. if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
    161. if (bi.getHeight() > bi.getWidth()) {
    162. ratio = (new Integer(height)).doubleValue() / bi.getHeight();
    163. } else {
    164. ratio = (new Integer(width)).doubleValue() / bi.getWidth();
    165. }
    166. AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
    167. itemp = op.filter(bi, null);
    168. }
    169. if (bb) {
    170. // copyimg(filePath, "D:\\img");
    171. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    172. Graphics2D g = image.createGraphics();
    173. g.setColor(Color.white);
    174. g.fillRect(0, 0, width, height);
    175. if (width == itemp.getWidth(null)) {
    176. g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
    177. } else {
    178. g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
    179. }
    180. g.dispose();
    181. itemp = image;
    182. }
    183. return (BufferedImage) itemp;
    184. } catch (IOException e) {
    185. e.printStackTrace();
    186. }
    187. return null;
    188. }
    189. }
    1. package org.jeecg.modules.ppl.utils;
    2. import org.jeecg.modules.system.util.MinioUtil;
    3. import javax.imageio.ImageIO;
    4. import java.awt.*;
    5. import java.awt.geom.AffineTransform;
    6. import java.awt.image.AffineTransformOp;
    7. import java.awt.image.BufferedImage;
    8. import java.io.*;
    9. import java.net.URL;
    10. import java.util.ArrayList;
    11. import java.util.List;
    12. /**
    13. * 合成群头像
    14. */
    15. public class ImageUtil {
    16. /**
    17. * 图片的间隙
    18. */
    19. private static final int SIDE = 6;
    20. /**
    21. * 画板尺寸
    22. */
    23. private static final int CANVANS_W = 112;
    24. private static final int CANVANS_H = 112;
    25. /**
    26. * 尺寸1 (小)
    27. */
    28. private static final int ONE_IMAGE_SIZE = CANVANS_H - (2 * SIDE);
    29. /**
    30. * 尺寸2 (中)
    31. */
    32. private static final int TWO_IMAGE_SIZE = (CANVANS_H - (3 * SIDE)) / 2;
    33. /**
    34. * 尺寸3 (大)
    35. */
    36. private static final int FIVE_IMAGE_SIZE = (CANVANS_H - (4 * SIDE)) / 3;
    37. private ImageUtil() {
    38. }
    39. /**
    40. * 生成群组头像
    41. *
    42. * @param paths 图片链接
    43. * @param dir 输出路径
    44. * @param groupId 群编号
    45. * @return
    46. * @throws IOException
    47. */
    48. public static boolean getCombinationOfhead(List<String> paths, String dir, String groupId) throws IOException {
    49. List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
    50. int imageSize = 0;
    51. if (paths.size() <= 1) {
    52. //若为一张图片
    53. imageSize = ONE_IMAGE_SIZE;
    54. } else if (paths.size() > 1 && paths.size() < 5) {
    55. //若为2-4张图片
    56. imageSize = TWO_IMAGE_SIZE;
    57. } else {
    58. //若>=5张图片
    59. imageSize = FIVE_IMAGE_SIZE;
    60. }
    61. for (int i = 0; i < paths.size(); i++) {
    62. BufferedImage resize2 = ImageUtil.resize2(paths.get(i), imageSize, imageSize, true);
    63. bufferedImages.add(resize2);
    64. }
    65. BufferedImage outImage = new BufferedImage(CANVANS_W, CANVANS_H, BufferedImage.TYPE_INT_RGB);
    66. // 生成画布
    67. Graphics g = outImage.getGraphics();
    68. Graphics2D g2d = (Graphics2D) g;
    69. // 设置背景色
    70. g2d.setBackground(new Color(231, 231, 231));
    71. // 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
    72. g2d.clearRect(0, 0, CANVANS_W, CANVANS_H);
    73. // 开始拼凑 根据图片的数量判断该生成那种样式的组合头像目前为九种
    74. for (int i = 1; i <= bufferedImages.size(); i++) {
    75. Integer size = bufferedImages.size();
    76. switch (size) {
    77. case 1:
    78. g2d.drawImage(bufferedImages.get(i - 1), SIDE, SIDE, null);
    79. break;
    80. case 2:
    81. if (i == 1) {
    82. g2d.drawImage(bufferedImages.get(i - 1), SIDE, (CANVANS_W - imageSize) / 2, null);
    83. } else {
    84. g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, (CANVANS_W - imageSize) / 2, null);
    85. }
    86. break;
    87. case 3:
    88. if (i == 1) {
    89. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - imageSize) / 2, SIDE, null);
    90. } else {
    91. g2d.drawImage(bufferedImages.get(i - 1), (i - 1) * SIDE + (i - 2) * imageSize, imageSize + (2 * SIDE), null);
    92. }
    93. break;
    94. case 4:
    95. if (i <= 2) {
    96. g2d.drawImage(bufferedImages.get(i - 1), i * SIDE + (i - 1) * imageSize, SIDE, null);
    97. } else {
    98. g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, imageSize + 2 * SIDE, null);
    99. }
    100. break;
    101. case 5:
    102. if (i <= 2) {
    103. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, (CANVANS_W - 2 * imageSize - SIDE) / 2, null);
    104. } else {
    105. g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);
    106. }
    107. //
    108. break;
    109. case 6:
    110. if (i <= 3) {
    111. g2d.drawImage(bufferedImages.get(i - 1), SIDE * i + imageSize * (i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2, null);
    112. } else {
    113. g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);
    114. }
    115. break;
    116. case 7:
    117. if (i <= 1) {
    118. g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, SIDE, null);
    119. }
    120. if (i <= 4 && i > 1) {
    121. g2d.drawImage(bufferedImages.get(i - 1), ((i - 1) * SIDE) + ((i - 2) * imageSize), 2 * SIDE + imageSize, null);
    122. }
    123. if (i <= 7 && i > 4) {
    124. g2d.drawImage(bufferedImages.get(i - 1), ((i - 4) * SIDE) + ((i - 5) * imageSize), 3 * SIDE + 2 * imageSize, null);
    125. }
    126. break;
    127. case 8:
    128. if (i <= 2) {
    129. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, SIDE, null);
    130. }
    131. if (i <= 5 && i > 2) {
    132. g2d.drawImage(bufferedImages.get(i - 1), ((i - 2) * SIDE) + ((i - 3) * imageSize), 2 * SIDE + imageSize, null);
    133. }
    134. if (i <= 8 && i > 5) {
    135. g2d.drawImage(bufferedImages.get(i - 1), ((i - 5) * SIDE) + ((i - 6) * imageSize), 3 * SIDE + 2 * imageSize, null);
    136. }
    137. break;
    138. case 9:
    139. if (i <= 3) {
    140. g2d.drawImage(bufferedImages.get(i - 1), (i * SIDE) + ((i - 1) * imageSize), SIDE, null);
    141. }
    142. if (i <= 6 && i > 3) {
    143. g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), 2 * SIDE + imageSize, null);
    144. }
    145. if (i <= 9 && i > 6) {
    146. g2d.drawImage(bufferedImages.get(i - 1), ((i - 6) * SIDE) + ((i - 7) * imageSize), 3 * SIDE + 2 * imageSize, null);
    147. }
    148. break;
    149. default:
    150. break;
    151. }
    152. }
    153. StringBuffer outPath = new StringBuffer().append(dir)
    154. .append(File.separatorChar)
    155. .append(groupId).append(".png");
    156. String format = "png";
    157. File file = new File(outPath.toString());
    158. if (!file.exists()) {
    159. file.mkdirs();
    160. }
    161. return ImageIO.write(outImage, format, file);
    162. }
    163. /**
    164. * 生成群组头像
    165. *
    166. * @param paths 图片链接
    167. * @param dir 输出路径
    168. * @return
    169. * @throws IOException
    170. */
    171. public static BufferedImage getCombinationOfhead(List<String> paths, String dir) throws IOException {
    172. List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
    173. int imageSize = 0;
    174. if (paths.size() <= 1) {
    175. //若为一张图片
    176. imageSize = ONE_IMAGE_SIZE;
    177. } else if (paths.size() > 1 && paths.size() < 5) {
    178. //若为2-4张图片
    179. imageSize = TWO_IMAGE_SIZE;
    180. } else {
    181. //若>=5张图片
    182. imageSize = FIVE_IMAGE_SIZE;
    183. }
    184. for (int i = 0; i < paths.size(); i++) {
    185. BufferedImage resize2 = ImageUtil.resize2(paths.get(i), imageSize, imageSize, true);
    186. bufferedImages.add(resize2);
    187. }
    188. BufferedImage outImage = new BufferedImage(CANVANS_W, CANVANS_H, BufferedImage.TYPE_INT_RGB);
    189. // 生成画布
    190. Graphics g = outImage.getGraphics();
    191. Graphics2D g2d = (Graphics2D) g;
    192. // 设置背景色
    193. g2d.setBackground(new Color(231, 231, 231));
    194. // 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
    195. g2d.clearRect(0, 0, CANVANS_W, CANVANS_H);
    196. // 开始拼凑 根据图片的数量判断该生成那种样式的组合头像目前为九种
    197. for (int i = 1; i <= bufferedImages.size(); i++) {
    198. Integer size = bufferedImages.size();
    199. switch (size) {
    200. case 1:
    201. g2d.drawImage(bufferedImages.get(i - 1), SIDE, SIDE, null);
    202. break;
    203. case 2:
    204. if (i == 1) {
    205. g2d.drawImage(bufferedImages.get(i - 1), SIDE, (CANVANS_W - imageSize) / 2, null);
    206. } else {
    207. g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, (CANVANS_W - imageSize) / 2, null);
    208. }
    209. break;
    210. case 3:
    211. if (i == 1) {
    212. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - imageSize) / 2, SIDE, null);
    213. } else {
    214. g2d.drawImage(bufferedImages.get(i - 1), (i - 1) * SIDE + (i - 2) * imageSize, imageSize + (2 * SIDE), null);
    215. }
    216. break;
    217. case 4:
    218. if (i <= 2) {
    219. g2d.drawImage(bufferedImages.get(i - 1), i * SIDE + (i - 1) * imageSize, SIDE, null);
    220. } else {
    221. g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, imageSize + 2 * SIDE, null);
    222. }
    223. break;
    224. case 5:
    225. if (i <= 2) {
    226. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, (CANVANS_W - 2 * imageSize - SIDE) / 2, null);
    227. } else {
    228. g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);
    229. }
    230. //
    231. break;
    232. case 6:
    233. if (i <= 3) {
    234. g2d.drawImage(bufferedImages.get(i - 1), SIDE * i + imageSize * (i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2, null);
    235. } else {
    236. g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);
    237. }
    238. break;
    239. case 7:
    240. if (i <= 1) {
    241. g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, SIDE, null);
    242. }
    243. if (i <= 4 && i > 1) {
    244. g2d.drawImage(bufferedImages.get(i - 1), ((i - 1) * SIDE) + ((i - 2) * imageSize), 2 * SIDE + imageSize, null);
    245. }
    246. if (i <= 7 && i > 4) {
    247. g2d.drawImage(bufferedImages.get(i - 1), ((i - 4) * SIDE) + ((i - 5) * imageSize), 3 * SIDE + 2 * imageSize, null);
    248. }
    249. break;
    250. case 8:
    251. if (i <= 2) {
    252. g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, SIDE, null);
    253. }
    254. if (i <= 5 && i > 2) {
    255. g2d.drawImage(bufferedImages.get(i - 1), ((i - 2) * SIDE) + ((i - 3) * imageSize), 2 * SIDE + imageSize, null);
    256. }
    257. if (i <= 8 && i > 5) {
    258. g2d.drawImage(bufferedImages.get(i - 1), ((i - 5) * SIDE) + ((i - 6) * imageSize), 3 * SIDE + 2 * imageSize, null);
    259. }
    260. break;
    261. case 9:
    262. if (i <= 3) {
    263. g2d.drawImage(bufferedImages.get(i - 1), (i * SIDE) + ((i - 1) * imageSize), SIDE, null);
    264. }
    265. if (i <= 6 && i > 3) {
    266. g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), 2 * SIDE + imageSize, null);
    267. }
    268. if (i <= 9 && i > 6) {
    269. g2d.drawImage(bufferedImages.get(i - 1), ((i - 6) * SIDE) + ((i - 7) * imageSize), 3 * SIDE + 2 * imageSize, null);
    270. }
    271. break;
    272. default:
    273. break;
    274. }
    275. }
    276. return outImage;
    277. }
    278. /**
    279. * 生成图片到minio
    280. * @param paths
    281. * @param dir
    282. * @param fileName
    283. * @return minio/ppl/group/
    284. */
    285. public static String generateImage(List<String> paths, String dir, String fileName) throws IOException {
    286. BufferedImage outImage = getCombinationOfhead(paths,dir);
    287. InputStream inputStream = bufferedImageToInputStream(outImage);
    288. return MinioUtil.uploadImage(inputStream,dir,fileName);
    289. }
    290. /**
    291. * 将BufferedImage转换为InputStream
    292. * @param image
    293. * @return
    294. */
    295. public static InputStream bufferedImageToInputStream(BufferedImage image){
    296. ByteArrayOutputStream os = new ByteArrayOutputStream();
    297. try {
    298. ImageIO.write(image, "png", os);
    299. InputStream input = new ByteArrayInputStream(os.toByteArray());
    300. return input;
    301. } catch (IOException e) {
    302. e.printStackTrace();
    303. }
    304. return null;
    305. }
    306. /**
    307. * 图片缩放
    308. *
    309. * @param filePath 图片路径
    310. * @param height 高度
    311. * @param width 宽度
    312. * @param bb 比例不对时是否需要补白
    313. */
    314. public static BufferedImage resize2(String filePath, int height, int width,
    315. boolean bb) {
    316. try {
    317. double ratio = 0; // 缩放比例
    318. // System.out.println("图片缩放"+filePath);
    319. BufferedImage bi = null;
    320. if (filePath.indexOf("http://") == 0) {
    321. bi = ImageIO.read(new URL(filePath));
    322. } else {
    323. bi = ImageIO.read(ImageUtil.class.getClassLoader().getResourceAsStream(filePath));
    324. }
    325. Image itemp = bi.getScaledInstance(width, height,Image.SCALE_SMOOTH);
    326. // 计算比例
    327. if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
    328. if (bi.getHeight() > bi.getWidth()) {
    329. ratio = (new Integer(height)).doubleValue()
    330. / bi.getHeight();
    331. } else {
    332. ratio = (new Integer(width)).doubleValue() / bi.getWidth();
    333. }
    334. AffineTransformOp op = new AffineTransformOp(
    335. AffineTransform.getScaleInstance(ratio, ratio), null);
    336. itemp = op.filter(bi, null);
    337. }
    338. if (bb) {
    339. BufferedImage image = new BufferedImage(width, height,
    340. BufferedImage.TYPE_INT_RGB);
    341. Graphics2D g = image.createGraphics();
    342. g.setColor(Color.white);
    343. g.fillRect(0, 0, width, height);
    344. if (width == itemp.getWidth(null)) {
    345. g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
    346. itemp.getWidth(null), itemp.getHeight(null),
    347. Color.white, null);
    348. } else {
    349. g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
    350. itemp.getWidth(null), itemp.getHeight(null),
    351. Color.white, null);
    352. }
    353. g.dispose();
    354. itemp = image;
    355. }
    356. return (BufferedImage) itemp;
    357. } catch (IOException e) {
    358. e.printStackTrace();
    359. }
    360. return null;
    361. }
    362. /**
    363. * 删除文件
    364. * @param path
    365. * @param filename
    366. */
    367. public void delFile(String path, String filename) {
    368. File file = new File(path + "/" + filename);
    369. if (file.exists() && file.isFile()) {
    370. file.delete();
    371. }
    372. }
    373. /**
    374. * 可依次生成九种情况的群头像
    375. *
    376. * @param args
    377. * @throws IOException
    378. */
    379. public static void main(String[] args) throws IOException {
    380. Integer groupId = 0;
    381. List<String> list = new ArrayList<>();
    382. for (int i = 1; i < 5; i++) {
    383. //本地图片路径或者网络图片路径
    384. list.add("D:\\IdeaProjects\\jeecg-boot-master\\jeecg-boot\\jeecg-boot-module-ppl\\src\\main\\resources\\static\\portrait\\male.png");
    385. }
    386. //生成目录
    387. String path = "D:\\IdeaProjects\\jeecg-boot-master\\jeecg-boot\\jeecg-boot-module-ppl\\src\\main\\resources\\static\\portrait";
    388. ImageUtil.getCombinationOfhead(list, path, "群名");
    389. }
    390. }