栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC结合Jcrop实现图片裁剪

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC结合Jcrop实现图片裁剪

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

一、jsp页面:

 

二、jcrop组件引用情况:

" type="text/css"> 
   
   

三、jcrop使用方法

 

四、后台代码:

@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工具类:

 
  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
      });

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号