
1、html页面部分代码:(实现选着图片时,jcrop能够刷新图片)
<script type="text/javascript">
$(function(){
$("#upload_org_code")
.uploadifive(
{
'queueID' : 'some_file_queue',//绑定到隐藏的div,使进度条消失。
'height' : 27,
'width' : 80,
'fileType' :'image',
'buttonText' : '选择图片',
//'swf' : '${pageContext.request.contextPath}/js/upload/uploadify.swf',
'uploadScript' : '${pageContext.request.contextPath}/uploadAvatar.do',
'fileObjName' : 'file',
'auto' : true,
'method' : 'post',
'multi' : false,
'cancelImg' : '${pageContext.request.contextPath}/js/upload/uploadify-cancel.png',
'fileTypeExts' : '*.jpg;*.png',
'fileSizeLimit' : '2MB',
'onUploadComplete' : function(file, data, response) {
//jcrop
$("#target").removeAttr("src");
$("#target")
.attr(
"src",
"${pageContext.request.contextPath}/uploadAvatar/"
+ strToJson(data).fileAvatarUrl);
$("#cutImageAvater").css("display","block");
displayJcrop();
//jcrop preview
$("#newpicture").removeAttr("src");
$("#newpicture")
.attr(
"src",
"${pageContext.request.contextPath}/uploadAvatar/"
+ strToJson(data).fileAvatarUrl);
},
//加上此句会重写onSelectError方法【需要重写的事件】
'overrideEvents' : [ 'onSelectError',
'onDialogClose' ],
//返回一个错误,选择文件的时候触发
'onError' : function(file, fileType,
data) {
alert("图片类型错误或者图片太大!");
}
});
});
//将字符串转换成json格式
function strToJson(data) {
var a = JSON.parse(data);
return a;
}
</script>
<script type="text/javascript">
var jcrop_api,boundx, boundy;//定义全局变量实现jcrop刷新图片的功能。
function displayJcrop(){
var $preview = $('#preview-pane'),
$pcnt = $('#preview-pane .preview-container'),
$pimg = $('#preview-pane .preview-container img'),
xsize = $pcnt.width(),
ysize = $pcnt.height();
// Create variables (in this scope) to hold the API and image size
// var jcrop_api,boundx, boundy,
// Grab some information about the preview pane
// destroy Jcrop if it is existed
if (typeof jcrop_api != 'undefined') //实现jcrop刷新图片的功能
jcrop_api.destroy();
boundx=null;boundy=null;jcrop_api=null;
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ],
aspectRatio: 1
},function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
// Move the preview into the jcrop container for css positioning
$preview.appendto(null);//释放预览图片的空间,使预览图片刷新。
$preview.appendTo(jcrop_api.ui.holder);
});
function updatePreview(c)
{
var x=c.x;
var y=c.y;
var w=c.w;
var h=c.h;
$("#x1").val(parseInt(x));
$("#y1").val(parseInt(y));
$("#w").val(parseInt(w));
$("#h").val(parseInt(h));
if (parseInt(c.w) > 0)
{
var rx = xsize / c.w;
var ry = ysize / c.h;
$pimg.css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
}
</script>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><html><head><title>头像上传</title><!-- 在裁剪图片页面中,引入两个js文件,和1个Jcrop需要的css文件(Jcrop包中有,注意引入顺序,先引入jQuery --><link rel="stylesheet" type="text/css"href="../css/upload/uploadifive.css"><link rel="stylesheet" type="text/css"href="../css/upload/jquery.Jcrop.min.css" /><link rel="stylesheet" type="text/css"href="../css/css/bootstrap.min.css" /><script type="text/JavaScript" src="../js/upload/jquery.min.js"></script><script type="text/javascript" src="../js/upload/jquery.uploadifive.min.js"></script><script type="text/javascript" src="../js/upload/jquery.Jcrop.min.js"></script><script type="text/javascript" src="../js/upload/jquery.color.js"></script><script type="text/javascript" src="../js/bootstrap.min.js"></script><style type="text/css">#some_file_queue {display: none;//绑定到隐藏的div,使进度条消失。}body { background-color: #d0e4fe;}</style><script type="text/javascript">$(function(){$("#upload_org_code").uploadifive({'queueID' : 'some_file_queue',//绑定到隐藏的div,使进度条消失。'height' : 27,'width' : 80,'fileType' :'image', 'buttonText' : '选择图片',//'swf' : '${pageContext.request.contextPath}/js/upload/uploadify.swf','uploadScript' : '${pageContext.request.contextPath}/uploadAvatar.do','fileObjName' : 'file','auto' : true,'method' : 'post','multi' : false,'cancelImg' : '${pageContext.request.contextPath}/js/upload/uploadify-cancel.png','fileTypeExts' : '*.jpg;*.png','fileSizeLimit' : '2MB',
'onUploadComplete' : function(file, data, response) {//jcrop$("#target").removeAttr("src");$("#target").attr("src","${pageContext.request.contextPath}/uploadAvatar/"+ strToJson(data).fileAvatarUrl);$("#cutImageAvater").css("display","block");displayJcrop();//jcrop preview$("#newpicture").removeAttr("src");$("#newpicture").attr("src","${pageContext.request.contextPath}/uploadAvatar/"+ strToJson(data).fileAvatarUrl);
},//加上此句会重写onSelectError方法【需要重写的事件】'overrideEvents' : [ 'onSelectError','onDialogClose' ],//返回一个错误,选择文件的时候触发'onError' : function(file, fileType,data) {alert("图片类型错误或者图片太大!");}});});//将字符串转换成json格式function strToJson(data) {var a = JSON.parse(data);return a;}</script>
<script type="text/javascript">var jcrop_api,boundx, boundy;//定义全局变量实现jcrop刷新图片的功能。
function displayJcrop(){ var $preview = $('#preview-pane'), $pcnt = $('#preview-pane .preview-container'), $pimg = $('#preview-pane .preview-container img'),
xsize = $pcnt.width(), ysize = $pcnt.height(); // Create variables (in this scope) to hold the API and image size // var jcrop_api,boundx, boundy,
// Grab some information about the preview pane // destroy Jcrop if it is existed if (typeof jcrop_api != 'undefined') //实现jcrop刷新图片的功能 jcrop_api.destroy(); boundx=null;boundy=null;jcrop_api=null; $('#target').Jcrop({ onChange: updatePreview, onSelect: updatePreview, bgOpacity: .4, setSelect: [ 100, 100, 50, 50 ], aspectRatio: 1 },function(){ // Use the API to get the real image size var bounds = this.getBounds(); boundx = bounds[0]; boundy = bounds[1]; // Store the API in the jcrop_api variable jcrop_api = this;
// Move the preview into the jcrop container for css positioning $preview.appendto(null);//释放预览图片的空间,使预览图片刷新。 $preview.appendTo(jcrop_api.ui.holder); }); function updatePreview(c) {var x=c.x; var y=c.y; var w=c.w; var h=c.h; $("#x1").val(parseInt(x)); $("#y1").val(parseInt(y)); $("#w").val(parseInt(w)); $("#h").val(parseInt(h)); if (parseInt(c.w) > 0) { var rx = xsize / c.w; var ry = ysize / c.h;
$pimg.css({ width: Math.round(rx * boundx) + 'px', height: Math.round(ry * boundy) + 'px', marginLeft: '-' + Math.round(rx * c.x) + 'px', marginTop: '-' + Math.round(ry * c.y) + 'px' }); } };
}</script><script type="text/javascript">function goBack(){window.history.back();}</script><style type="text/css">
/* Apply these styles only when #preview-pane has been placed within the Jcrop widget right: -280px; */ #preview-pane { display: block; position: absolute; z-index: 2000; top: 150px; left:450px; padding: 6px; border: 1px rgba(0,0,0,.4) solid; background-color: white;
-webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px;
-webkit-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2); -moz-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2); box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);}
/* The Javascript code will set the aspect ratio of the crop area based on the size of the thumbnail preview, specified here */#preview-pane .preview-container { width: 250px; height: 170px; overflow: hidden;}.inputAxiesClass{border-radius: 6px;}</style>
</head><body style="background-image: url(../images/section9_.jpg)"><div class="head" style="border-bottom-style: solid; border-bottom-color:#0066CC"> <a href="../indexmain.jsp"><img alt="阜特科技" src="../images/flogo_.png"> </a> </div><div id="cutImageAvater" style="width:600px;height:400px; display:none; margin-left: 10px" ><img src="" id="target" alt="上传图片展示区" style="width:300px;height:300px;" />
<div id="preview-pane"> <div class="preview-container"> <img src="" class="jcrop-preview" alt="预览图片" id="newpicture" /> </div> </div> <br> <form action="../saveAvatar.do" method="post" > x轴:<input type="text" size="4" id="x1" name="x" readonly="true" class="inputAxiesClass"/> y轴:<input type="text" size="4" id="y1" name="y" readonly="true" class="inputAxiesClass"/> 宽度:<input type="text" size="4" id="w" name="w" readonly="true" class="inputAxiesClass"/> 高度:<input type="text" size="4" id="h" name="h" readonly="true" class="inputAxiesClass"/> <input type="submit" value="保存裁剪" class="btn btn-success"/> <input type="button" value="返回首页" onclick="goBack()" class="btn btn-primary"/> </form> </div><form action="" method="post" enctype="multipart/form-data" style="margin-top:10px"><div id="some_file_queue"></div><input type="file" name="file" id="upload_org_code" /> </form> </html>