我正在寻找 javax.imageio 包的一个很好的替代方案,它可以让我对图像进行简单的旋转、剪切和缩放操作。例如,我想做
int angle, height, width;
image.rotateRight(angle).scale(height, width);
为了获得向右旋转角度度并缩小到高x宽像素的图像。
使用 Graphics2D 和 BufferedImages,我将不得不这样做,这既不可读,也不容易编写:
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = result.createGraphics();
graphics.translate(height/2, width/2);
graphics.rotate(angle);
graphics.translate(-width/2, -height/2);
graphics.drawImage(image, 0, 0, width, height, null);
(实际上,该代码甚至没有考虑非方形图像,这将需要我在翻译中做更多的魔法)。