|
@@ -1,13 +1,25 @@
|
|
|
package com.pavis.ctr.audit.common.utils;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSException;
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
import com.baidubce.services.bos.BosClient;
|
|
|
+import com.pavis.ctr.audit.common.constant.Constants;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
/**
|
|
|
* @author semi
|
|
@@ -33,7 +45,8 @@ public class ImageUtils {
|
|
|
for (int i = 0; i < imgs.length; i++) {
|
|
|
BufferedImage img = imgs[i];
|
|
|
allw += img.getWidth();
|
|
|
- allh += img.getHeight();
|
|
|
+ // 宽度+10,作为位移宽度
|
|
|
+ allh += img.getHeight() + Constants.SPACE_HEIGHT;
|
|
|
if (img.getWidth() > allwMax) {
|
|
|
allwMax = img.getWidth();
|
|
|
}
|
|
@@ -47,6 +60,10 @@ public class ImageUtils {
|
|
|
} else {
|
|
|
destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
|
|
|
}
|
|
|
+ // 为图片设置背景色
|
|
|
+ Graphics graphics = destImage.getGraphics();
|
|
|
+ graphics.setColor(Color.YELLOW);
|
|
|
+ graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight());
|
|
|
// 合并所有子图片到新图片
|
|
|
int wx = 0, wy = 0;
|
|
|
for (int i = 0; i < imgs.length; i++) {
|
|
@@ -55,14 +72,19 @@ public class ImageUtils {
|
|
|
int h1 = img.getHeight();
|
|
|
// 从图片中读取RGB
|
|
|
int[] ImageArrayOne = new int[w1 * h1];
|
|
|
- ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
|
|
|
- if (isHorizontal) { // 水平方向合并
|
|
|
- destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
|
|
|
- } else { // 垂直方向合并
|
|
|
- destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
|
|
|
+ // 逐行扫描图像中各个像素的RGB到数组中
|
|
|
+ ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
|
|
|
+ // 水平方向合并
|
|
|
+ if (isHorizontal) {
|
|
|
+ // 设置上半部分或左半部分的RGB
|
|
|
+ destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1);
|
|
|
+ } else {
|
|
|
+ // 垂直方向合并
|
|
|
+ // 设置上半部分或左半部分的RGB
|
|
|
+ destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1);
|
|
|
}
|
|
|
wx += w1;
|
|
|
- wy += h1;
|
|
|
+ wy += h1 + Constants.SPACE_HEIGHT;
|
|
|
}
|
|
|
return destImage;
|
|
|
}
|
|
@@ -77,6 +99,7 @@ public class ImageUtils {
|
|
|
*/
|
|
|
public static String joinImageListVertical(String[] pics, String type, String dstPic, BosClient client) {
|
|
|
try {
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
int len = pics.length;
|
|
|
if (len == 1) {
|
|
|
return pics[0];
|
|
@@ -88,11 +111,141 @@ public class ImageUtils {
|
|
|
BufferedImage dstImg = mergeImage(false, images);
|
|
|
File file = new File(dstPic);
|
|
|
ImageIO.write(dstImg, type, file);
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
// 上传至bos
|
|
|
- return BosUtils.putByName(client, StringUtils.substringAfterLast(dstPic, "/"), file);
|
|
|
+ String mergeImgUrl = BosUtils.putByName(client, StringUtils.substringAfterLast(dstPic, "/"), file);
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ System.out.println("merge img url:" + mergeImgUrl);
|
|
|
+ return mergeImgUrl;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 纵向拼接一组(多张)图像
|
|
|
+ *
|
|
|
+ * @param pics 将要拼接的图像数组
|
|
|
+ * @param type 写入图像类型
|
|
|
+ * @param dstPic 写入图像路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String joinImagesVertical(String[] pics, String type, String dstPic, BosClient client) {
|
|
|
+ String mergeImgUrl = "";
|
|
|
+ try {
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ // 加载图片并计算总宽度和高度
|
|
|
+ int maxWidth = 0;
|
|
|
+ int totalHeight = 0;
|
|
|
+ BufferedImage[] images = new BufferedImage[pics.length];
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ for (int i = 0; i < pics.length; i++) {
|
|
|
+ URL url = new URL(pics[i]);
|
|
|
+ BufferedImage image = ImageIO.read(url);
|
|
|
+ images[i] = image;
|
|
|
+ maxWidth = Math.max(maxWidth, image.getWidth());
|
|
|
+ totalHeight += image.getHeight() + Constants.SPACE_HEIGHT;
|
|
|
+ }
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ // 创建拼接后的图片
|
|
|
+ BufferedImage joinedImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
+ // 设置背景颜色
|
|
|
+ Graphics2D g = joinedImage.createGraphics();
|
|
|
+ g.setColor(Color.YELLOW);
|
|
|
+ g.fillRect(0, 0, joinedImage.getWidth(), joinedImage.getHeight());
|
|
|
+ // 拼接图片
|
|
|
+ int y = 0;
|
|
|
+ for (BufferedImage image : images) {
|
|
|
+ g.drawImage(image, 0, y, null);
|
|
|
+ y += image.getHeight() + Constants.SPACE_HEIGHT;
|
|
|
+ }
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ File file = new File(dstPic);
|
|
|
+ // 保存拼接后的图片
|
|
|
+ ImageIO.write(joinedImage, type, file);
|
|
|
+ // 上传至bos
|
|
|
+ mergeImgUrl = BosUtils.putByName(client, StringUtils.substringAfterLast(dstPic, "/"), file);
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ System.out.println(DateUtils.getNowDate().toString());
|
|
|
+ System.out.println("merge img url:" + mergeImgUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return mergeImgUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BufferedImage[] imageDownloader(String[] pics) {
|
|
|
+ // 创建线程池,根据实际情况调整线程池大小
|
|
|
+ int corePoolSize = pics.length / 2;
|
|
|
+ int maximumPoolSize = pics.length;
|
|
|
+ long keepAliveTime = 60L;
|
|
|
+ TimeUnit unit = TimeUnit.SECONDS;
|
|
|
+ BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
|
|
|
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
|
|
|
+ // 并发下载图片
|
|
|
+ List<Future<BufferedImage>> futures = new ArrayList<>();
|
|
|
+ for (String imageUrl : pics) {
|
|
|
+ Future<BufferedImage> future = executor.submit(() -> {
|
|
|
+ URL url = new URL(imageUrl);
|
|
|
+ InputStream in = url.openStream();
|
|
|
+ BufferedImage image = ImageIO.read(in);
|
|
|
+ in.close();
|
|
|
+ System.out.println(imageUrl + " downloaded successfully.");
|
|
|
+ return image;
|
|
|
+ });
|
|
|
+ futures.add(future);
|
|
|
+ }
|
|
|
+ // 关闭线程池
|
|
|
+ executor.shutdown();
|
|
|
+ // 处理下载结果
|
|
|
+ List<BufferedImage> images = new ArrayList<>();
|
|
|
+ for (Future<BufferedImage> future : futures) {
|
|
|
+ try {
|
|
|
+ BufferedImage image = future.get();
|
|
|
+ images.add(image);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("Error downloading image: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 将所有下载成功的图片存储在BufferedImage数组中返回
|
|
|
+ return images.toArray(new BufferedImage[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String joinImageListHtml(String[] pics, String dstHtml, OSS ossClient) {
|
|
|
+ try {
|
|
|
+ if (pics.length > 0) {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ builder.append("<html>\n" +
|
|
|
+ "<head></head>\n" +
|
|
|
+ "<body>\n");
|
|
|
+ for (String pic : pics) {
|
|
|
+ String s = String.format("<img src=\"%s\">\n", pic);
|
|
|
+ builder.append(s);
|
|
|
+ }
|
|
|
+ builder.append("</body>\n" +
|
|
|
+ "</html>");
|
|
|
+ InputStream is = IOUtils.toInputStream(builder.toString(), "UTF-8");
|
|
|
+ PutObjectResult result = ossClient.putObject(Constants.OSS_BUCKET, dstHtml, is);
|
|
|
+ System.out.println("put result:" + JSON.toJSONString(result));
|
|
|
+ }
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
+ System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
+ System.out.println("Request ID:" + oe.getRequestId());
|
|
|
+ System.out.println("Host ID:" + oe.getHostId());
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ System.out.println("Error Message:" + ce.getMessage());
|
|
|
+ } catch (IOException ie) {
|
|
|
+ System.out.println("Error Message:" + ie.getMessage());
|
|
|
+ }
|
|
|
+ String mergeUrl = Constants.OSS_URL + dstHtml;
|
|
|
+ System.out.println("merge url:" + mergeUrl);
|
|
|
+ return mergeUrl;
|
|
|
+ }
|
|
|
}
|