fix: 图片旋转背景透明

This commit is contained in:
zlg
2024-06-06 14:42:33 +08:00
parent 27225181f6
commit 1f0844cb76

View File

@@ -34,11 +34,6 @@ public class RotateImageUtils {
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, bufferedImage.getType());
// 创建一个 Graphics2D 对象,用于绘制新图片
Graphics2D graphics = outputImage.createGraphics();
// 如果原始图片尺寸大于目标尺寸,则进行压缩
if (originalWidth > targetWidth || originalHeight > targetHeight) {
Image scaledImage = bufferedImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
graphics.drawImage(scaledImage, 0, 0, null);
}
graphics.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(outputImage, "png", baos);//写入流中
@@ -76,37 +71,35 @@ public class RotateImageUtils {
* @author zlg
* @date: 2024/5/9 13:14
*/
public BufferedImage rotateImage(BufferedImage image, double theta, Color backgroundColor) {
int width = image.getWidth();
int height = image.getHeight();
double angle = theta * Math.PI / 180; // 度转弧度
double[] xCoords = getX(width / 2, height / 2, angle);
double[] yCoords = getY(width / 2, height / 2, angle);
int WIDTH = (int) (xCoords[3] - xCoords[0]);
int HEIGHT = (int) (yCoords[3] - yCoords[0]);
BufferedImage resultImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
int x = i - WIDTH / 2;
int y = HEIGHT / 2 - j;
double radius = Math.sqrt(x * x + y * y);
double angle1;
if (y > 0) {
angle1 = Math.acos(x / radius);
} else {
angle1 = 2 * Math.PI - Math.acos(x / radius);
}
x = (int) Math.round(radius * Math.cos(angle1 - angle));
y = (int) Math.round(radius * Math.sin(angle1 - angle));
if (x < (width / 2) & x > -(width / 2) & y < (height / 2) & y > -(height / 2)) {
int rgb = image.getRGB((int) Math.round(x + width / 2), (int) Math.round(height / 2 - y));
resultImage.setRGB(i, j, rgb);
} else {
resultImage.setRGB(i, j, -1);
}
}
}
return resultImage;
public BufferedImage rotateImage(BufferedImage image, double theta) throws IOException {
BufferedImage originalImage = image;
int width = originalImage.getWidth();
int height = originalImage.getHeight();
// 创建新的BufferedImage对象设置类型为ARGB_8888
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// 使用Graphics2D对象将原始图片绘制到新的BufferedImage对象上同时设置背景色为透明色
Graphics2D g2d = newImage.createGraphics();
g2d.setBackground(new Color(0, 0, 0, 0)); // 设置背景色为透明色
g2d.clearRect(0, 0, width, height); // 清除画布
g2d.drawImage(originalImage, 0, 0,null); // 绘制原始图片
g2d.dispose();
// 对新的BufferedImage对象进行旋转操作
double angle = Math.toRadians(theta);
int newWidth = (int) (height * Math.cos(angle) + width * Math.sin(angle));
int newHeight = (int) (height * Math.sin(angle) + width * Math.cos(angle));
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2dRotated = rotatedImage.createGraphics();
g2dRotated.translate((newWidth - width) / 2, (newHeight - height) / 2);
g2dRotated.rotate(angle, width / 2, height / 2);
g2dRotated.drawImage(newImage, 0, 0,null);
g2dRotated.dispose();
int newSize = 160; // 新的大小为500像素
BufferedImage resizedImage = new BufferedImage(newSize, newSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2dResized = resizedImage.createGraphics();
g2dResized.drawImage(rotatedImage, 0, 0, newSize, newSize, null);
g2dResized.dispose();
// 将调整大小后的图像保存到文件中
return resizedImage;
}
// 获取四个角点旋转后Y方向坐标