使用jcrop和java裁剪图像
我试图使用JQuery Jcrop插件和Java工具裁剪图像,但无法得到正确的结果 Jsp代码:使用jcrop和java裁剪图像,java,jquery,spring,jcrop,Java,Jquery,Spring,Jcrop,我试图使用JQuery Jcrop插件和Java工具裁剪图像,但无法得到正确的结果 Jsp代码: <script type="text/javascript"> $(function () { $('#actualImage').Jcrop({ setSelect: [0, 0, 268, 180], addClass: 'custom', bgColor: 'yellow', bgOpacity: .8, //sideHan
<script type="text/javascript">
$(function () {
$('#actualImage').Jcrop({
setSelect: [0, 0, 268, 180],
addClass: 'custom',
bgColor: 'yellow',
bgOpacity: .8,
//sideHandles: true
allowResize: false,
allowSelect: false,
onSelect: storeCoords
});
});
function storeCoords(c) {
jQuery('#X1').val(c.x);
jQuery('#Y1').val(c.y);
jQuery('#X2').val(c.x2);
jQuery('#Y2').val(c.y2);
jQuery('#W').val(c.w);
jQuery('#H').val(c.h);
}
function cropPicture(){
$.ajax({
url: "cropPhoto.htm",
type: "POST",
data :{
x : $('input#X1').val(),
y : $('input#Y1').val(),
x2 : $('input#X2').val(),
y2 : $('input#Y2').val(),
w : $('input#W').val(),
h : $('input#H').val(),
imageName : $('input#imageName').val()
},
success: function (data) {
window.location = 'photo.htm';
}
}
</script>
我可以裁剪照片,但结果不正确。例如,请查看以下图像:
是的,我遇到了问题。我需要调整图像的大小。因此,我使用了以下代码:
public static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
现在我可以得到裁剪后的图像。实际问题是图像裁剪:您没有计算图像的实际自然高度和自然宽度。您可以尝试以下方法:
函数showCoords(c){
//获取服务器站点裁剪图像的图像自然高度/宽度。
var imageheight=document.getElementById('cropbox').naturalHeight;
var imagewidth=document.getElementById('cropbox').naturalWidth;
var xper=(c.x*100/jQuery('#cropbox').width());
var yper=(c.y*100/jQuery('#cropbox').height());
var wPer=(c.w*100/jQuery('#cropbox').width());
var hper=(c.h*100/jQuery('#cropbox').height());
var actX=(xper*imagewidth/100);
变量actY=(yper*图像高度/100);
var actW=(wPer*imagewidth/100);
var actH=(hper*成像高度/100);
jQuery('#x').val(parseInt(actX));
jQuery('#y').val(parseInt(actY));
jQuery('#w').val(parseInt(actW));
jQuery('#h').val(parseInt(actH));
};
如果您想要完整的示例,请参见确定公布的风险值正确吗?你还没有调整图像容器的大小?是的,谢谢你的回复。我用粘贴的代码解决了这个问题。
public static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}