我已经尝试使用setRGB
和BufferedImage
旋转Java中的图片,但我得到一个奇怪的结果。有没有人知道为什么?的BufferedImage setRGB奇怪的结果
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坐标。 –