分享

java图片上传剪切图片整套实现方法(2)

 青_春 2016-03-18

html页面:http:///projects/imgareaselect/

2、上传好图片后,要根据自己的需要进行剪切,处理方法为:

//剪切图片
  public String clippingPicture(){
   HttpServletRequest request = ServletActionContext.getRequest();
   HttpSession session = request.getSession();
   try{
    String powerStr = session.getAttribute("power").toString();
    int power=0;
    if(powerStr==null){
     request.setAttribute("failMsg", "异常操作,用户已退出,请重新登录!");
     return "login";
    }else{
     power = Integer.parseInt(powerStr);
    }
    if(power==2){
     //对图片进行剪切处理
     Driver_detail detail = dds.getDriver_detailByDriverId(driverId);
     String pimgurl = detail.getImageUrl();
        //要删除的图片绝对路径
     String savePath = ServletActionContext.getServletContext().getRealPath("");
     // 获取项目根路径  
     savePath = savePath.replace("\", "\\\\");
     savePath = savePath + "\\\\uploadImages\\\\";
        String imgAbl = savePath+pimgurl;
        OperateImage o = new OperateImage( x , y , width , height );
        o.setSrcpath(imgAbl);
        o.setSubpath(imgAbl);
        o.cut();
        //对图片进行缩放处理
        Driver_detail detail1 = dds.getDriver_detailByDriverId(driverId);
     String pimgurl1 = detail1.getImageUrl();
        //要删除的图片绝对路径
     String savePath1 = ServletActionContext.getServletContext().getRealPath("");
     // 获取项目根路径  
     savePath1 = savePath1.replace("\", "\\\\");
     savePath1 = savePath1 + "\\\\uploadImages\\\\";
        String imgAbl1 = savePath1+pimgurl1;
        Resize r= new Resize();
        BufferedImage b;
        b = ImageIO.read(new FileInputStream(imgAbl1));
        BufferedImage bb = r.rize(b,100,100);
        ImageIO.write(bb, "jpg" , new File(imgAbl1));
     return "showDriversAction";
    }else{
     request.setAttribute("failMsg", "异常操作,用户已退出,请重新登录!");
     return "login";
    }
   }catch (CannotCreateTransactionException e) {
    
    request.setAttribute("failMsg", "数据库连接失败,请联系管理员");
    return "login";
    
   }catch (HibernateException e) {
    request.setAttribute("failMsg", "更改司机头像出错,请重新操作");
    return "showDriversAction";
   }catch (Exception e) {
    request.setAttribute("failMsg", "更改司机头像出错,请重新操作");
    return "showDriversAction";
   }
   
  }

 

在方法中用到了几个关键的类:

OperateImage.java剪切图片类:

package mobi.zhangsheng.jiejia.util;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import mobi.zhangsheng.jiejia.domain.Company_detail;

import org.apache.struts2.ServletActionContext;

public class OperateImage {
  // ===源图片路径名称如:c:\1.jpg
    private String srcpath;

    // ===剪切图片存放路径名称。如:c:\2.jpg

    private String subpath ;

    // ===剪切点x坐标
    private int x ;

    private int y ;

    // ===剪切点宽度

    private int width ;

    private int height ;

    public OperateImage() {

    }

    public OperateImage( int x, int y, int width, int height) {

    this.x = x ;

    this.y = y ;

    this.width = width ;

    this.height = height ;

    }

  

    public void cut() throws IOException {

    FileInputStream is = null ;

    ImageInputStream iis = null ;

    try {

    // 读取图片文件

    is = new FileInputStream(srcpath);
  

    Iterator < ImageReader > it = ImageIO.getImageReadersByFormatName("jpg");

    ImageReader reader = it.next();
    // 获取图片流
 

    iis = ImageIO.createImageInputStream(is);

  

    reader.setInput(iis, true);


    ImageReadParam param = reader.getDefaultReadParam();

  
 
    Rectangle rect = new Rectangle(x, y, width, height);

    // 提供一个 BufferedImage,将其用作解码像素数据的目标。

    param.setSourceRegion(rect);

  
   
    BufferedImage bi = reader.read(0 ,param);

    // 保存新图片

    ImageIO.write(bi, "jpg" , new File(subpath));

    } finally {

    if (is != null)

    is.close() ;

    if (iis != null)

    iis.close();

    }

    }

    public int getHeight() {

    return height;

    }

    public void setHeight(int height) {

    this .height = height;

    }

    public String getSrcpath() {

    return srcpath;

    }

    public void setSrcpath(String srcpath) {

    this .srcpath = srcpath;

    }

    public String getSubpath() {

    return subpath;

    }

    public void setSubpath(String subpath) {

    this .subpath = subpath;

    }

    public int getWidth(){

    return width;

    }

    public void setWidth(int width) {

    this .width = width;

    }

    public int getX() {

    return x;

    }

    public void setX( int x) {

    this .x = x;

    }

    public int getY() {

    return y;

    }

    public void setY(int y) {

    this .y = y;

    }

//    public static void main(String[] args) throws Exception {
//
//    String name = "F:\\myeclipse\\Tomcat 6.0\\webapps\\daijiayun\\uploadImages\\2012092113512099056.jpg" ;
//
//    OperateImage o = new OperateImage( 50 , 50 , 150 , 150 );
//
//    o.setSrcpath(name);
//
//    o.setSubpath( "F:\\myeclipse\\Tomcat 6.0\\webapps\\daijiayun\\uploadImages\\2012092113512099056.jpg" );
//   
//    o.cut();
//   
//   
//
 //   }

}

 

 

Resize 缩放图片类,将图片统一缩放为100×100的大小:

package mobi.zhangsheng.jiejia.util;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

public class Resize {
 BufferedImage bufImage;
 int width;
 int height;

 public Resize() {
  // TODO Auto-generated constructor stub
 }

 public Resize(String srcPath, int width, int height) {
  this.width = width;
  this.height = height;
  try {
   this.bufImage = ImageIO.read(new File(srcPath));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static BufferedImage rize(BufferedImage srcBufImage, int width,
   int height) {

  BufferedImage bufTarget = null;

  double sx = (double) width / srcBufImage.getWidth();
  double sy = (double) height / srcBufImage.getHeight();

  int type = srcBufImage.getType();
  if (type == BufferedImage.TYPE_CUSTOM) {
   ColorModel cm = srcBufImage.getColorModel();
   WritableRaster raster = cm.createCompatibleWritableRaster(width,
     height);
   boolean alphaPremultiplied = cm.isAlphaPremultiplied();
   bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
  } else
   bufTarget = new BufferedImage(width, height, type);

  Graphics2D g = bufTarget.createGraphics();
  g.setRenderingHint(RenderingHints.KEY_RENDERING,
    RenderingHints.VALUE_RENDER_QUALITY);
  g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx,
    sy));
  g.dispose();
  return bufTarget;
 }
 
// public static void main(String[] args) {
//  
//  Resize r= new Resize();
//  BufferedImage b;
//  try {
//   b = ImageIO.read(new FileInputStream("f:\\flower2.jpg"));
//   BufferedImage bb = Resize.rize(b,100,100);
//   ImageIO.write(bb, "jpg" , new File("f:\\flower222222.jpg"));
//   System.out.println(bb);
//  } catch (FileNotFoundException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  } catch (IOException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
// }

}

 

希望这个功能够为大家提供帮助,这些是主要的函数,如需要帮助QQ:1037139984。

写的不够详细,还请大家见谅。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约