[英]How to change the size of image with given new height and width using java?
This is my demo.jsp page where i have two fields to give new height and width to change the uploaded image size with new one.could anybody tell me how to do this? 这是我的demo.jsp页面,其中我有两个字段可以提供新的高度和宽度以更改新的高度和宽度。有人可以告诉我该怎么做吗?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ImageSizeModification</title>
<script>
function ImageResizer() {
console.log('called');
var srcImg = document.getElementById("imgID").value;
var newImg = new Image();
newImg.src = srcImg;
var height = newImg.height;
var width = newImg.width;
var newWidth = document.getElementById("widthID").value;
var newHeight = document.getElementById("HeightID").value;
console.log(newImg, srcImg, newWidth, newHeight, height, width);
}
</script>
</head>
<body>
<%
out.println("<table>");
out.println("<tr><td><input type='file' id='imgID'></td></tr>");
out.println("<tr><td>Enter new Width:</td><td><input type='text' id='widthID'></td></tr>");
out.println("<tr><td>Enter new Height:</td><td><input type='text' id='HeightID'></td></tr>");
out.println("<tr><td><input type='button' value='Submit' onclick='ImageResizer()'></td></tr>");
out.println("</table>");
%>
</body>
If I understand correctly, what you want to do is to resize an image in the browser, so a smaller file goes up to your server. 如果我理解正确,您要做的是在浏览器中调整图像的大小,以便将较小的文件上传到服务器。 If that's so, this is not a Java/JSP question, but a Javascript one.
如果是这样,那么这不是Java / JSP问题,而是Javascript问题。
This post discusses how to do that using javascript libraries, and I think might solve your problem. 这篇文章讨论了如何使用javascript库做到这一点,我认为这可能会解决您的问题。
document.getElementById("imgID").style.height = parseInt(newWidth) ;
document.getElementById("imgID").style.width = parseInt(newHeight) ;
add above two line before console log. 在控制台日志之前添加以上两行。
or using Jquery 或使用Jquery
$('#imgID').width(700);
$('#imgID').height(700);
You cannot do it with jsp alone you need to create a servlet get the image through Inputstream manipulate it through any image manipulation api and write it to response. 您不能单独使用jsp来完成它,您需要创建一个servlet,通过Inputstream获取图像,并通过任何图像操作api对其进行操作,然后将其写入响应中。 Imgscalr is an easy to use api.
Imgscalr是易于使用的api。 You can refer this link example and clarification
您可以参考此链接示例和说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.