9 changed files with 184 additions and 19 deletions
@ -0,0 +1,125 @@ |
|||||
|
|
||||
|
package org.dromara.business.utils; |
||||
|
|
||||
|
import javax.imageio.ImageIO; |
||||
|
import java.awt.*; |
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URL; |
||||
|
import java.util.Base64; |
||||
|
|
||||
|
public class ImageRotator { |
||||
|
|
||||
|
/** |
||||
|
* 旋转图片并叠加到另一个图片上 |
||||
|
* @param sourceImagePath 源图片路径 |
||||
|
* @param targetImagePath 目标图片路径 |
||||
|
* @param angle 旋转角度(顺时针) |
||||
|
* @return 处理后的图片的Base64字符串 |
||||
|
*/ |
||||
|
public static String rotateAndOverlayImage(String sourceImagePath, String targetImagePath, double angle) { |
||||
|
try { |
||||
|
URL resourceUrl = ImageRotator.class.getClassLoader().getResource("templates/1.png"); |
||||
|
URL targetUrl = ImageRotator.class.getClassLoader().getResource("templates/2.png"); |
||||
|
|
||||
|
// 读取源图片和目标图片
|
||||
|
BufferedImage sourceImage = ImageIO.read(new File(resourceUrl.getPath())); |
||||
|
BufferedImage targetImage = ImageIO.read(new File(targetUrl.getPath())); |
||||
|
|
||||
|
// 创建旋转后的图片
|
||||
|
BufferedImage rotatedImage = rotateImage(sourceImage, angle); |
||||
|
|
||||
|
// 创建新的画布,使用目标图片的尺寸
|
||||
|
BufferedImage resultImage = new BufferedImage( |
||||
|
targetImage.getWidth(), |
||||
|
targetImage.getHeight(), |
||||
|
BufferedImage.TYPE_INT_ARGB |
||||
|
); |
||||
|
|
||||
|
// 创建图形上下文
|
||||
|
Graphics2D g2d = resultImage.createGraphics(); |
||||
|
|
||||
|
// 绘制目标图片
|
||||
|
g2d.drawImage(targetImage, 0, 0, null); |
||||
|
|
||||
|
// 计算旋转图片的位置(居中)
|
||||
|
int x = (targetImage.getWidth() - rotatedImage.getWidth()) / 2; |
||||
|
int y = (targetImage.getHeight() - rotatedImage.getHeight()) / 2; |
||||
|
|
||||
|
// 绘制旋转后的图片
|
||||
|
g2d.drawImage(rotatedImage, x, y, null); |
||||
|
|
||||
|
// 释放图形上下文
|
||||
|
g2d.dispose(); |
||||
|
|
||||
|
// 转换为Base64
|
||||
|
return convertToBase64(resultImage); |
||||
|
|
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 旋转图片 |
||||
|
* @param image 要旋转的图片 |
||||
|
* @param angle 旋转角度(顺时针) |
||||
|
* @return 旋转后的图片 |
||||
|
*/ |
||||
|
private static BufferedImage rotateImage(BufferedImage image, double angle) { |
||||
|
double rads = Math.toRadians(angle); |
||||
|
double sin = Math.abs(Math.sin(rads)); |
||||
|
double cos = Math.abs(Math.cos(rads)); |
||||
|
|
||||
|
int w = image.getWidth(); |
||||
|
int h = image.getHeight(); |
||||
|
|
||||
|
int newWidth = (int) Math.floor(w * cos + h * sin); |
||||
|
int newHeight = (int) Math.floor(h * cos + w * sin); |
||||
|
|
||||
|
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); |
||||
|
Graphics2D g2d = rotated.createGraphics(); |
||||
|
|
||||
|
g2d.translate((newWidth - w) / 2, (newHeight - h) / 2); |
||||
|
g2d.rotate(rads, w / 2, h / 2); |
||||
|
g2d.drawImage(image, 0, 0, null); |
||||
|
g2d.dispose(); |
||||
|
|
||||
|
return rotated; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将图片转换为Base64字符串 |
||||
|
* @param image 要转换的图片 |
||||
|
* @return Base64字符串 |
||||
|
*/ |
||||
|
private static String convertToBase64(BufferedImage image) { |
||||
|
try { |
||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||
|
ImageIO.write(image, "jpeg", outputStream); |
||||
|
byte[] imageBytes = outputStream.toByteArray(); |
||||
|
return Base64.getEncoder().encodeToString(imageBytes); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 使用示例
|
||||
|
public static void main(String[] args) { |
||||
|
String sourceImagePath = "C:\\Users\\Administrator\\Desktop\\1.png"; // 源图片路径
|
||||
|
String targetImagePath = "C:\\Users\\Administrator\\Desktop\\2.png"; // 目标图片路径
|
||||
|
double angle = 90; // 旋转角度
|
||||
|
|
||||
|
String base64Result = rotateAndOverlayImage(sourceImagePath, targetImagePath, angle); |
||||
|
if (base64Result != null) { |
||||
|
System.out.println("处理成功,Base64结果:" + base64Result); |
||||
|
} else { |
||||
|
System.out.println("处理失败"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 12 KiB |
Loading…
Reference in new issue