gpt4 book ai didi

java - java中将图像向右旋转90度

转载 作者:行者123 更新时间:2023-12-02 11:07:21 27 4
gpt4 key购买 nike

我无法将图像向右旋转 90 度。我需要能够在java中单独旋转图像。唯一的事情。不幸的是,我需要在特定点绘制图像,并且没有带有参数的方法:1. 单独旋转图像,2. 允许我设置 x 和 y。感谢任何帮助

public class Tumbler extends GraphicsProgram{

public void run() {
setSize(1000,1000);
GImage original = new GImage("sunset.jpg");
add(original, 10, 10);
int[][] pixels = original.getPixelArray();
int height = pixels.length;
int width = pixels[0].length;

// Your code starts here
int newheight = width;
int newwidth = height;
int[][] newpixels = new int[newheight][newwidth];

for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
newpixels[j][height-1-i] = pixels[i][j];
}
}


GImage image = new GImage(newpixels);
add(image, width+20, 10);

// Your code ends here
}

最佳答案

如果我们想要获得不错的性能,我们绝对应该使用 Graphics2D(与直接复制像素相比快约 10 倍):

public static BufferedImage rotateClockwise90(BufferedImage src) {
int width = src.getWidth();
int height = src.getHeight();

BufferedImage dest = new BufferedImage(height, width, src.getType());

Graphics2D graphics2D = dest.createGraphics();
graphics2D.translate((height - width) / 2, (height - width) / 2);
graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
graphics2D.drawRenderedImage(src, null);

return dest;
}

关于java - java中将图像向右旋转90度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20959796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com