【发布时间】:2015-08-18 20:05:17
【问题描述】:
我开发了一个游戏,旋转图像目前大部分时间都花在了计算一帧的过程中。为了优化,我正在寻找旋转缓冲图像的最快方法。我已经尝试了下面显示的两种方法。
最慢的方法:
public static BufferedImage rotate(BufferedImage imgOld, int deg){ //Parameter for this method are the picture to rotate and the rotation in degrees
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(deg), (int)(imgOld.getWidth()/2), (int)(imgOld.getHeight()/2)); //initialize and configure transformation
BufferedImage imgNew = new BufferedImage(imgOld.getWidth(), imgOld.getHeight(), imgOld.getType()); //create new bufferedimage with the properties of the image to rotate
Graphics2D g = (Graphics2D) imgNew.getGraphics(); //create Graphics
g.setTransform(at); //apply transformation
g.drawImage(imgOld, 0, 0, null); //draw rotated image
g.dispose();
imgOld.flush();
return imgNew;
}
稍微快一点的方法:
public static BufferedImage rotate(BufferedImage imgOld, int deg){ //parameter same as method above
BufferedImage imgNew = new BufferedImage(imgOld.getWidth(), imgOld.getHeight(), imgOld.getType()); //create new buffered image
Graphics2D g = (Graphics2D) imgNew.getGraphics(); //create new graphics
g.rotate(QaDMath.toRadians(deg), imgOld.getWidth()/2, imgOld.getHeight()/2); //configure rotation
g.drawImage(imgOld, 0, 0, null); //draw rotated image
return imgNew; //return rotated image
}
}
我发现了许多与旋转图像相关的主题,但没有一个讨论最快、最多的解决方案。 我希望我没有错过任何主题,这不是重复的。
希望有比我更熟练的人知道解决方案
【问题讨论】:
-
您需要在运行时旋转它们吗?您是否可以提前生成您需要的旋转图像,然后只使用旋转适当数量的版本?
-
我也想过这个选项,但担心几千张图片对于java使用的内存来说太多了
标签: java image optimization rotation bufferedimage