Commit 847910eb by songyinghui

feat: 新版图片合成

parent e9fceca0
......@@ -124,7 +124,7 @@ public class MaterialShareLogApiServiceImpl implements MaterialShareLogApiServic
try {
URL url = new URL(materialConvertDTO.getQrCodeUrl());
InputStream inputStream = url.openStream();
byteArrayOutputStream = cloneInputStream(inputStream);
byteArrayOutputStream = DrawImageUtils.cloneInputStream(inputStream);
}catch (Exception ex) {
log.info("获取二维码流对象异常 qrCodeUrl:{}", materialConvertDTO.getQrCodeUrl());
return ServiceResponse.failure("-701", "系统异常");
......@@ -136,7 +136,7 @@ public class MaterialShareLogApiServiceImpl implements MaterialShareLogApiServic
List<String> imageUrls = materialConvertDTO
.getImageUrls()
.parallelStream()
.map(item -> DrawImageUtils.addWater(enterpriseDTO.getFactoryCode(), item, new ByteArrayInputStream(finalByteArrayOutputStream.toByteArray())))
.map(item -> DrawImageUtils.addWaterNew(enterpriseDTO.getFactoryCode(), item, new ByteArrayInputStream(finalByteArrayOutputStream.toByteArray())))
.collect(Collectors.toList());
List<String> materialIds = imageUrls.parallelStream()
.map(item -> {
......@@ -154,19 +154,4 @@ public class MaterialShareLogApiServiceImpl implements MaterialShareLogApiServic
.collect(Collectors.toList());
return ServiceResponse.success(materialIds);
}
private static ByteArrayOutputStream cloneInputStream(InputStream input) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
return baos;
} catch (Exception e) {
return null;
}
}
}
......@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.thirdparty.cloudfile.CloudFileUtil;
import com.gic.thirdparty.cloudfile.enums.CloudFileBusinessOptEnum;
import com.gic.thirdparty.cloudfile.enums.CloudFileTypeEnum;
......@@ -17,6 +18,7 @@ import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -177,30 +179,29 @@ public class DrawImageUtils {
return url;
}
public static String addWaterNew(String factoryCode, String url, String qrCodeUrl) {
public static String addWaterNew(String factoryCode, String url, InputStream qrCodeInputStream) {
// 读取原图片信息
//文件转化为图片
try {
BufferedImage qrCodeBufferedImage = ImageIO.read(qrCodeInputStream);
long startTime = System.currentTimeMillis();
ImageCombiner imageCombiner = new ImageCombiner(url, OutputFormat.JPG);
BufferedImage combinedImage = imageCombiner.getCombinedImage();
int srcImgWidth = combinedImage.getWidth(null);
int srcImgHeight = combinedImage.getHeight(null);
log.info("图片宽度:{}, 图片高度:{}", srcImgHeight, srcImgHeight);
int srcImgWidth = imageCombiner.getCanvasWidth();
int srcImgHeight = imageCombiner.getCanvasHeight();
log.info("图片宽度:{}, 图片高度:{}", srcImgWidth, srcImgHeight);
if (srcImgHeight > 2000) {
log.info("原图片高度超过2000 高度:{}, 等比压缩", srcImgHeight);
// 按高度等比压缩
int tempWidth = BigDecimal.valueOf(srcImgWidth)
.multiply(BigDecimal.valueOf(2000).divide(BigDecimal.valueOf(srcImgHeight), 2, RoundingMode.HALF_DOWN))
.intValue();
log.info("压缩后的宽:{}", tempWidth);
srcImgHeight = 2000;
srcImgWidth = tempWidth;
imageCombiner = new ImageCombiner(combinedImage, tempWidth, 2000, ZoomMode.Height, OutputFormat.JPG);
imageCombiner = new ImageCombiner(url, 0, srcImgHeight, ZoomMode.Height, OutputFormat.JPG);
srcImgWidth = imageCombiner.getCanvasWidth();
log.warn("转换后实际的宽: {}, 高:{}", imageCombiner.getCanvasWidth(), imageCombiner.getCanvasHeight());
}
log.info("转换后图片宽度:{}, 图片高度:{}", srcImgWidth, srcImgHeight);
// 计算二维码宽度
int qrCodeWidthTemp = srcImgWidth * 136 / 750;
imageCombiner.addImageElement(qrCodeUrl, srcImgWidth - (qrCodeWidthTemp + 20), srcImgHeight - (qrCodeWidthTemp + 20), qrCodeWidthTemp, qrCodeWidthTemp, ZoomMode.Height);
log.info("二维码计算后的宽度 {}", qrCodeWidthTemp);
imageCombiner.addImageElement(qrCodeBufferedImage, srcImgWidth - (qrCodeWidthTemp + 20), srcImgHeight - (qrCodeWidthTemp + 20), qrCodeWidthTemp, qrCodeWidthTemp, ZoomMode.Height);
imageCombiner.combine();
InputStream combinedImageStream = imageCombiner.getCombinedImageStream();
log.info("合成之后的大小 {}", combinedImageStream.available());
......@@ -241,14 +242,43 @@ public class DrawImageUtils {
String url = "https://jhdm-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-7c3bc8061ffb4e2694c0e288a1ce176b.jpeg?imageView2/format/jpg/q/50";
//String url = "https://newdmwltest-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-bd89447576b5438d8045def2da2c4c0f.jpg?imageView2/format/jpg/q/50";
System.setProperty("gic.module.name", "haoban-manage3-service");
System.out.println(DrawImageUtils.addWaterNew("jhdm", url, qrcodeUrl));
// Random random = new Random();
// String url = DrawImageUtils.drawImage("jhdm", Math.abs(random.nextInt(1000)) + "",
// Math.abs(random.nextInt(1000)) + "万", DrawBkgImg.AREA_MONTH_BKG);
// System.out.println(url);
//
// String temp = DrawImageUtils.drawImage("jhdm", Math.abs(random.nextInt(1000)) + "",
// Math.abs(random.nextInt(1000)) + "万", DrawBkgImg.CLERK_MONTH_BKG);
// System.out.println(temp);
ByteArrayOutputStream byteArrayOutputStream = null;
try {
URL openUrl = new URL(qrcodeUrl);
InputStream inputStream = openUrl.openStream();
byteArrayOutputStream = cloneInputStream(inputStream);
}catch (Exception ex) {
log.info("获取二维码流对象异常 qrCodeUrl:{}", ex);
}
if (byteArrayOutputStream == null) {
return;
}
for (int i = 0; i < 30; i++) {
System.out.println(DrawImageUtils.addWaterNew("jhdm", url, new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
}
}
/**
* inputStrean 拷贝
* @param input
* @return
*/
public static ByteArrayOutputStream cloneInputStream(InputStream input) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
return baos;
} catch (Exception ex) {
log.info("stream 拷贝异常", ex);
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment