以图片左上顶点为远点,剪成一张width和height的图片(仅限JPG的图)

 

public   String   getJSpecialSmallJPG(String fullname,int width,int height,String type)   throws   Exception{
        File   _file   =   new   File(fullname); 
        String newsrc = _file.getParent()+type+_file.getName();
        FileInputStream is = null ;

        ImageInputStream iis =null ;
        Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
        is = new FileInputStream(fullname);
       ImageReader reader = (ImageReader)readers.next();
       iis = ImageIO.createImageInputStream(is);
       reader.setInput(iis,true);
       ImageReadParam param = reader.getDefaultReadParam();
      
      
       Image   src   =   javax.imageio.ImageIO.read(_file);                                           //构造Image对象  
       int   x=src.getWidth(null);     //原图的宽                                                                     //得到源图宽  
       int   y=src.getHeight(null);    //原图的高

       int tempheight=0;
       tempheight=(int) (height*x/width);
       int width1=0;
       int height1=0;
       if(tempheight>y){
           width1=(int)(width*y/height);
           height1=y;
       }else{
           height1=(int) (x*height/width);
           width1=x;
       }
       Rectangle rect = new Rectangle(0, 0, width1, height1);
       param.setSourceRegion(rect);
      
       BufferedImage bi = reader.read(0,param);
       ImageIO.write(bi, "jpg", new File(newsrc));
       return newsrc;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值