欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery jcrop插件截图使用方法

程序员文章站 2023-11-15 12:45:53
在后台来进行图片切割。 头像截取的原理:在前台使用jcrop获取切面的x轴坐标、y轴坐标、切面高度、切面宽度,然后将这四个值传给后 台。在后台要进行放大处理:将切面放大n倍,n...

在后台来进行图片切割。
头像截取的原理:在前台使用jcrop获取切面的x轴坐标、y轴坐标、切面高度、切面宽度,然后将这四个值传给后
台。在后台要进行放大处理:将切面放大n倍,n=原图/前台展示的头像。即x = x*原图宽/前图宽,y = y*原图高/前
图高,w = w*原图宽/前图宽,h = h*原图高/前图高。
实例:
jsp:

. 代码如下:


<p id="cutimage" style="display: none;">
 <p class="bigimg" style="float: left;">
     <img id="srcimg" src="" width="400px" height="270px"/>
 </p>
 <p id="preview_box" class="previewimg">
     <img id="previewimg" src="" width="120px"/>
 </p>

 <p >
 <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>
</p>
    </p>

样式:大图、小图展示都需要固定高度、宽度,因为后台需要进行放大处理。即:<img width=""height=""/>
然后是使用jcrop了。在使用jcrop前我们需要下载jcrop:https://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( {
     ectratio : 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();
 }
    }
}


jQuery jcrop插件截图使用方法