SpringMVC结合Jcrop实现图片裁剪

 更新时间:2016年12月30日 16:31:53   作者:繁华穿越现实  
这篇文章主要介绍了SpringMVC结合Jcrop实现图片裁剪的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了SpringMVC结合Jcrop实现图片裁剪的具体代码,供大家参考,具体内容如下

一、jsp页面:

<form name="form" action="<%=request.getContextPath()%>/UploadDemo/uploadHeadImage" class="form-horizontal" 
   method="post" enctype="multipart/form-data"> 
  <div class="modal-body text-center"> 
    <div class="zxx_main_con"> 
      <div class="zxx_test_list"> 
        <input class="photo-file" type="file" name="imgFile" id="fcupload" onchange="readURL(this);"/> 
        <img alt="" src="" id="cutimg"/> 
        <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"/> 
      </div> 
    </div> 
  </div> 
   
  <div class="modal-footer"> 
    <button id="submit" onclick="">上传</button> 
  </div> 
</form> 

二、jcrop组件引用情况:

<link rel="stylesheet" href="<c:url value="/resources/uploadPlugin/css/jquery.Jcrop.css"/>" type="text/css"></link> 
  <script type="text/javascript" src="<c:url value="/resources/uploadPlugin/scripts/jquery-1.8.3.js"/>"></script> 
  <script type="text/javascript" src="<c:url value="/resources/uploadPlugin/scripts/jquery.Jcrop.js"/>"></script> 

三、jcrop使用方法

<script type="text/javascript"> 
   //定义一个全局api,这样操作起来比较灵活 
    var api = null; 
    function readURL(input) { 
      if (input.files && input.files[0]) { 
        var reader = new FileReader(); 
        reader.readAsDataURL(input.files[0]); 
        reader.onload = function (e) { 
          $('#cutimg').removeAttr('src'); 
          $('#cutimg').attr('src', e.target.result); 
          api = $.Jcrop('#cutimg', { 
            setSelect: [ 20, 20, 200, 200 ], 
            aspectRatio: 1, 
            onSelect: updateCoords 
          }); 
        }; 
        if (api != undefined) { 
          api.destroy(); 
        } 
      } 
      function updateCoords(obj) { 
        $("#x").val(obj.x); 
        $("#y").val(obj.y); 
        $("#w").val(obj.w); 
        $("#h").val(obj.h); 
      }; 
    } 
  </script> 

四、后台代码:

@RequestMapping(value = "/uploadHeadImage") 
  public String uploadHeadImage( 
      HttpServletRequest request, 
      @RequestParam(value = "x") String x, 
      @RequestParam(value = "y") String y, 
      @RequestParam(value = "h") String h, 
      @RequestParam(value = "w") String w, 
      @RequestParam(value = "imgFile") MultipartFile imageFile 
  ) throws Exception{ 
    System.out.println("==========Start============="); 
    String realPath = request.getSession().getServletContext().getRealPath("/"); 
    String resourcePath = "resources/uploadImages/"; 
    if(imageFile!=null){ 
      if(FileUploadUtil.allowUpload(imageFile.getContentType())){ 
        String fileName = FileUploadUtil.rename(imageFile.getOriginalFilename()); 
        int end = fileName.lastIndexOf("."); 
        String saveName = fileName.substring(0,end); 
        File dir = new File(realPath + resourcePath); 
        if(!dir.exists()){ 
          dir.mkdirs(); 
        } 
        File file = new File(dir,saveName+"_src.jpg"); 
        imageFile.transferTo(file); 
        String srcImagePath = realPath + resourcePath + saveName; 
        int imageX = Integer.parseInt(x); 
        int imageY = Integer.parseInt(y); 
        int imageH = Integer.parseInt(h); 
        int imageW = Integer.parseInt(w); 
        //这里开始截取操作 
        System.out.println("==========imageCutStart============="); 
        ImageCut.imgCut(srcImagePath,imageX,imageY,imageW,imageH); 
        System.out.println("==========imageCutEnd============="); 
      } 
    } 
    return "user/uploadImg/test"; 
  } 

五、ImageCut.java工具类:

