使用jquery jcrop插件進行圖片的截取與保存

第一步:上傳大圖片到服務器,並且展示到頁面
第二部:調用jcrop組件js及css

[html] view plaincopy
  1. <script src="${ctx}/js/jquery-jcrop/jquery.Jcrop.js"></script>    
  2. <link rel="stylesheet" href="${ctx}/css/jquery-jcrop/jquery.Jcrop.css" type="text/css" />   

 

第三部:採用插件
進行圖片的選取,其中主要包括x,y,width,height

 


 

[javascript] view plaincopy
  1. $('#cropButton').click(function(){  
  2.             var x = $("#x").val();  
  3.             var y = $("#y").val();  
  4.             var w = $("#w").val();  
  5.             var h = $("#h").val();    
  6.             if(w == 0 || h == 0 ){  
  7.                 alert("您還沒有選擇圖片的剪切區域,不能進行剪切圖片!");  
  8.                 return;  
  9.             }     
  10.             //alert("你要剪切圖片的X座標: "+x + ",Y座標: " + y + ",剪切圖片的寬度: " + w + ",高度:" + h );  
  11.             if(confirm("確定按照當前大小剪切圖片嗎")){                 
  12.                 //document.form1.submit();  
  13.                 //$("#makeHeadImgDiv").toggle();  
  14.                 function loadHeadUrl(data){  
  15.                     //alert(data.headUrl);  
  16.                      alert(data.retMsg);  
  17.                      $("#img_headUrl").attr("src",data.headUrl);  
  18.                      $("#headUrl").val(data.headUrl);  
  19.                     //alert($("#headUrl").val());  
  20.                 }  
  21.                 var url="${ctx}/admin/pri/guest/guest_cutHeadImg.action";  
  22.                 var options ={   
  23.                         //beforeSubmit: validate,  
  24.                         url:       url,  
  25.                         success:   loadHeadUrl,   
  26.                         type:      'post',        
  27.                         dataType:  'json'  
  28.                 };  
  29.                 $('#cutHeadImgFrm').ajaxSubmit(options);  
  30.             }  
  31.      });  


然後調用後臺方法切割圖片。
設計的包:
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;
第四部:保存新文件路徑,展示圖片到頁面。

 


方法類:

 

