Merge remote-tracking branch 'origin/dev'
# Conflicts: # schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java
This commit is contained in:
@@ -34,11 +34,6 @@ public class RotateImageUtils {
|
|||||||
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, bufferedImage.getType());
|
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, bufferedImage.getType());
|
||||||
// 创建一个 Graphics2D 对象,用于绘制新图片
|
// 创建一个 Graphics2D 对象,用于绘制新图片
|
||||||
Graphics2D graphics = outputImage.createGraphics();
|
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();
|
graphics.dispose();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
|
||||||
ImageIO.write(outputImage, "png", baos);//写入流中
|
ImageIO.write(outputImage, "png", baos);//写入流中
|
||||||
@@ -76,37 +71,35 @@ public class RotateImageUtils {
|
|||||||
* @author zlg
|
* @author zlg
|
||||||
* @date: 2024/5/9 13:14
|
* @date: 2024/5/9 13:14
|
||||||
*/
|
*/
|
||||||
public BufferedImage rotateImage(BufferedImage image, double theta) {
|
public BufferedImage rotateImage(BufferedImage image, double theta) throws IOException {
|
||||||
int width = image.getWidth();
|
BufferedImage originalImage = image;
|
||||||
int height = image.getHeight();
|
int width = originalImage.getWidth();
|
||||||
double angle = theta * Math.PI / 180; // 度转弧度
|
int height = originalImage.getHeight();
|
||||||
double[] xCoords = getX(width / 2, height / 2, angle);
|
// 创建新的BufferedImage对象,设置类型为ARGB_8888
|
||||||
double[] yCoords = getY(width / 2, height / 2, angle);
|
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||||
int WIDTH = (int) (xCoords[3] - xCoords[0]);
|
// 使用Graphics2D对象将原始图片绘制到新的BufferedImage对象上,同时设置背景色为透明色
|
||||||
int HEIGHT = (int) (yCoords[3] - yCoords[0]);
|
Graphics2D g2d = newImage.createGraphics();
|
||||||
BufferedImage resultImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
|
g2d.setBackground(new Color(0, 0, 0, 0)); // 设置背景色为透明色
|
||||||
for (int i = 0; i < WIDTH; i++) {
|
g2d.clearRect(0, 0, width, height); // 清除画布
|
||||||
for (int j = 0; j < HEIGHT; j++) {
|
g2d.drawImage(originalImage, 0, 0,null); // 绘制原始图片
|
||||||
int x = i - WIDTH / 2;
|
g2d.dispose();
|
||||||
int y = HEIGHT / 2 - j;
|
// 对新的BufferedImage对象进行旋转操作
|
||||||
double radius = Math.sqrt(x * x + y * y);
|
double angle = Math.toRadians(theta);
|
||||||
double angle1;
|
int newWidth = (int) (height * Math.cos(angle) + width * Math.sin(angle));
|
||||||
if (y > 0) {
|
int newHeight = (int) (height * Math.sin(angle) + width * Math.cos(angle));
|
||||||
angle1 = Math.acos(x / radius);
|
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
|
||||||
} else {
|
Graphics2D g2dRotated = rotatedImage.createGraphics();
|
||||||
angle1 = 2 * Math.PI - Math.acos(x / radius);
|
g2dRotated.translate((newWidth - width) / 2, (newHeight - height) / 2);
|
||||||
}
|
g2dRotated.rotate(angle, width / 2, height / 2);
|
||||||
x = (int) Math.round(radius * Math.cos(angle1 - angle));
|
g2dRotated.drawImage(newImage, 0, 0,null);
|
||||||
y = (int) Math.round(radius * Math.sin(angle1 - angle));
|
g2dRotated.dispose();
|
||||||
if (x < (width / 2) & x > -(width / 2) & y < (height / 2) & y > -(height / 2)) {
|
int newSize = 160; // 新的大小为500像素
|
||||||
int rgb = image.getRGB((int) Math.round(x + width / 2), (int) Math.round(height / 2 - y));
|
BufferedImage resizedImage = new BufferedImage(newSize, newSize, BufferedImage.TYPE_INT_ARGB);
|
||||||
resultImage.setRGB(i, j, rgb);
|
Graphics2D g2dResized = resizedImage.createGraphics();
|
||||||
} else {
|
g2dResized.drawImage(rotatedImage, 0, 0, newSize, newSize, null);
|
||||||
resultImage.setRGB(i, j, -1);
|
g2dResized.dispose();
|
||||||
}
|
// 将调整大小后的图像保存到文件中
|
||||||
}
|
return resizedImage;
|
||||||
}
|
|
||||||
return resultImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取四个角点旋转后Y方向坐标
|
// 获取四个角点旋转后Y方向坐标
|
||||||
|
Reference in New Issue
Block a user