jQuery jcrop外掛程式截圖使用方法

來源:互聯網
上載者:User

在後台來進行圖片切割。
頭像截取的原理:在前台使用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();
 }
    }
}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.