ImageUtils.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.pavis.ctr.audit.common.utils;
  2. import com.baidubce.services.bos.BosClient;
  3. import org.apache.commons.lang3.StringUtils;
  4. import javax.imageio.ImageIO;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.net.URL;
  9. /**
  10. * @author semi
  11. * @create 2023-05-15 14:21
  12. */
  13. public class ImageUtils {
  14. /**
  15. * 合并任数量的图片成一张图片
  16. *
  17. * @param isHorizontal true代表水平合并,fasle代表垂直合并
  18. * @param imgs 待合并的图片数组
  19. * @return
  20. * @throws IOException
  21. */
  22. public static BufferedImage mergeImage(boolean isHorizontal, BufferedImage[] imgs) throws IOException {
  23. // 生成新图片
  24. BufferedImage destImage = null;
  25. // 计算新图片的长和高
  26. int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
  27. // 获取总长、总宽、最长、最宽
  28. for (int i = 0; i < imgs.length; i++) {
  29. BufferedImage img = imgs[i];
  30. allw += img.getWidth();
  31. allh += img.getHeight();
  32. if (img.getWidth() > allwMax) {
  33. allwMax = img.getWidth();
  34. }
  35. if (img.getHeight() > allhMax) {
  36. allhMax = img.getHeight();
  37. }
  38. }
  39. // 创建新图片
  40. if (isHorizontal) {
  41. destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
  42. } else {
  43. destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
  44. }
  45. // 合并所有子图片到新图片
  46. int wx = 0, wy = 0;
  47. for (int i = 0; i < imgs.length; i++) {
  48. BufferedImage img = imgs[i];
  49. int w1 = img.getWidth();
  50. int h1 = img.getHeight();
  51. // 从图片中读取RGB
  52. int[] ImageArrayOne = new int[w1 * h1];
  53. ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
  54. if (isHorizontal) { // 水平方向合并
  55. destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
  56. } else { // 垂直方向合并
  57. destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
  58. }
  59. wx += w1;
  60. wy += h1;
  61. }
  62. return destImage;
  63. }
  64. /**
  65. * 纵向拼接一组(多张)图像
  66. *
  67. * @param pics 将要拼接的图像数组
  68. * @param type 写入图像类型
  69. * @param dstPic 写入图像路径
  70. * @return
  71. */
  72. public static String joinImageListVertical(String[] pics, String type, String dstPic, BosClient client) {
  73. try {
  74. int len = pics.length;
  75. if (len == 1) {
  76. return pics[0];
  77. }
  78. BufferedImage[] images = new BufferedImage[len];
  79. for (int i = 0; i < len; i++) {
  80. images[i] = ImageIO.read(new URL(pics[i]));
  81. }
  82. BufferedImage dstImg = mergeImage(false, images);
  83. File file = new File(dstPic);
  84. ImageIO.write(dstImg, type, file);
  85. // 上传至bos
  86. return BosUtils.putByName(client, StringUtils.substringAfterLast(dstPic, "/"), file);
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. return "";
  90. }
  91. }
  92. }