【发布时间】:2016-11-09 11:23:36
【问题描述】:
我尝试使用setRGB 和BufferedImage 在Java 中旋转图像,但得到一个奇怪的结果。有人知道为什么吗?
BufferedImage pic1 = ImageIO.read(new File("Images/Input-1.bmp"));
int width = pic1.getWidth(null);
int height = pic1.getHeight(null);
double angle = Math.toRadians(90);
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double x0 = 0.5 * (width - 1); // point to rotate about
double y0 = 0.5 * (height - 1); // center of image
BufferedImage pic2 = pic1;
// rotation
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double a = x - x0;
double b = y - y0;
int xx = (int) (+a * cos - b * sin + x0);
int yy = (int) (+a * sin + b * cos + y0);
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
pic2.setRGB(x, y, pic1.getRGB(xx, yy));
}
}
}
ImageIO.write(pic2, "bmp", new File("Images/Output2.bmp"));
LEFT 一侧是原始图片,RIGHT 一侧是我的结果。有谁知道我该如何解决?
感谢您的帮助。
【问题讨论】:
-
您无法原地旋转图像。您制作另一个单独的旋转图像。如果要将图像旋转 90 度,则不必进行旋转数学运算。只需将 x 坐标复制到新的 y 坐标,将 y 坐标复制到新的 x 坐标即可。
标签: java image image-processing bufferedimage