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

前端之家收集整理的这篇文章主要介绍了JCrop+ajaxUpload 图像切割上传的实例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

先给大家展示下效果图:

页面代码

里面用户的uuid是写死的test

<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>头像剪裁
Box">

后台代码

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 图像切割上传的实例代码。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

原文链接:https://www.f2er.com/ajax/47180.html

猜你在找的Ajax相关文章