1.下载最新的Jcrop文件。
http://deepliquid.com/content/Jcrop.html

 
  
  1. public class Utils {   
  2.    
  3.     public static String getExtension(File f) {    
  4.         return (f != null) ? getExtension(f.getName()) : "";    
  5.     }    
  6.    
  7.     public static String getExtension(String filename) {    
  8.         return getExtension(filename, "");    
  9.     }    
  10.    
  11.     public static String getExtension(String filename, String defExt) {    
  12.         if ((filename != null) && (filename.length() > 0)) {    
  13.             int i = filename.lastIndexOf('.');    
  14.    
  15.             if ((i >-1) && (i < (filename.length() - 1))) {    
  16.                 return filename.substring(i + 1);    
  17.             }    
  18.         }    
  19.         return defExt;    
  20.     }    
  21.    
  22.     public static String trimExtension(String filename) {    
  23.         if ((filename != null) && (filename.length() > 0)) {    
  24.             int i = filename.lastIndexOf('.');    
  25.             if ((i >-1) && (i < (filename.length()))) {    
  26.                 return filename.substring(0, i);    
  27.             }    
  28.         }    
  29.         return filename;    
  30.     }    
  31. }   


 
  
  1. public class SaveImage{ 
  2. /**    
  3.   * 保存图片    
  4.   * @param img       原图路径    
  5.   * @param dest      目标图路径    
  6.   * @param top       选择框的左边y坐标    
  7.   * @param left      选择框的左边x坐标    
  8.   * @param width     选择框宽度    
  9.   * @param height    选择框高度    
  10.   * @return    
  11.   * @throws IOException    
  12.   */     
  13.  public static boolean saveImage(File img,       
  14.             String dest,       
  15.             int top,       
  16.             int left,       
  17.             int width,       
  18.             int height) throws IOException {      
  19.   File fileDest = new File(dest);      
  20.   if(!fileDest.getParentFile().exists())      
  21.       fileDest.getParentFile().mkdirs();      
  22.   String ext = Utils.getExtension(dest).toLowerCase();      
  23.   BufferedImage bi = (BufferedImage)ImageIO.read(img);      
  24.   height = Math.min(height, bi.getHeight());      
  25.   width = Math.min(width, bi.getWidth());      
  26.   if(height <= 0) height = bi.getHeight();      
  27.   if(width <= 0) width = bi.getWidth();      
  28.   top = Math.min(Math.max(0, top), bi.getHeight()-height);      
  29.   left = Math.min(Math.max(0, left), bi.getWidth()-width);      
  30.         
  31.   BufferedImage bi_cropper = bi.getSubp_w_picpath(left, top, width, height);      
  32.   return ImageIO.write(bi_cropper, ext.equals("png")?"png":"jpeg", fileDest);      
  33.  }     
  34.     
  35.  public static void main(String[] args) {   
  36.   try {   
  37.    System.out.println(saveImage(new File("E:\\JavaWork\\pic\\WebRoot\\css\\flowers.jpg"),"E:\\JavaWork\\pic\\WebRoot\\css\\flowers1.jpg",106,87,289,217));   
  38.   } catch (IOException e) {   
  39.    // TODO Auto-generated catch block   
  40.    e.printStackTrace();   
  41.   }   
  42.  }   
  43. }   

这里的top、left、width、height都可以直接用Jcrop里获取到。 在Jcrop的“Basic Handler”这个demo里面,相应的X1、Y1、W、H这四个参数,用request可以得到这些值。

转载自:http://mofan.iteye.com/blog/767431