简体   繁体   English

如何使用java在给定新高度和宽度的情况下更改图像大小?

[英]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.

相关问题 如何使用 java 获取图像的高度和宽度? - How to get image height and width using java? java函数确定具有给定宽度的图像的高度? - java function to determine height of an image with the given width? 如何使用Java更改HTML文件的宽度和高度 - How to change the width and height of an html file using java 如何在不影响Java中的图像实际纵横比的情况下,为图像计算新的高度和宽度以适合预定义的高度和宽度? - How to calculate new height and width for an image to fit with in predefined height and width, without affecting the image real aspect ratio in java? 如何获得新的调整尺寸图像的高度和宽度? - How to obtain new height and width of resized image? 通过Java缩小JPEG图像大小而不缩放其宽度/高度 - Reduce JPEG image size without scale its width/height by Java 如何使用蜡染获得 SVG 图像的图像大小(宽度/高度) - How to get the image size (width/height) of an SVG image with batik 如何使用java获取pdf中任何给定单词的(x,y width height) - How to get (x,y width height )of any given word in pdf using java 如何在 JAVA 中使用 itext API 创建固定大小(高度和宽度)的二维条码? - How to create fixed size(height and width) 2d barcode using itext API in JAVA? 如何在CodenameOne中更改组件的大小(宽度和高度) - How is possible to change size(width and height) of the component in codenameone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM