`
llyzq
  • 浏览: 577603 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Jquery Jcrop 插件java的使用方法 包括图片保存类

    博客分类:
  • J2EE
 
阅读更多

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

 

1。下载最新的Jcrop文件。 

http://deepliquid.com/content/Jcrop.html

 

里面有demo文件,可以直接看.但要实现剪切功能还需要类来支持。默认的是PHP的。

 

2。Utils类

 

Java代码  收藏代码
  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. }  

 这个类主要是用来获取文件的扩展名。

 

3。SaveImges类

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

 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics