加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JCrop+ajaxUpload 图像切割上传的实例代码

发布时间:2020-12-15 23:25:26 所属栏目:百科 来源:网络整理
导读:先给大家展示下效果图: 页面代码 里面用户的uuid是写死的test 用户头像剪裁 头像剪裁 + 添加图片 提交 后台代码 uploadPhoto(@RequestParam("upimage") MultipartFile imageFile,HttpServletRequest request,HttpServletResponse response) throws Exceptio

先给大家展示下效果图:

页面代码

里面用户的uuid是写死的test

用户头像剪裁
头像剪裁
+添加图片
提交

后台代码

uploadPhoto(@RequestParam("upimage") MultipartFile imageFile,HttpServletRequest request,HttpServletResponse response) throws Exception { Map result = new HashMap(); boolean tag =false; String unid = request.getParameter("unid"); String x = request.getParameter("x"); String y = request.getParameter("y"); String h = request.getParameter("h"); String w = request.getParameter("w"); // 页面实际图片宽高 String pheight = request.getParameter("ph"); String pweight = request.getParameter("pw"); // 切图参数 int imageX = Integer.parseInt(x); int imageY = Integer.parseInt(y); int imageH = Integer.parseInt(h); int imageW = Integer.parseInt(w); int srcH = Integer.parseInt(pheight); int srcW = Integer.parseInt(pweight); String realPath = request.getSession().getServletContext().getRealPath("/"); String resourcePath = "resources/uploadImages/"; try { if (imageFile != null) { if (FileUploadUtil.allowUpload(imageFile.getContentType())) { // 这里开始截取操作 byte[] b = ImageCut.imgCut(imageFile.getInputStream(),imageX,imageY,imageW,imageH,srcW,srcH); if (b != null) { // 存入数据库 User user = userService.selectByPrimaryKey(unid); user.setPhoto(b); tag = (userService.updateByPrimaryKeySelective(user)==1)?tag=true:tag; result.put("result",tag); } } } } catch (Exception e) { e.printStackTrace(); } result.put("result",tag); return result; }

图像切割工具类

= 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,null); g.dispose(); // 输出文件 ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(tag,"JPEG",out); return out.toByteArray(); } } catch (Exception e) { e.printStackTrace(); } return null; } }

以上所述是小编给大家介绍的JCrop+ajaxUpload 图像切割上传的实例代码。编程之家 52php.cn 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读