最近项目需要头像裁剪的功能,在网上找了一下,发现了github上的cropper项目还不错,借鉴了一下。。用起来挺简单的,下面是我做的一个小例子:
开始先放个成品图:
下面给出前后端的代码
前端页面是一个单独的jsp页面,用来做弹出层来裁剪图片比较好。
关于jsp页面引用的两个关于cropper的 文件,我就不提供了。大家需要的可以去官方的github上去下载。
地址:https://github.com/fengyuanchen/cropper
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../common_front.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${path }/front/plugins/cropper/cropper.css" rel="external nofollow" >
<script src="/UploadFiles/2021-04-02/cropper.js">
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裁剪头像的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
标签:
cropper.js,裁剪,头像
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“使用cropper.js裁剪头像的实例代码”评论...
更新日志
2025年10月24日
2025年10月24日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
