开发者

Inputstream to BufferedImage Conversion damages the file

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

InputStream imageStream = item.getInputStream();

BufferedImage imageBuffer = ImageIO.read(imageStream);

Then i resize the image and write in a location. But, all of the output files size is 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.

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

InputStream imageStream = item.getInputStream();

And, i am writing it by

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开发者_JAVA技巧.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!


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 dstImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ato.filter(uploadImage, dstImage);

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

Edit:

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

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"));


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

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