我有不同尺寸的文档图像,我希望能够以以下方式有效地缩放和旋转它们(标准“旋转”和“缩放”逻辑)。我该怎么做?
图像高 H 像素,宽 W 像素。最初,它应该缩放到 600 像素宽。每次旋转时,面板的宽度和高度应交换,缩放后的图像应旋转 90 度。在每次缩放时,图像应按“比例”因子缩放。
这是我迄今为止尝试过的BufferedImage img
……结果 BufferedImage 缩放和旋转但不平移(旋转 90 度后在面板顶部居中):
double scale = zoom * 600.0 / img.getWidth();
rotation = (rotation + degrees) % 360;
int scaledWidth = (int)(scale * img.getWidth());
int scaledHeight = (int)(scale * img.getHeight());
BufferedImage bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
if (rotation % 180 == 0)
bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
else
bufferedImage = new BufferedImage(scaledHeight, scaledWidth, img.getType());
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(rotation), scaledWidth/2, scaledHeight/2);
transform.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
scaledImage = operation.filter(img, bufferedImage);
imagePanel.setPreferredSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));