Java图片剪切

package com.sony.vaiogate.ads.common.util;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class ImageTools {
   
 /**cut out picture*/
 public static File cutting(String path,File file,int x,int y,int width,int height){
   try{
    String endName=file.getName();
    endName=endName.substring(endName.lastIndexOf(".")+1);
    Iterator<ImageReader> readers=ImageIO.getImageReadersByFormatName(endName);
    ImageReader reader = (ImageReader)readers.next();
    InputStream is=new FileInputStream(file);
    ImageInputStream iis = ImageIO.createImageInputStream(is);
    reader.setInput(iis,true);
    ImageReadParam param = reader.getDefaultReadParam();
    Rectangle rect = new Rectangle(x,y,width,height);
    param.setSourceRegion(rect);
             BufferedImage bi = reader.read(0,param);
             File newFile=new File(path+"c_"+file.getName());
             //if(!newFile.exists())newFile.mkdirs();
             ImageOutputStream out=ImageIO.createImageOutputStream(new FileOutputStream(newFile));
             ImageIO.write(bi,endName,out);
             file=newFile;
   
   }catch(Exception e){
    e.printStackTrace();
   }
   return file;
 }
 public static String CutOutPicture(String path,String pictureName,int previewWidth,int prebiweHeight){
   
          BufferedImage image = null;  
          try {  
          File file=new File(path+pictureName);
             image = ImageIO.read(file);
             int pictureWidth = image.getWidth();// width
             int pictureHeight = image.getHeight();// height
             int cutWidth = 0; //cut width
             int cutHeight = 0;//cut height
             int xWidth  = 0;  //start cut x
             int yHeight = 0;  //start cut y

             if((float)pictureWidth/(float)pictureHeight > (float)previewWidth/(float)prebiweHeight){
              cutWidth = (int)((float)pictureHeight * (float)previewWidth / (float)prebiweHeight);
              xWidth = (pictureWidth-cutWidth)/2;
              cutHeight = pictureHeight;
             }
             if((float)pictureWidth/(float)pictureHeight < (float)previewWidth/(float)prebiweHeight){
              cutHeight = (int)((float)pictureWidth * (float)prebiweHeight / (float)previewWidth);
              yHeight = (pictureHeight-cutHeight)/2;
              cutWidth = pictureWidth;
             }
             System.out.println("pictureWidth=" + pictureWidth);  
             System.out.println("pictureHeight=" + pictureHeight); 
             System.out.println("xWidth=" + xWidth);  
             System.out.println("yHeight=" + yHeight);
             System.out.println("cutWidth=" + cutWidth);  
             System.out.println("cutHeight=" + cutHeight);
             ImageTools.cutting(path,file,xWidth,yHeight,cutWidth,cutHeight);
          } catch (Exception e) {  
             e.printStackTrace();  
          }  
  return null;
 }
 public static void main(String args[]){
      String pictureName = "xx1.jpg";
      String path = "D:/";
      int previewWidth  = 114; //previewWidth
      int prebiweHeight = 64; //prebiweHeight
   ImageTools.CutOutPicture(path,pictureName,previewWidth,prebiweHeight);
 }
 /*
        //Draw picture.
        {
            int iWidth = m_pBitmap->GetWidth();
            int iHeight = m_pBitmap->GetHeight();

            CRect rct(0, 0, iWidth, iHeight);
            if ((float)rct.Width() / (float)rct.Height() > (float)m_rctClient.Width() / (float)m_rctClient.Height())
            {
                iWidth = (int)((float)iHeight * (float) m_rctClient.Width() / (float)m_rctClient.Height());
                int iMargin = (rct.Width() - iWidth) / 2;
                rct.left += iMargin;
                rct.right -= iMargin;
            }

            if ((float)rct.Width() / (float)rct.Height() < (float)m_rctClient.Width() / (float)m_rctClient.Height())
            {
                iHeight = (int)((float)iWidth * (float)m_rctClient.Height() / (float)m_rctClient.Width());
                int iMargin = (rct.Height() - iHeight) / 2;
                rct.top += iMargin;
                rct.bottom -= iMargin;
            }
            pRT->DrawBitmap(m_pBitmap, GetRect_2(m_rctClient), GetRect_2(rct), 1.0F, FALSE);
        }
   */
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lb8607

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值