【问题标题】:Writing image by flipping original image horizontally通过水平翻转原始图像来写入图像
【发布时间】:2019-08-19 07:51:58
【问题描述】:

我想水平翻转原始图像并在同一文件夹中创建图像,但它不会创建新图像。提前致谢

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

公共类 ImagesFlipHorizo​​ntally {

public static void main(String[] args) throws IOException {

    File location1 = new File("E:\\Users/Peter/Downloads/moon1.jpg");
    BufferedImage image = ImageIO.read(location1);

    File location2 = new File("E:\\Users/Peter/Downloads/moon1mirror.jpg");
    int width = image.getWidth();
    int height = image.getHeight();
    BufferedImage mirror = mirrorimage (image, width, height);
    ImageIO.write(mirror, "jpg", location2);

}


    private static BufferedImage mirrorimage (BufferedImage img, int w, int h) {

        BufferedImage horizontallyflipped = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        for (int xx = w-1; xx > 0; xx--) {
            for (int yy = 0; yy < h; yy++) {

                img.setRGB(w-xx, yy, img.getRGB(xx, yy));

            }
        }

        return horizontallyflipped;

    }

}

【问题讨论】:

  • 你对水平翻转的对象什么也没做
  • 不应该是img.setRGB(...) 而是horizontallyflipped.setRGB(...)

标签: java


【解决方案1】:

除了做xx &gt;= 0之外,交换没有完成。

private static BufferedImage mirrorimage (BufferedImage img, int w, int h) {
    BufferedImage horizontallyflipped = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    for (int yy = 0; yy < h; yy++) {
        for (int xx = 0; xx < w/2; xx++) {
            int c = img.getRGB(xx, yy);
            img.setRGB(xx, yy, img.getRGB(w - 1 - xx, yy));
            img.setRGB(w - 1 - xx, yy, c);
        }
    }
    return horizontallyflipped;
}

有更快的方法,比如Flip Image with Graphics2D

【讨论】:

  • 谢谢楼主,我去看看
猜你喜欢
  • 2019-11-26
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多