package com.ruoyi.inspect.util;
|
|
import com.deepoove.poi.data.PictureRenderData;
|
import com.deepoove.poi.data.Pictures;
|
|
import javax.imageio.ImageIO;
|
import java.awt.image.BufferedImage;
|
import java.io.File;
|
import java.io.IOException;
|
|
/**
|
* 报告签名图片工具。
|
*/
|
public final class ReportSignaturePictureUtils {
|
|
private static final int MAX_WIDTH = 90;
|
private static final int MAX_HEIGHT = 45;
|
|
private ReportSignaturePictureUtils() {
|
}
|
|
/**
|
* 按原始比例将签名适配到报告签名单元格,避免图片越过单元格边界而被裁剪。
|
*/
|
public static PictureRenderData create(String imageDirectory, String signatureUrl) {
|
String signaturePath = imageDirectory + "/" + signatureUrl;
|
try {
|
BufferedImage image = ImageIO.read(new File(signaturePath));
|
if (image != null && image.getWidth() > 0 && image.getHeight() > 0) {
|
double scale = Math.min((double) MAX_WIDTH / image.getWidth(),
|
(double) MAX_HEIGHT / image.getHeight());
|
int width = Math.max(1, (int) Math.round(image.getWidth() * scale));
|
int height = Math.max(1, (int) Math.round(image.getHeight() * scale));
|
return Pictures.ofLocal(signaturePath).size(width, height).create();
|
}
|
} catch (IOException ignored) {
|
// 图片元数据读取失败时仍可尝试以默认尺寸写入报告。
|
}
|
return Pictures.ofLocal(signaturePath).size(MAX_WIDTH, MAX_HEIGHT).create();
|
}
|
}
|