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

先给大家展示下效果图:

页面代码

里面用户的uuid是写死的test

rush:js;"> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>头像剪裁
Box">

后台代码

rush:js;"> @ResponseBody @RequestMapping("uploadphoto") public Map 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 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...