|
@@ -8,9 +8,12 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
|
|
+import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
public class PdfUtil {
|
|
public class PdfUtil {
|
|
@@ -40,17 +43,15 @@ public class PdfUtil {
|
|
PDFRenderer renderer = new PDFRenderer(document);
|
|
PDFRenderer renderer = new PDFRenderer(document);
|
|
|
|
|
|
// 始终处理首页(页码从1开始)
|
|
// 始终处理首页(页码从1开始)
|
|
- generatedImages.add(
|
|
|
|
- renderPage(renderer, 0, baseName + ".png", outputDir, dpi)
|
|
|
|
- );
|
|
|
|
|
|
+ String firstImg = renderPage(renderer, 0, baseName + ".png", outputDir, dpi);
|
|
|
|
+ generatedImages.add(compressAndSave(firstImg));
|
|
|
|
|
|
// 处理尾页(当总页数 > 1 时)
|
|
// 处理尾页(当总页数 > 1 时)
|
|
if (totalPages > 1) {
|
|
if (totalPages > 1) {
|
|
- generatedImages.add(
|
|
|
|
- renderPage(renderer, totalPages - 1,
|
|
|
|
- baseName + "_footer.png",
|
|
|
|
- outputDir, dpi)
|
|
|
|
- );
|
|
|
|
|
|
+ String lastImg = renderPage(renderer, totalPages - 1,
|
|
|
|
+ baseName + "_footer.png",
|
|
|
|
+ outputDir, dpi);
|
|
|
|
+ generatedImages.add(compressAndSave(lastImg));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -67,4 +68,147 @@ public class PdfUtil {
|
|
ImageIO.write(image, "PNG", outputFile);
|
|
ImageIO.write(image, "PNG", outputFile);
|
|
return outputFile.getAbsolutePath();
|
|
return outputFile.getAbsolutePath();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 检查图片分辨率是否超过限制
|
|
|
|
+ *
|
|
|
|
+ * @param image 图片对象
|
|
|
|
+ * @param maxSize 最大允许尺寸(单边像素数)
|
|
|
|
+ * @return 是否超出限制
|
|
|
|
+ */
|
|
|
|
+ private static boolean isResolutionExceeded(BufferedImage image, int maxSize) {
|
|
|
|
+ return image.getWidth() > maxSize || image.getHeight() > maxSize;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+// /**
|
|
|
|
+// * 压缩图片并转换为Base64
|
|
|
|
+// *
|
|
|
|
+// * @param inputFile 输入图片文件
|
|
|
|
+// * @param maxSize 最大允许尺寸(单边像素数)
|
|
|
|
+// * @param quality 压缩质量 (0.0-1.0)
|
|
|
|
+// * @param format 输出格式 ("jpg", "png"等)
|
|
|
|
+// * @return Base64编码的图片数据
|
|
|
|
+// */
|
|
|
|
+// public static String compressAndConvertToBase64(File inputFile, int maxSize, float quality, String format)
|
|
|
|
+// throws IOException {
|
|
|
|
+//
|
|
|
|
+// // 读取原始图片
|
|
|
|
+// BufferedImage originalImage = ImageIO.read(inputFile);
|
|
|
|
+//
|
|
|
|
+// // 检查分辨率是否超出限制
|
|
|
|
+// if (!isResolutionExceeded(originalImage, maxSize)) {
|
|
|
|
+// System.out.println("图片尺寸符合要求,无需压缩");
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 计算新尺寸(保持宽高比)
|
|
|
|
+// int originalWidth = originalImage.getWidth();
|
|
|
|
+// int originalHeight = originalImage.getHeight();
|
|
|
|
+// double ratio = (double) maxSize / Math.max(originalWidth, originalHeight);
|
|
|
|
+// int newWidth = (int) (originalWidth * ratio);
|
|
|
|
+// int newHeight = (int) (originalHeight * ratio);
|
|
|
|
+//
|
|
|
|
+// // 创建缩放后的图片
|
|
|
|
+// Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
|
|
|
|
+// BufferedImage outputImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+//
|
|
|
|
+// // 绘制缩放后的图片
|
|
|
|
+// Graphics2D g2d = outputImage.createGraphics();
|
|
|
|
+// g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
+// g2d.drawImage(scaledImage, 0, 0, null);
|
|
|
|
+// g2d.dispose();
|
|
|
|
+//
|
|
|
|
+// // 转换为Base64
|
|
|
|
+// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+// ImageIO.write(outputImage, format, baos);
|
|
|
|
+// byte[] imageBytes = baos.toByteArray();
|
|
|
|
+//
|
|
|
|
+// return Base64.getEncoder().encodeToString(imageBytes);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ public static String compressAndSave(String inputFile) throws IOException {
|
|
|
|
+ return compressAndSave(FileUtil.file(inputFile));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String compressAndSave(File inputFile) throws IOException {
|
|
|
|
+ int maxSize = 8192;
|
|
|
|
+ String format = FileUtil.extName(inputFile);
|
|
|
|
+ return compressAndSave(inputFile, null, maxSize, format);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 压缩图片并保存到文件
|
|
|
|
+ *
|
|
|
|
+ * @param inputFile 输入文件
|
|
|
|
+ * @param outputFile 输出文件
|
|
|
|
+ * @param maxSize 最大尺寸
|
|
|
|
+ * @param format 输出格式
|
|
|
|
+ */
|
|
|
|
+ public static String compressAndSave(File inputFile, File outputFile, int maxSize, String format)
|
|
|
|
+ throws IOException {
|
|
|
|
+
|
|
|
|
+ BufferedImage originalImage = ImageIO.read(inputFile);
|
|
|
|
+
|
|
|
|
+ if (!isResolutionExceeded(originalImage, maxSize)) {
|
|
|
|
+ if (outputFile != null) {
|
|
|
|
+ // 直接复制文件
|
|
|
|
+ Files.copy(inputFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
+ return outputFile.getAbsolutePath();
|
|
|
|
+ }
|
|
|
|
+ return inputFile.getAbsolutePath();
|
|
|
|
+ }
|
|
|
|
+ if (outputFile == null) {
|
|
|
|
+ outputFile = inputFile;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int originalWidth = originalImage.getWidth();
|
|
|
|
+ int originalHeight = originalImage.getHeight();
|
|
|
|
+ double ratio = (double) maxSize / Math.max(originalWidth, originalHeight);
|
|
|
|
+ int newWidth = (int) (originalWidth * ratio);
|
|
|
|
+ int newHeight = (int) (originalHeight * ratio);
|
|
|
|
+
|
|
|
|
+ Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
|
|
|
|
+ BufferedImage outputImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+
|
|
|
|
+ Graphics2D g2d = outputImage.createGraphics();
|
|
|
|
+ g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
+ g2d.drawImage(scaledImage, 0, 0, null);
|
|
|
|
+ g2d.dispose();
|
|
|
|
+
|
|
|
|
+ // 保存到文件
|
|
|
|
+ ImageIO.write(outputImage, format, outputFile);
|
|
|
|
+ return outputFile.getAbsolutePath();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ try {
|
|
|
|
+ // 示例用法
|
|
|
|
+ File inputFile = new File("D:\\home\\wwwroot\\mo_report_file\\wangzaijun@simuwang.com\\20250605\\image\\泓湖泓福积极配置2期私募证券投资基金-周报-20250530.png");
|
|
|
|
+ int maxSize = 8192; // OpenAI限制
|
|
|
|
+
|
|
|
|
+ // 1. 检查图片是否超出限制
|
|
|
|
+ BufferedImage image = ImageIO.read(inputFile);
|
|
|
|
+ if (isResolutionExceeded(image, maxSize)) {
|
|
|
|
+ System.out.println("图片超出尺寸限制,需要压缩");
|
|
|
|
+ System.out.println("原始尺寸: " + image.getWidth() + "x" + image.getHeight());
|
|
|
|
+
|
|
|
|
+// // 2. 压缩并转换为Base64
|
|
|
|
+// String base64Image = compressAndConvertToBase64(inputFile, maxSize, 0.85f, "png");
|
|
|
|
+// System.out.println("Base64 数据长度: " + base64Image.length());
|
|
|
|
+// System.out.println("Base64 前缀: " + base64Image.substring(0, 50) + "...");
|
|
|
|
+
|
|
|
|
+ // 3. 压缩并保存到文件
|
|
|
|
+ File outputFile = new File("D:\\home\\wwwroot\\mo_report_file\\wangzaijun@simuwang.com\\20250605\\image\\泓湖泓福积极配置2期私募证券投资基金-周报-20250530.png");
|
|
|
|
+ String output = compressAndSave(inputFile, outputFile, maxSize, "png");
|
|
|
|
+ System.out.println("图片已压缩保存到: " + output);
|
|
|
|
+
|
|
|
|
+ // 验证压缩后尺寸
|
|
|
|
+ BufferedImage compressedImage = ImageIO.read(outputFile);
|
|
|
|
+ System.out.println("压缩后尺寸: " + compressedImage.getWidth() + "x" + compressedImage.getHeight());
|
|
|
|
+ } else {
|
|
|
|
+ System.out.println("图片尺寸符合要求");
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|