You've already forked FrameTour-BE
feat(printer): 根据图片方向智能设置裁剪参数
- 在图片裁剪前增加图片方向检测功能 - 当检测到横图时才应用270度旋转裁剪参数 - 添加ImageUtils工具类isLandscape方法判断图片方向 - 完善图片方向检测异常处理和资源清理 - 优化打印服务中图片处理流程的条件判断逻辑
This commit is contained in:
@@ -353,6 +353,14 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
log.debug("打印机高度未配置或无效,使用默认值: height={}", printHeight);
|
log.debug("打印机高度未配置或无效,使用默认值: height={}", printHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检测原图方向
|
||||||
|
boolean isLandscape = false;
|
||||||
|
try {
|
||||||
|
isLandscape = ImageUtils.isLandscape(url);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("检测图片方向失败,默认为竖图: url={}", url, e);
|
||||||
|
}
|
||||||
|
|
||||||
// 使用smartCropAndFill裁剪图片
|
// 使用smartCropAndFill裁剪图片
|
||||||
File croppedFile = ImageUtils.smartCropAndFill(url, printWidth, printHeight);
|
File croppedFile = ImageUtils.smartCropAndFill(url, printWidth, printHeight);
|
||||||
|
|
||||||
@@ -365,8 +373,9 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
|
|
||||||
log.info("照片裁剪成功: memberId={}, scenicId={}, 原图={}, 裁剪后={}, 尺寸={}x{}",
|
log.info("照片裁剪成功: memberId={}, scenicId={}, 原图={}, 裁剪后={}, 尺寸={}x{}",
|
||||||
memberId, scenicId, url, cropUrl, printWidth, printHeight);
|
memberId, scenicId, url, cropUrl, printWidth, printHeight);
|
||||||
String crop = JacksonUtil.toJSONString(new Crop(270));
|
if (isLandscape) {
|
||||||
entity.setCrop(crop);
|
entity.setCrop(JacksonUtil.toJSONString(new Crop(270)));
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// 清理临时文件
|
// 清理临时文件
|
||||||
if (croppedFile != null && croppedFile.exists()) {
|
if (croppedFile != null && croppedFile.exists()) {
|
||||||
@@ -637,9 +646,19 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
log.debug("打印机高度未配置或无效,使用默认值: height={}", printHeight);
|
log.debug("打印机高度未配置或无效,使用默认值: height={}", printHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检测原图方向
|
||||||
|
boolean isLandscape = false;
|
||||||
|
try {
|
||||||
|
isLandscape = ImageUtils.isLandscape(url);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("检测图片方向失败,默认为竖图: url={}", url, e);
|
||||||
|
}
|
||||||
|
|
||||||
// 使用smartCropAndFill裁剪图片
|
// 使用smartCropAndFill裁剪图片
|
||||||
File croppedFile = ImageUtils.smartCropAndFill(url, printWidth, printHeight);
|
File croppedFile = ImageUtils.smartCropAndFill(url, printWidth, printHeight);
|
||||||
|
if (isLandscape) {
|
||||||
entity.setCrop(JacksonUtil.toJSONString(new Crop(270)));
|
entity.setCrop(JacksonUtil.toJSONString(new Crop(270)));
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 上传裁剪后的图片
|
// 上传裁剪后的图片
|
||||||
|
|||||||
@@ -101,6 +101,29 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断图片是否为横图(宽度大于高度)
|
||||||
|
* 支持URL字符串或文件路径
|
||||||
|
*
|
||||||
|
* @param imageSource URL字符串或文件路径
|
||||||
|
* @return true表示横图,false表示竖图
|
||||||
|
* @throws IOException 读取图片失败
|
||||||
|
*/
|
||||||
|
public static boolean isLandscape(String imageSource) throws IOException {
|
||||||
|
BufferedImage image = null;
|
||||||
|
try {
|
||||||
|
image = loadImage(imageSource);
|
||||||
|
if (image == null) {
|
||||||
|
throw new IOException("无法读取图片: " + imageSource);
|
||||||
|
}
|
||||||
|
return image.getWidth() > image.getHeight();
|
||||||
|
} finally {
|
||||||
|
if (image != null) {
|
||||||
|
image.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 旋转图片90度(顺时针)
|
* 旋转图片90度(顺时针)
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user