图片镜像翻转 Java

前言

开发软件时,icon有时候找不到对应的翻转图,所以我借鉴了网上一段程序来进行翻转

代码

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

public class ImageMirror {
    public static void main(String[] args) throws IOException{
        File file = null;
        BufferedImage image = null;

        try {
            file = new File("C:/.../icon.png");
            image = ImageIO.read(file);

            int width = image.getWidth();
            int height = image.getHeight();

            for (int j = 0; j < height; j++) {
                int l = 0, r = width - 1;
                while (l < r) {
                    int pl = image.getRGB(l, j);
                    int pr = image.getRGB(r, j);

                    image.setRGB(l, j, pr);
                    image.setRGB(r, j, pl);

                    l++;
                    r--;
                }
            }

            file = new File("C:/.../icon_mirror.png");
            ImageIO.write(image, "png", file);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java)