/** 
   * 截取图片 
   * @param srcImageFile 原图片地址 
   * @param x  截取时的x坐标 
   * @param y  截取时的y坐标 
   * @param desWidth  截取的宽度 
   * @param desHeight  截取的高度 
   */ 
  public static void imgCut(String srcImageFile, int x, int y, int desWidth, 
               int desHeight) { 
    try { 
      Image img; 
      ImageFilter cropFilter; 
      BufferedImage bi = ImageIO.read(new File(srcImageFile+"_src.jpg")); 
      int srcWidth = bi.getWidth(); 
      int srcHeight = bi.getHeight(); 
      if (srcWidth >= desWidth && srcHeight >= desHeight) { 
        Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); 
        cropFilter = new CropImageFilter(x, y, desWidth, desHeight); 
        img = Toolkit.getDefaultToolkit().createImage( 
            new FilteredImageSource(image.getSource(), cropFilter)); 
        BufferedImage tag = new BufferedImage(desWidth, desHeight, 
            BufferedImage.TYPE_INT_RGB); 
        Graphics g = tag.getGraphics(); 
        g.drawImage(img, 0, 0, null); 
        g.dispose(); 
        //输出文件 
        ImageIO.write(tag, "JPEG", new File(srcImageFile+"_cut.jpg")); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 

六、jcrop的两种使用方式:

1、

jQuery('#cropbox').Jcrop({
         onChange: showCoords,
         onSelect: showCoords
      });

2、

var api = $.Jcrop('#cropbox',{
         onChange: showPreview,
         onSelect: showPreview,
         aspectRatio: 1
      });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • MyBatis-Plus联表查询及分页代码举例

    MyBatis-Plus联表查询及分页代码举例

    本文介绍了mybatis-plus-join工具的使用,该工具可以简化mybatis-plus的联表查询,使得开发者可以以类似QueryWrapper的方式进行联表查询,无需手动编写xml文件,感兴趣的朋友跟随小编一起看看吧
    2025-03-03
  • Java基础篇_有关接口和抽象类的几道练习题(分享)

    Java基础篇_有关接口和抽象类的几道练习题(分享)

    下面小编就为大家带来一篇Java基础篇_有关接口和抽象类的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • SpringBoot2底层注解@Configuration配置类详解

    SpringBoot2底层注解@Configuration配置类详解

    这篇文章主要为大家介绍了SpringBoot2底层注解@Configuration配置类详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • websocket在springboot+vue中的使用教程

    websocket在springboot+vue中的使用教程

    这篇文章主要介绍了websocket在springboot+vue中的使用教程,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • Java对int[]数组做新增删除去重操作代码

    Java对int[]数组做新增删除去重操作代码

    这篇文章主要介绍了Java里面对int[]数组做新增删除去重实现,这里记录下使用int[]数组对数组进行新增删除去重等操作,用来更加了解java里面的集合类思想,需要的朋友可以参考下
    2023-10-10
  • java利用Ant脚本生成war包全过程

    java利用Ant脚本生成war包全过程

    这篇文章主要为大家详细介绍了java利用Ant脚本生成war包全过程,感兴趣的朋友可以参考一下
    2016-03-03
  • Java方法参数装配顺序详解

    Java方法参数装配顺序详解

    这篇文章主要介绍了Java方法参数装配顺序详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot之controller参数校验详解

    SpringBoot之controller参数校验详解

    介绍了Java中使用@Validated和@Valid进行参数校验的方法,包括不同标签的使用场景、基本属性和一些常用的注解类型,同时,还讨论了如何在控制器中使用这些校验标签,以及如何处理校验结果和自定义错误消息,最后,还介绍了如何实现分组校验和嵌套校验,并提供了一些示例代码
    2024-11-11
  • Hibernate中Session增删改查操作代码详解

    Hibernate中Session增删改查操作代码详解

    这篇文章主要介绍了Hibernate中Session增删改查操作代码详解,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • IntelliJ IDEA 2022安装注册永久激活

    IntelliJ IDEA 2022安装注册永久激活

    java开发工具IntelliJ IDEA深受用户喜爱,很多朋友对这个idea开发工具比较忠心,一旦有新版本发出,很多小伙伴就迫不及待的想更新,今天小编给大家带来了idea2022.1最新永久激活码,亲测有效,喜欢的朋友快来下载体验吧
    2022-08-08

最新评论