如何旋转图像并从JSP / Servlet页面保存它

时间:2022-10-17 21:18:55

I was able to successfully use JQueryRotate to rotate my images in a JSP page. However, the user now wants to have the ability to save the rotated image.

我能够成功地使用JQueryRotate在JSP页面中旋转我的图像。但是,用户现在希望能够保存旋转的图像。

But from what I've gathered so far, it seems that it's not possible to do this client side. So I probably have to re-start from scratch.

但是从目前为止我收集到的内容来看,似乎无法做到这一点。所以我可能不得不从头开始重新开始。

Is it possible to do a rotate image with saving capability for images rendered in a JSP/Servlet? If yes, how?

是否可以对JSP / Servlet中呈现的图像执行保存功能的旋转图像?如果有,怎么样?

EDIT: By the way, HTML5 is out of the questions because my requirement is to still support IE9.

编辑:顺便说一下,HTML5是不可能的,因为我的要求是仍然支持IE9。

1 个解决方案

#1


0  

Well, just to sumarize. Using non-HTML5 browsers - no way to save the modified on the client side picture. In a modern browser - you could try to save a canvas containing the image. Also you could keep track of the css trasformation and, on demand, render trasformed image but this is server side and just will show rotated image, not save it. Other thing you could do is send the transformed image to the server when the user ask for saving operation, rotate/transform the image using image manipulation software/library on the server side and send the new image back to the client. This could work, for example php has some image libraries. BTW, some social sites share images but, I think, all the transformation if any (crop, cut, resize, rotate...) is made on the server, when the user upload the image and it is saved on the server. BTW (2) a time ago in a J2EE application we had to do something like this because of user requirement - save an image rendered in a browser to the client machine. Will try to rescue the idea and if there is something interesting will post it.

好吧,只是为了sumarize。使用非HTML5浏览器 - 无法将修改后的内容保存在客户端图片上。在现代浏览器中 - 您可以尝试保存包含图像的画布。您还可以跟踪css转换,并根据需要渲染转换后的图像,但这是服务器端,只显示旋转的图像,而不是保存它。您可以做的其他事情是当用户要求保存操作时将转换后的图像发送到服务器,使用服务器端的图像处理软件/库来旋转/转换图像,并将新图像发送回客户端。这可能有用,例如php有一些图像库。顺便说一句,一些社交网站共享图像,但我认为,当用户上传图像并将其保存在服务器上时,所有转换(裁剪,剪切,调整大小,旋转......)都在服务器上进行。 BTW(2)前一段时间,在J2EE应用程序中,由于用户需求,我们不得不做这样的事情 - 将在浏览器中呈现的图像保存到客户机。将尝试拯救这个想法,如果有一些有趣的东西会发布它。

EDITED Here is some code we made a time ago for saving screen captures:

编辑这是我们前一段时间用于保存屏幕捕获的一些代码:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class Screenshot {

    public Screenshot() {
    }

    public static void imageCapture(int left, int top, int width, int height, String outFileName, String imgFormat) {
        if (!outFileName.toUpperCase().endsWith(imgFormat)) {
            outFileName += "." + imgFormat;
        }
        try {
            Robot robot = new Robot();
            BufferedImage bi = robot.createScreenCapture(new Rectangle(left, top, width, height));
            ImageIO.write(bi, imgFormat, new File(outFileName));
        }
        catch (AWTException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void imageCapture(int left, int top, int width, int height, OutputStream outStream, String imgFormat) {
        try {
            Robot robot = new Robot();
            BufferedImage bi = robot.createScreenCapture(new Rectangle(left, top, width, height));
            ImageIO.write(bi, imgFormat, outStream);
        }
        catch (AWTException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

As you see we used java.awt. Set full file path or already prepared OutputStream to the file you want to write and then send back to the browser the image file. This approach has a disadvantage - if you have scroll, the parte out of the window (let say "scrolled out") will no be captured. Well in fact it has many disadvantages but that's all I have and you can make some research. Hope this helps.

如您所见,我们使用了java.awt。将完整文件路径或已准备好的OutputStream设置为要写入的文件,然后将图像文件发送回浏览器。这种方法有一个缺点 - 如果你有滚动,窗外的部分(比如说“滚出”)就不会被捕获。事实上,它有许多缺点,但这就是我所拥有的,你可以做一些研究。希望这可以帮助。

#1


0  

Well, just to sumarize. Using non-HTML5 browsers - no way to save the modified on the client side picture. In a modern browser - you could try to save a canvas containing the image. Also you could keep track of the css trasformation and, on demand, render trasformed image but this is server side and just will show rotated image, not save it. Other thing you could do is send the transformed image to the server when the user ask for saving operation, rotate/transform the image using image manipulation software/library on the server side and send the new image back to the client. This could work, for example php has some image libraries. BTW, some social sites share images but, I think, all the transformation if any (crop, cut, resize, rotate...) is made on the server, when the user upload the image and it is saved on the server. BTW (2) a time ago in a J2EE application we had to do something like this because of user requirement - save an image rendered in a browser to the client machine. Will try to rescue the idea and if there is something interesting will post it.

好吧,只是为了sumarize。使用非HTML5浏览器 - 无法将修改后的内容保存在客户端图片上。在现代浏览器中 - 您可以尝试保存包含图像的画布。您还可以跟踪css转换,并根据需要渲染转换后的图像,但这是服务器端,只显示旋转的图像,而不是保存它。您可以做的其他事情是当用户要求保存操作时将转换后的图像发送到服务器,使用服务器端的图像处理软件/库来旋转/转换图像,并将新图像发送回客户端。这可能有用,例如php有一些图像库。顺便说一句,一些社交网站共享图像,但我认为,当用户上传图像并将其保存在服务器上时,所有转换(裁剪,剪切,调整大小,旋转......)都在服务器上进行。 BTW(2)前一段时间,在J2EE应用程序中,由于用户需求,我们不得不做这样的事情 - 将在浏览器中呈现的图像保存到客户机。将尝试拯救这个想法,如果有一些有趣的东西会发布它。

EDITED Here is some code we made a time ago for saving screen captures:

编辑这是我们前一段时间用于保存屏幕捕获的一些代码:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class Screenshot {

    public Screenshot() {
    }

    public static void imageCapture(int left, int top, int width, int height, String outFileName, String imgFormat) {
        if (!outFileName.toUpperCase().endsWith(imgFormat)) {
            outFileName += "." + imgFormat;
        }
        try {
            Robot robot = new Robot();
            BufferedImage bi = robot.createScreenCapture(new Rectangle(left, top, width, height));
            ImageIO.write(bi, imgFormat, new File(outFileName));
        }
        catch (AWTException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void imageCapture(int left, int top, int width, int height, OutputStream outStream, String imgFormat) {
        try {
            Robot robot = new Robot();
            BufferedImage bi = robot.createScreenCapture(new Rectangle(left, top, width, height));
            ImageIO.write(bi, imgFormat, outStream);
        }
        catch (AWTException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

As you see we used java.awt. Set full file path or already prepared OutputStream to the file you want to write and then send back to the browser the image file. This approach has a disadvantage - if you have scroll, the parte out of the window (let say "scrolled out") will no be captured. Well in fact it has many disadvantages but that's all I have and you can make some research. Hope this helps.

如您所见,我们使用了java.awt。将完整文件路径或已准备好的OutputStream设置为要写入的文件,然后将图像文件发送回浏览器。这种方法有一个缺点 - 如果你有滚动,窗外的部分(比如说“滚出”)就不会被捕获。事实上,它有许多缺点,但这就是我所拥有的,你可以做一些研究。希望这可以帮助。