我想水平翻转原始图像并在同一文件夹中创建图像,但是不会创建新图像。预先感谢
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
公共类ImagesFlipHorizontally {
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;
}
}
答案 0 :(得分:0)
除了进行xx >= 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