Commit a2c89b7c authored by 赵啸非's avatar 赵啸非

修改图片转换

parent ae32497e
Pipeline #2874 canceled with stages
......@@ -7,6 +7,7 @@ package com.mortals.xhx.common.utils;
**/
import com.aspose.words.Document;
import com.aspose.words.GraphicsQualityOptions;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
import com.mortals.framework.exception.AppException;
......@@ -24,6 +25,7 @@ import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -92,11 +94,19 @@ public class WordUtil {
//doc.save(os, SaveFormat.JPEG);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setResolution(256);
GraphicsQualityOptions qualityOptions = new GraphicsQualityOptions();
RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityOptions.setRenderingHints(renderingHints);
options.setGraphicsQualityOptions(qualityOptions);
options.setJpegQuality(10);
options.setPrettyFormat(true);
options.setUseAntiAliasing(true);
options.setUseHighQualityRendering(true);
int pageCount = doc.getPageCount();
log.info("转换的页数:{}",pageCount);
log.info("转换的页数:{}", pageCount);
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < pageCount; i++) {
OutputStream output = new ByteArrayOutputStream();
......@@ -104,18 +114,19 @@ public class WordUtil {
doc.save(output, options);
ImageInputStream imageInputStream = ImageIO.createImageInputStream(parse(output));
imageList.add(ImageIO.read(imageInputStream));
log.info("转换{}页完成!",i);
log.info("转换{}页完成!", i);
}
log.info("转换结束,开始拼接图片:{}",pageCount);
BufferedImage mergeImage = mergeImage(false, imageList);
log.info("转换结束,开始拼接图片:{}", pageCount);
//BufferedImage mergeImage = mergeImage(false, imageList);
BufferedImage mergeImage = moreToOne(imageList);
Thumbnails.of(mergeImage)
.scale(1f)
.outputQuality(0.5f).toFile(file);
.outputQuality(0.8f).toFile(file);
// ImageIO.write(mergeImage, "jepg", file);
//ImageIO.write(mergeImage, "jepg", file);
// 转换结束后时间
/* doc.save(jpegPath);*/
/* doc.save(jpegPath);*/
doc.cleanup();
long now = System.currentTimeMillis();
log.info("文件转换结束,共耗时:" + ((now - old) / 1000.0) + "秒");
......@@ -139,9 +150,6 @@ public class WordUtil {
}
public static void pdfToImages(String filePath, String jpegPath) {
log.info(String.format("pdf to images文件转换开始:%s", DateUtils.getCurrStrDateTime()));
// 转换开始前时间
......@@ -237,6 +245,62 @@ public class WordUtil {
return destImage;
}
/**
* 将多张图片拼接成一张长图
*
* @return 拼接后的图片BufferedImage
*/
public static BufferedImage moreToOne(List<BufferedImage> imgs) {
//传入图片少于1张直接返回
int len = imgs.size();
if (len < 1) {
return null;
}
//使用一个数组将图片装起来
BufferedImage[] bufferedImages = new BufferedImage[len];
int[][] imageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
//文件地址,可直接转换
bufferedImages[i] = Thumbnails.of(imgs.get(i))
//.size(400, 600)
.width(595)
.outputQuality(0.8f)
.asBufferedImage();
} catch (Exception e) {
log.error("图片转换失败", e);
}
//获取图片宽高
int width = bufferedImages[i].getWidth();
int height = bufferedImages[i].getHeight();
// 从图片中读取RGB
imageArrays[i] = new int[width * height];
imageArrays[i] = bufferedImages[i].getRGB(0, 0, width, height,
imageArrays[i], 0, width);
}
//拼接图片的宽高
int dstHeight = 0;
int dstWidth = bufferedImages[0].getWidth();
for (BufferedImage bufferedImage : bufferedImages) {
dstWidth = Math.max(dstWidth, bufferedImage.getWidth());
dstHeight += bufferedImage.getHeight();
}
// 生成新图片
BufferedImage imageNew = null;
try {
imageNew = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB);
int newHeight = 0;
for (int i = 0; i < bufferedImages.length; i++) {
imageNew.setRGB(0, newHeight, dstWidth, bufferedImages[i].getHeight(), imageArrays[i], 0, dstWidth);
newHeight += bufferedImages[i].getHeight();
}
} catch (Exception e) {
log.error("图片拼接出错", e);
}
return imageNew;
}
public static ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
......@@ -247,7 +311,7 @@ public class WordUtil {
}
public static void main(String[] args) {
String docPath = "E:\\pic\\doc\\3.docx";
String docPath = "E:\\pic\\doc\\new.docx";
String jpgPath = "E:\\pic\\doc\\3.jpg";
......
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