You've already forked FrameTour-BE
feat(image): 添加图片180度旋转功能
- 新增rotateImage180方法实现图片180度旋转 - 支持源文件读取和目标文件写入 - 使用AffineTransform实现图像旋转变换 - 保持图片原始尺寸不变 - 添加详细的异常处理和资源释放 - 移除对270度旋转的限制检查
This commit is contained in:
@@ -193,6 +193,52 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 旋转图片180度
|
||||||
|
*
|
||||||
|
* @param sourceFile 源图片文件
|
||||||
|
* @param targetFile 目标图片文件
|
||||||
|
* @throws IOException 读取或写入文件失败
|
||||||
|
*/
|
||||||
|
public static void rotateImage180(File sourceFile, File targetFile) throws IOException {
|
||||||
|
BufferedImage sourceImage = null;
|
||||||
|
BufferedImage rotatedImage = null;
|
||||||
|
try {
|
||||||
|
sourceImage = ImageIO.read(sourceFile);
|
||||||
|
if (sourceImage == null) {
|
||||||
|
throw new IOException("无法读取图片文件: " + sourceFile.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = sourceImage.getWidth();
|
||||||
|
int height = sourceImage.getHeight();
|
||||||
|
|
||||||
|
// 创建旋转后的图片(宽高不变)
|
||||||
|
rotatedImage = new BufferedImage(width, height, sourceImage.getType());
|
||||||
|
Graphics2D g2d = rotatedImage.createGraphics();
|
||||||
|
|
||||||
|
// 设置旋转变换
|
||||||
|
AffineTransform transform = new AffineTransform();
|
||||||
|
transform.translate(width / 2.0, height / 2.0);
|
||||||
|
transform.rotate(Math.PI);
|
||||||
|
transform.translate(-width / 2.0, -height / 2.0);
|
||||||
|
|
||||||
|
g2d.setTransform(transform);
|
||||||
|
g2d.drawImage(sourceImage, 0, 0, null);
|
||||||
|
g2d.dispose();
|
||||||
|
|
||||||
|
// 保存旋转后的图片
|
||||||
|
ImageIO.write(rotatedImage, "jpg", targetFile);
|
||||||
|
log.info("图片旋转180度成功,尺寸保持: {}x{}", width, height);
|
||||||
|
} finally {
|
||||||
|
if (sourceImage != null) {
|
||||||
|
sourceImage.flush();
|
||||||
|
}
|
||||||
|
if (rotatedImage != null) {
|
||||||
|
rotatedImage.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 智能裁切图片以填充目标尺寸,支持自动旋转以减少裁切损失
|
* 智能裁切图片以填充目标尺寸,支持自动旋转以减少裁切损失
|
||||||
*
|
*
|
||||||
@@ -330,10 +376,6 @@ public class ImageUtils {
|
|||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (degrees != 270) {
|
|
||||||
throw new IllegalArgumentException("仅支持270度旋转");
|
|
||||||
}
|
|
||||||
|
|
||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user