图像水平镜像

  • 图像水平镜像
    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;
        }

}            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值