From 1f0844cb76bd7bc8248b06c3ffb998ff09fe3ee3 Mon Sep 17 00:00:00 2001 From: zlg <482370576@qq.com> Date: Thu, 6 Jun 2024 14:42:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9B=BE=E7=89=87=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E9=80=8F=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/common/utils/RotateImageUtils.java | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java index 5bf47e6..a6a67d9 100644 --- a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java @@ -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方向坐标