[java] view plaincopy
  1. package com.wondertek.meeting.util;  
  2.   
  3. import java.io.*;  
  4. import java.awt.*;  
  5. import java.awt.image.*;  
  6. import java.awt.Graphics;  
  7. import java.awt.color.ColorSpace;  
  8. import javax.imageio.ImageIO;  
  9.   
  10. public class ImageCut {  
  11.       
  12.     /**  
  13.      * 圖像切割(改)     *  
  14.      * @param srcImageFile            源圖像地址 
  15.      * @param dirImageFile            新圖像地址 
  16.      * @param x                       目標切片起點x座標 
  17.      * @param y                      目標切片起點y座標 
  18.      * @param destWidth              目標切片寬度 
  19.      * @param destHeight             目標切片高度 
  20.      */  
  21.     public static void abscut(String srcImageFile,String dirImageFile, int x, int y, int destWidth,  
  22.             int destHeight) {  
  23.         try {  
  24.             Image img;  
  25.             ImageFilter cropFilter;  
  26.             // 讀取源圖像  
  27.             BufferedImage bi = ImageIO.read(new File(srcImageFile));  
  28.             int srcWidth = bi.getWidth(); // 源圖寬度  
  29.             int srcHeight = bi.getHeight(); // 源圖高度            
  30.             if (srcWidth >= destWidth && srcHeight >= destHeight) {  
  31.                 Image image = bi.getScaledInstance(srcWidth, srcHeight,  
  32.                         Image.SCALE_DEFAULT);  
  33.                 // 改進的想法:是否可用多線程加快切割速度  
  34.                 // 四個參數分別爲圖像起點座標和寬高  
  35.                 // 即: CropImageFilter(int x,int y,int width,int height)  
  36.                 cropFilter = new CropImageFilter(x, y, destWidth, destHeight);  
  37.                 img = Toolkit.getDefaultToolkit().createImage(  
  38.                         new FilteredImageSource(image.getSource(), cropFilter));  
  39.                 BufferedImage tag = new BufferedImage(destWidth, destHeight,  
  40.                         BufferedImage.TYPE_INT_RGB);  
  41.                 Graphics g = tag.getGraphics();  
  42.                 g.drawImage(img, 00null); // 繪製縮小後的圖  
  43.                 g.dispose();  
  44.                 // 輸出爲文件  
  45.                 ImageIO.write(tag, "JPEG"new File(dirImageFile));  
  46.             }  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.   
  52.       
  53.     /** 
  54.      * 縮放圖像 
  55.      *  
  56.      * @param srcImageFile       源圖像文件地址 
  57.      * @param result             縮放後的圖像地址 
  58.      * @param scale              縮放比例 
  59.      * @param flag               縮放選擇:true 放大; false 縮小; 
  60.      */  
  61.     public static void scale(String srcImageFile, String result, int scale,  
  62.             boolean flag) {  
  63.         try {  
  64.             BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件  
  65.             int width = src.getWidth(); // 得到源圖寬  
  66.             int height = src.getHeight(); // 得到源圖長  
  67.             if (flag) {  
  68.                 // 放大  
  69.                 width = width * scale;  
  70.                 height = height * scale;  
  71.             } else {  
  72.                 // 縮小  
  73.                 width = width / scale;  
  74.                 height = height / scale;  
  75.             }  
  76.             Image image = src.getScaledInstance(width, height,Image.SCALE_DEFAULT);  
  77.             BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
  78.             Graphics g = tag.getGraphics();  
  79.             g.drawImage(image, 00null); // 繪製縮小後的圖  
  80.             g.dispose();  
  81.             ImageIO.write(tag, "JPEG"new File(result));// 輸出到文件流  
  82.         } catch (IOException e) {  
  83.             e.printStackTrace();  
  84.         }  
  85.     }  
  86.       
  87.     /** 
  88.      * 重新生成按指定寬度和高度的圖像 
  89.      * @param srcImageFile       源圖像文件地址 
  90.      * @param result             新的圖像地址 
  91.      * @param _width             設置新的圖像寬度 
  92.      * @param _height            設置新的圖像高度 
  93.      */  
  94.     public static void scale(String srcImageFile, String result, int _width,int _height) {        
  95.         scale(srcImageFile,result,_width,_height,0,0);  
  96.     }  
  97.       
  98.     public static void scale(String srcImageFile, String result, int _width,int _height,int x,int y) {  
  99.         try {  
  100.               
  101.             BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件  
  102.               
  103.             int width = src.getWidth(); // 得到源圖寬  
  104.             int height = src.getHeight(); // 得到源圖長  
  105.               
  106.             if (width > _width) {  
  107.                  width = _width;  
  108.             }  
  109.             if (height > _height) {  
  110.                 height = _height;  
  111.             }             
  112.             Image image = src.getScaledInstance(width, height,Image.SCALE_DEFAULT);  
  113.             BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
  114.             Graphics g = tag.getGraphics();  
  115.             g.drawImage(image, x, y, null); // 繪製縮小後的圖  
  116.             g.dispose();              
  117.             ImageIO.write(tag, "JPEG"new File(result));// 輸出到文件流  
  118.         } catch (IOException e) {  
  119.             e.printStackTrace();  
  120.         }  
  121.     }  
  122.       
  123.     /** 
  124.      * 圖像類型轉換 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) 
  125.      */  
  126.     public static void convert(String source, String result) {  
  127.         try {  
  128.             File f = new File(source);  
  129.             f.canRead();  
  130.             f.canWrite();  
  131.             BufferedImage src = ImageIO.read(f);  
  132.             ImageIO.write(src, "JPG"new File(result));  
  133.         } catch (Exception e) {  
  134.             e.printStackTrace();  
  135.         }  
  136.     }  
  137.   
  138.     /** 
  139.      * 彩色轉爲黑白 
  140.      *  
  141.      * @param source 
  142.      * @param result 
  143.      */  
  144.     public static void gray(String source, String result) {  
  145.         try {  
  146.             BufferedImage src = ImageIO.read(new File(source));  
  147.             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
  148.             ColorConvertOp op = new ColorConvertOp(cs, null);  
  149.             src = op.filter(src, null);  
  150.             ImageIO.write(src, "JPEG"new File(result));  
  151.         } catch (IOException e) {  
  152.             e.printStackTrace();  
  153.         }  
  154.     }  
  155.   
  156.     /** 
  157.      * @param args 
  158.      */  
  159.     public static void main(String[] args) {  
  160.         //暈。。。搞成多個了...  
  161.         //cut("c:/images/ipomoea.jpg", "c:/images/t/ipomoea.jpg", 200, 150);  
  162.         //ok  
  163.         //gray("c:/images/ipomoea.jpg", "c:/images/t/ipomoea.jpg");  
  164.         //convert("c:/images/ipomoea.jpg", "c:/images/t/ipomoea.gif");  
  165.         //scale("c:/images/5105049910001020110718648723.jpg", "c:/images/t/5105049910001020110718648725.jpg",154,166,157,208);  
  166.         //scale("c:/images/rose1.jpg", "c:/images/t/rose1.jpg",154,166,157,208);  
  167.         scale("c:/images/rose1.jpg""c:/images/t/rose2.jpg",154,166,10,10);  
  168.           
  169.     }  
  170. }  


 

發佈了12 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章