扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
使用cropper.js怎么裁剪头像?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
东湖ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="../common_front.jsp" %>Insert title here
snippet_file_0.txt
下面是我后台处理方法,大家可以借鉴一下。后台是ssm框架,主要是保存图片和图片转码
//用户上传头像 /** * * @param image 选择的图片 * @param model * @param userId 用户id * @param userType 用户类型 * @param request * @param originalImage 上一张临时图片 * @return */ @RequestMapping(value="/imageUpload",method=RequestMethod.POST) public String iconImageUpload(@RequestParam(value="file",required=false)MultipartFile image,Model model,@CookieValue("userId")String userId,HttpServletRequest request,String originalImage){ String basePath="image/"; //web.xml里面配置的用户图片存储路径 String userImagePath=request.getSession().getServletContext().getInitParameter("userImageSavePath"); //图片相对路径 String imageRelativePath=FileUtils.fileUpload(image, request,basePath+userImagePath); System.out.println("图片保存路径------"+imageRelativePath); System.out.println("上一张临时图片------"+originalImage); //删除上一张临时图片 if(originalImage!=null){ String basePathTemp=request.getSession().getServletContext().getRealPath("/"); FileUtils.deleteFile(basePathTemp+originalImage); } model.addAttribute("imageRelativePath", imageRelativePath); model.addAttribute("userId", userId); return "/crop_image"; } //将裁剪好的头像由base64还原成图片 @ResponseBody @RequestMapping(value="/saveUserIcon",method=RequestMethod.POST) public Msg saveUserIcon(String icon,@CookieValue("userType")String userType,@CookieValue("userId")String userId,String originalImage,HttpServletRequest request){ System.out.println("icon-----"+icon); //先生成图片地址 String realpath=request.getSession().getServletContext().getRealPath("/"); String basePath="image/"; String userImagePath=request.getSession().getServletContext().getInitParameter("userImageSavePath"); Calendar now=Calendar.getInstance(); String relativePath=basePath+userImagePath+"/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH)+1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+FileUtils.getUUID()+".png"; String imagePath=realpath+relativePath; //将base64 转换成图片 FileUtils.base64ToImage(icon, imagePath); //删除原图 if(originalImage!=null){ FileUtils.deleteFile(realpath+originalImage); } return Msg.success(); } //下面是解码的方法 public static boolean base64ToImage(String base64, String path) {// 对字节数组字符串进行Base64解码并生成图片 if (base64 == null){ // 图像数据为空 return false; } System.out.println(base64); // base64 的格式为 “data:image/png;base64,****”,逗号之前都是一些说明性的文字,我们只需要逗号之后的就行了 base64=base64.split(",")[1]; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(base64); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成图片 OutputStream out = new FileOutputStream(path); out.write(bytes); out.flush(); out.close(); return true; } catch (Exception e) { return false; } }
关于使用cropper.js怎么裁剪头像问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流