使用jcrop進行頭像剪切
項目需要做一個頭像截取的功能,類似于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
使用方法:
//裁剪圖像 function cutImage(){ $("#srcImg").Jcrop( { aspectRatio : 1, onChange : showCoords, onSelect : showCoords, minSize :[200,200] }); //簡單的事件處理程序,響應自onChange,onSelect事件,按照上面的Jcrop調用 function showCoords(obj) { $("#x").val(obj.x); $("#y").val(obj.y); $("#w").val(obj.w); $("#h").val(obj.h); if (parseInt(obj.w) > 0) { //計算預覽區域圖片縮放的比例,通過計算顯示區域的寬度(與高度)與剪裁的寬度(與高度)之比得到 var rx = $("#preview_box").width() / obj.w; var ry = $("#preview_box").height() / obj.h; //通過比例值控制圖片的樣式與顯示 $("#previewImg").css( { width : Math.round(rx * $("#srcImg").width()) + "px", //預覽圖片寬度為計算比例值與原圖片寬度的乘積 height : Math.round(rx * $("#srcImg").height()) + "px", //預覽圖片高度為計算比例值與原圖片高度的乘積 marginLeft : "-" + Math.round(rx * obj.x) + "px", marginTop : "-" + Math.round(ry * obj.y) + "px" }); } } }
在使用jcrop前一定要先將$(“”).jcrop();進行預初始化,否則沒有效果。
還有一種調用的方法,
var api = $.Jcrop('#cropbox',{ onChange: showPreview, onSelect: showPreview, aspectRatio: 1 });
這種方法是將Jcrop生成的對象賦給一個全局變量,這樣操作就會比較方便。
通過上面的js,就將X軸坐標、Y軸坐標、高度H、寬度W這個四個值傳遞給后臺了,后臺就只需要根據這四個值
進行放大處理,然后切割即可。
Action
/** * 裁剪頭像 */ public String cutImage(){ /* * 獲取參數 * x,y,w,h,bigImage */ HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); int x = Integer.valueOf(request.getParameter("x")); int y = Integer.valueOf(request.getParameter("y")); int w = Integer.valueOf(request.getParameter("w")); int h = Integer.valueOf(request.getParameter("h")); String bigImage = request.getParameter("bigImage"); //獲取文件真實路徑 //獲取文件名 String[] imageNameS = bigImage.split("/"); String imageName = imageNameS[imageNameS.length-1]; //文件正式路徑 String imagePath = getSavePath()+"\\"+imageName; //切割圖片 ImageCut imageCut = new ImageCut(); imageCut.cutImage(imagePath, x, y, w, h); //頭像裁剪完成后,將圖片路徑保存到用戶 UserBean userBean = (UserBean) request.getSession().getAttribute("userBean"); userBean.setUserPhoto(bigImage); //保存頭像 UserCenterService centerService = new UserCenterService(); centerService.updatePhoto(userBean); //將修改后的用戶保存到session中 request.getSession().setAttribute("userBean", userBean); return "updatePhoto"; } }
裁剪圖片工具類:ImageCut.java
public class ImageCut { /** * 圖片切割 * @param imagePath 原圖地址 * @param x 目標切片坐標 X軸起點 * @param y 目標切片坐標 Y軸起點 * @param w 目標切片 寬度 * @param h 目標切片 高度 */ public void cutImage(String imagePath, int x ,int y ,int w,int h){ try { Image img; ImageFilter cropFilter; // 讀取源圖像 BufferedImage bi = ImageIO.read(new File(imagePath)); int srcWidth = bi.getWidth(); // 源圖寬度 int srcHeight = bi.getHeight(); // 源圖高度 //若原圖大小大于切片大小,則進行切割 if (srcWidth >= w && srcHeight >= h) { Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); int x1 = x*srcWidth/400; int y1 = y*srcHeight/270; int w1 = w*srcWidth/400; int h1 = h*srcHeight/270; cropFilter = new CropImageFilter(x1, y1, w1, h1); img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(w1, h1,BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, null); // 繪制縮小后的圖 g.dispose(); // 輸出為文件 ImageIO.write(tag, "JPEG", new File(imagePath)); } } catch (IOException e) { e.printStackTrace(); } } }
效果圖:
點擊確認后,就會在指定路徑下生成相應的圖片:
OVER!!