BufferedImage setRGB 奇怪的结果

BufferedImage setRGB strange result

我尝试使用 setRGBBufferedImage 旋转 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 一侧是我的结果。有谁知道我该如何解决?

感谢您的帮助。

问题是您使用的是同一张图像作为输入和输出:

BufferedImage pic2 = pic1;

您必须为 pic2 创建另一个图像,然后进行旋转,将像素从 Image1 复制到 Image2。

但是请注意,使用 getRGB 和 setRGB 速度非常慢。如果你直接操作像素,它会快 100 倍。