java Inputstream to BufferedImage Conversion 损坏文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4189006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:14:03  来源:igfitidea点击:

Inputstream to BufferedImage Conversion damages the file

javaimage-processinginputstreambufferedimage

提问by Zarkonnen

I am uploading some image files using servelt. I want to resize the images. I converts the source to BufferedImage using below lines.

我正在使用 servelt 上传一些图像文件。我想调整图像大小。我使用以下几行将源转换为 BufferedImage。

InputStream imageStream = item.getInputStream();

InputStream imageStream = item.getInputStream();

BufferedImage imageBuffer = ImageIO.read(imageStream);

BufferedImage imageBuffer = ImageIO.read(imageStream);

Then i resize the image and write in a location. But, all of the output files size is 0.

然后我调整图像大小并写入一个位置。但是,所有输出文件的大小都是0.

I am using the following code to resize the image.

我正在使用以下代码调整图像大小。

AffineTransform at = new AffineTransform();
if(sx != 0)
    at.scale( sx , sx );
AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
uploadImage = ato.filter(uploadImage, null); //uploadImage == BufferedImage

Is there any good way to convert inputstream to bufferedImage without damaging the image? I am sure that the image is getting uploaded. But, after the conversion to BufferedImage, the file damaged.

有没有什么好的方法可以将输入流转换为 bufferedImage 而不损坏图像?我确定图片正在上传。但是,在转换为 BufferedImage 后,文件损坏了。

I am uploading by submitting a form to doPost() method. The below line gives me the InputStream from a list item.

我通过向 doPost() 方法提交表单来上传。下面的行为我提供了列表项中的 InputStream。

InputStream imageStream = item.getInputStream();

InputStream imageStream = item.getInputStream();

And, i am writing it by

而且,我正在写它

ImageIO.write(image, "jpg", new File(path + ".jpg"));

ImageIO.write(image, "jpg", new File(path + ".jpg"));

Update

更新

java.awt.image.ImagingOpException: Unable to transform src image
at java.awt.image.AffineTransformOp.filter(Unknown Source)
at com.pricar.servlet.imageupload.ImageUploadServlet.reSize(ImageUploadServlet.java:100)
at com.pricar.servlet.imageupload.ImageUploadServlet.doPost(ImageUploadServlet.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Any suggesstions or link woulb be appreciative!!!

任何建议或链接将不胜感激!!!

Thanks!

谢谢!

回答by Zarkonnen

The reason your code isn't working is in

您的代码不起作用的原因在于

uploadImage = ato.filter(uploadImage, null); //uploadImage == BufferedImage

Your destination image is null.

您的目标图像为空。

You have to create a new BufferedImage to put the scaled version into, like this:

您必须创建一个新的 BufferedImage 以将缩放版本放入,如下所示:

BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ato.filter(uploadImage, dstImage);

Then, save the dstImage (using ImageIO.write).

然后,保存 dstImage(使用 ImageIO.write)。

Edit:

编辑:

An easier way to scale down the image would be to just draw it into the dstImage at the right size:

缩小图像的一种更简单的方法是以正确的大小将其绘制到 dstImage 中:

int dstWidth = 100;
int dstHeight = 100;
InputStream imageStream = item.getInputStream();
BufferedImage srcImage = ImageIO.read(imageStream);
if (srcImage == null) { System.err.println("NO SOURCE IMAGE!"); }
BufferedImage dstImage = new BufferedImage(dstWidth, dstHeight,
    BufferedImage.TYPE_INT_RGB);
dstImage.getGraphics().drawImage(
    srcImage, 0, 0, dstWidth, dstHeight, null);
ImageIO.write(dstImage, "jpg", new File(path + ".jpg"));

回答by Jeff

This may just be a typo in the question, but the filter operation on AffineTransformOp is being used incorrectly.

这可能只是问题中的一个错字,但 AffineTransformOp 上的过滤器操作使用不正确。

If uploadImage is the source, it shouldn't also be the destination for filtering. In fact, if you try to specify uploadImage as both parameters filter should throw an exception.

如果uploadImage 是源,则它不应也是过滤的目标。事实上,如果您尝试将 uploadImage 指定为两个参数过滤器应该抛出异常。

Create a BufferedImage instance to act as your destination and pass that to the filter method.

创建一个 BufferedImage 实例作为您的目的地并将其传递给过滤器方法。