项目中运用cropper插件

在项目中简单的用了cropper插件,记录一下。

github地址: https://github.com/fengyuanchen/cropper
中文API文档:
网上搜到的这一篇,还比较详细。

项目中需求:
在dialog中实现图片的裁剪(裁剪的比例由外界传入,在裁剪过程中比例固定)
将demo中的index.html代码复制到dialog页面,根据自己的需求进行布局。
导入css:
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/cropper.min.css">
<link rel="stylesheet" href="css/main.css">
导入js:
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/cropper.min.js"></script>
<script src="js/main.js"></script>
初始化cropper的js代码:
var width //页面传过来的宽
var height  //页面传过来的高
//cropper初始化
var $dataX = $('#dataX'),
var $dataY = $('#dataY'),
var $dataHeight = $('#dataHeight'),
var $dataWidth = $('#dataWidth'),
var $dataRotate = $('#dataRotate'),
var options ={
  aspectRatio:width/height,
  preview:'.img-preview',
  crop:function(e){
$dataX.val(Math.round(data.x));
    $dataY.val(Math.round(data.y));
    $dataHeight.val(Math.round(data.height));
    $dataWidth.val(Math.round(data.width));
    $dataRotate.val(Math.round(data.rotate));
}
}
$('#image').cropper(options);

用js或者${}设置外部传入src的值(需要裁减图片的路径),在dialog进入时显示。
<div class="img-container">
     <img id="image" src="img/picture.jpg" alt="Picture">
</div>

当在页面点击getCroppedCanvas按钮时,会弹出getCroppedCanvasModal弹窗,来进行下载操作。
而我们现行需要进行上传到服务器,所以添加一个上传按钮,将下载按钮注掉。
<div class="modal fade docs-cropped" id="getCroppedCanvasModal" aria-hidden="true" aria-labelledby="getCroppedCanvasTitle" role="dialog" tabindex="-1">
     <div class="modal-dialog">
          <div class="modal-content">
             <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="getCroppedCanvasTitle">Cropped</h4>
              </div>
              <div class="modal-body"></div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<a class="btn btn-primary" id="uploadPic">确认上传</a>
                <a class="btn btn-primary hidden" id="download" href="javascript:void(0);" download="cropped.jpg">Download</a>
             </div>
          </div>
      </div>
</div><!-- /.modal -->

在js中上传图片base64:
$("#uploadPic").on('click',function(){
//设置canvas的id
    $("canvas").attr("id","myCanvas");
var Pic=document.getElenmentById("myCanvas").toDataURL("image/png");
Pic=Pic.replace(/^data:image\/(png|jpg);base64,/,"");
//下面可以Ajax将Pic传入后台进行base64保存图片的操作。。。

})

Controller接受后在后台将base64数据转换为图片保存
//base64字符串转化成图片  
    public static boolean GenerateImage(String imgStr,String imgFilePath)  
    {   //对字节数组字符串进行Base64解码并生成图片  
        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;  
                }  
            }  
            //生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);      
            out.write(b);  
            out.flush();  
            out.close();  
            return true;  
        }   
        catch (Exception e)   
        {  
            return false;  
        }  
    }  






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值