項目需要做一個頭像截取的功能,類似於QQ頭像截取功能。在網上搜了下,最后使用jQuery插件jcrop來截取,
在后台來進行圖片切割。
頭像截取的原理:在前台使用jcrop獲取切面的x軸坐標、y軸坐標、切面高度、切面寬度,然后將這四個值傳給后
台。在后台要進行放大處理:將切面放大N倍,N=原圖/前台展示的頭像。即X = X*原圖寬/前圖寬,Y = Y*原圖高/前
圖高,W = W*原圖寬/前圖寬,H = H*原圖高/前圖高。
實例:
JSP:
<div id="cutImage" style="display: none;"> <div class="bigImg" style="float: left;"> <img id="srcImg" src="" width="400px" height="270px"/> </div> <div id="preview_box" class="previewImg"> <img id="previewImg" src="" width="120px"/> </div> <div > <form action="" method="post" id="crop_form"> <input type="hidden" id="bigImage" name="bigImage"/> <input type="hidden" id="x" name="x" /> <input type="hidden" id="y" name="y" /> <input type="hidden" id="w" name="w" /> <input type="hidden" id="h" name="h" /> <P><input type="button" value="確認" id="crop_submit"/></P> </form> </div> </div>
樣式:大圖、小圖展示都需要固定高度、寬度,因為后台需要進行放大處理。即:<img width=""height=""/>
然后是使用jcrop了。在使用jcrop前我們需要下載jcrop:http://deepliquid.com/content/Jcrop.html。
將下載的壓縮包解壓后可以看到三個文件夾及一個index.html文件,/ css下放置的是Jcorp的樣式文件,/demo下放置的是幾個簡單的例子(index.html中引用的鏈接就是放置在這個文件夾下),/js下放置的是Jcorp中最重要的腳本文件。我們只需要使用三個文件即可:jquery.Jcrop.css、jquery.Jcrop.js、JQuery.js
使用方法:
1 //裁剪圖像 2 function cutImage(){ 3 $("#srcImg").Jcrop( { 4 aspectRatio : 1, 5 onChange : showCoords, 6 onSelect : showCoords, 7 minSize :[200,200] 8 }); 9 //簡單的事件處理程序,響應自onChange,onSelect事件,按照上面的Jcrop調用 10 function showCoords(obj) { 11 $("#x").val(obj.x); 12 $("#y").val(obj.y); 13 $("#w").val(obj.w); 14 $("#h").val(obj.h); 15 if (parseInt(obj.w) > 0) { 16 //計算預覽區域圖片縮放的比例,通過計算顯示區域的寬度(與高度)與剪裁的寬度(與高度)之比得到 17 var rx = $("#preview_box").width() / obj.w; 18 var ry = $("#preview_box").height() / obj.h; 19 //通過比例值控制圖片的樣式與顯示 20 $("#previewImg").css( { 21 width : Math.round(rx * $("#srcImg").width()) + "px", //預覽圖片寬度為計算比例值與原圖片寬度的乘積 22 height : Math.round(rx * $("#srcImg").height()) + "px", //預覽圖片高度為計算比例值與原圖片高度的乘積 23 marginLeft : "-" + Math.round(rx * obj.x) + "px", 24 marginTop : "-" + Math.round(ry * obj.y) + "px" 25 }); 26 } 27 } 28 }
在使用jcrop前一定要先將$(“”).jcrop();進行預初始化,否則沒有效果。
還有一種調用的方法,
1 var api = $.Jcrop('#cropbox',{ 2 onChange: showPreview, 3 onSelect: showPreview, 4 aspectRatio: 1 5 });
這種方法是將Jcrop生成的對象賦給一個全局變量,這樣操作就會比較方便。
通過上面的js,就將X軸坐標、Y軸坐標、高度H、寬度W這個四個值傳遞給后台了,后台就只需要根據這四個值
進行放大處理,然后切割即可。
Action
1 /** 2 * 裁剪頭像 3 */ 4 public String cutImage(){ 5 /* 6 * 獲取參數 7 * x,y,w,h,bigImage 8 */ 9 HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); 10 int x = Integer.valueOf(request.getParameter("x")); 11 int y = Integer.valueOf(request.getParameter("y")); 12 int w = Integer.valueOf(request.getParameter("w")); 13 int h = Integer.valueOf(request.getParameter("h")); 14 String bigImage = request.getParameter("bigImage"); 15 16 //獲取文件真實路徑 17 //獲取文件名 18 String[] imageNameS = bigImage.split("/"); 19 String imageName = imageNameS[imageNameS.length-1]; 20 //文件正式路徑 21 String imagePath = getSavePath()+"\\"+imageName; 22 23 //切割圖片 24 ImageCut imageCut = new ImageCut(); 25 imageCut.cutImage(imagePath, x, y, w, h); 26 27 //頭像裁剪完成后,將圖片路徑保存到用戶 28 UserBean userBean = (UserBean) request.getSession().getAttribute("userBean"); 29 userBean.setUserPhoto(bigImage); 30 //保存頭像 31 UserCenterService centerService = new UserCenterService(); 32 centerService.updatePhoto(userBean); 33 //將修改后的用戶保存到session中 34 request.getSession().setAttribute("userBean", userBean); 35 36 return "updatePhoto"; 37 } 38 }
裁剪圖片工具類:ImageCut.java
1 public class ImageCut { 2 3 /** 4 * 圖片切割 5 * @param imagePath 原圖地址 6 * @param x 目標切片坐標 X軸起點 7 * @param y 目標切片坐標 Y軸起點 8 * @param w 目標切片 寬度 9 * @param h 目標切片 高度 10 */ 11 public void cutImage(String imagePath, int x ,int y ,int w,int h){ 12 try { 13 Image img; 14 ImageFilter cropFilter; 15 // 讀取源圖像 16 BufferedImage bi = ImageIO.read(new File(imagePath)); 17 int srcWidth = bi.getWidth(); // 源圖寬度 18 int srcHeight = bi.getHeight(); // 源圖高度 19 20 //若原圖大小大於切片大小,則進行切割 21 if (srcWidth >= w && srcHeight >= h) { 22 Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); 23 24 int x1 = x*srcWidth/400; 25 int y1 = y*srcHeight/270; 26 int w1 = w*srcWidth/400; 27 int h1 = h*srcHeight/270; 28 29 cropFilter = new CropImageFilter(x1, y1, w1, h1); 30 img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); 31 BufferedImage tag = new BufferedImage(w1, h1,BufferedImage.TYPE_INT_RGB); 32 Graphics g = tag.getGraphics(); 33 g.drawImage(img, 0, 0, null); // 繪制縮小后的圖 34 g.dispose(); 35 // 輸出為文件 36 ImageIO.write(tag, "JPEG", new File(imagePath)); 37 } 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 }
效果圖:
點擊確認后,就會在指定路徑下生成相應的圖片:
OVER!!