我希望将从文件加载的图像旋转90度。我有代码,但是当我使用它时,出现错误,提示坐标超出范围。任何帮助,将不胜感激。谢谢。
这是我到目前为止编写的方法:
public void rotateImage(OFImage image)
{
if(currentImage != null) {
int width = image.getWidth();
int height = image.getHeight();
OFImage newImage = new OFImage(width, height);
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
Color col = image.getPixel(i, j);
newImage.setPixel(height - j - 2, i, col);
}
}
image = newImage;
}
}
答案 0 :(得分:0)
当您将图像旋转一定角度时,生成的图像会比原始图像大。旋转45度时得到的最大图像尺寸:
创建新图像时,您必须根据旋转后的大小设置其尺寸:
public BufferedImage rotateImage(BufferedImage image, double angle) {
double radian = Math.toRadians(angle);
double sin = Math.abs(Math.sin(radian));
double cos = Math.abs(Math.cos(radian));
int width = image.getWidth();
int height = image.getHeight();
int nWidth = (int) Math.floor((double) width * cos + (double) height * sin);
int nHeight = (int) Math.floor((double) height * cos + (double) width * sin);
BufferedImage rotatedImage = new BufferedImage(
nWidth, nHeight, BufferedImage.TYPE_INT_ARGB);
// and so on...
return rotatedImage;
}