- 图像水平镜像
x1=w-x0
y1=y0
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 图像水平镜像
* x1=w-x0
* y1=y0
* */
public class ch5_3 {
public static void main(String[] args) throws Exception {
long t1=System.currentTimeMillis(); //获取程序运行前的时间,单位毫秒
BufferedImage bi=ImageIO.read(new File("1.jpg")); //读取图像
BufferedImage nbi=mirrorHor(bi); //处理图像
ImageIO.write(nbi, "jpg", new File("1b.jpg")); //输出图像
long t2=System.currentTimeMillis(); //获取程序运行后的时间
System.out.println("程序运行"+(t2-t1)+"毫秒"); //程序结束后进行提示
}
/**
* 图像水平镜像
* @param BufferedImage bi 原始图像
* @return BufferedImage 变换后图像
* */
public static BufferedImage mirrorHor(BufferedImage bi) {
int w=bi.getWidth(); //得到图像的宽度
int h=bi.getHeight(); //得到图像的高度
BufferedImage nbi=new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
//创建新图像(临时图像变量)宽度和高度跟原图相同
//循环遍历每一个像素点
for(int y1=0;y1<h;y1++) {
for(int x1=0;x1<w;x1++) {
//计算输出图像坐标(x,y)所对应原图中的坐标(x0,y0)位置
int x0=w-1-x1;
int y0=y1;
int rgb=bi.getRGB(x0, y0);
nbi.setRGB(x1, y1, rgb); //设置输出图像坐标为(x,y)的像素值
}
}
return nbi;
}
}