使用Cropper裁剪图片并上传SSM【绝对能用】

8 篇文章 0 订阅

第一步:引入css和js

第二步:创建页面

<div style="width: 600px;height: 600px;"><!--必须通过父容器限定图像大小 -->
     <img id="imgTeset" src="">
</div>
<input type="file" id="fileHead" onchange="show(this)" />

第三步:编写加载本地图片部分

function show(a){
    var $file = $(a);
    var fileObj = $file[0];
    var windowURL = window.URL || window.webkitURL;
    var dataURL = null;
    if (!fileObj || !fileObj.files || !fileObj.files[0]){//没有选择图片
        return;
    }
    dataURL = windowURL.createObjectURL(fileObj.files[0]);
    $("#imgTeset").attr('src', dataURL);
    $('#imgTeset').cropper({
        aspectRatio: 1 / 1,
        viewMode: 1
    });
    $("#imgTeset").cropper('replace', dataURL);
}

第四步:增加调整按钮

<button type="button" onclick="$('#imgTeset').cropper('setDragMode','move')">移动</button>
<button type="button" onclick="horizontal()">水平翻转</button>
<button type="button" onclick="vertical()">垂直翻转</button>
<button type="button" onclick="cai()">裁剪</button>
var currentHorizontal=1;
var currentVertical=1;
//水平翻转
function horizontal(){
    currentHorizontal*=-1;
    $('#imgTeset').cropper('scaleX',currentHorizontal);
}
//垂直翻转
function vertical(){
    currentVertical*=-1;
    $('#imgTeset').cropper('scaleY',currentVertical);
}

第五步:裁剪并上传

function cai(){
    var size={width:128,height:128};//要裁剪成的图像大小
    var cas = $('#imgTeset').cropper('getCroppedCanvas',size);
    if(cas == null){
        alert("请选择图片");
        return false;
    }else{
        var base64url = cas.toDataURL('image/jpeg');//转换成图片格式
        $.ajax({
            url : "${pageContext.request.contextPath}/stu/cropper1",//上传地址
            dataType:'json',
            type: "post",
            data: {
                imgBase64 : base64url
            },
            success: function (data) {
                alert(data);
            }
        });
    }
}

第六步:服务器接收

@ResponseBody
@RequestMapping("/cropper1")
public String cropper1(String imgBase64,HttpServletRequest request){
    imgBase64 = imgBase64.split(",")[1];
    GenerateImage(imgBase64,request.getSession()
                  .getServletContext()
                  .getRealPath("WEB-INF/statics/images/temp")+"/cai.jpg");
    return "images/temp/cai.jpg";
}

public static boolean GenerateImage(String imgStr,String imgFilePath){
    if (imgStr == null) //图像数据为空
        return false;
    BASE64Decoder decoder = new BASE64Decoder();
    try{
        //Base64解码
        byte[] b = decoder.decodeBuffer(imgStr);
        for(int i=0;i<b.length;++i){
            if(b[i] < 0){//调整异常数据
                b[i] += 256;
            }
        }
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return true;
    }
    catch (Exception e){
        return false;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值