Inputstream to BufferedImage Conversion damages the file

15,919

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"));
Share:
15,919
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.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!

  • Jeff
    Jeff over 13 years
    Can you get any additional information from the exception's stack trace?
  • Admin
    Admin over 13 years
    @Zarkonnen: I used your getGraphics().drawImage(). But again the previous problem. Image file with 0 bytes. No errors in the console. No stack strace. I think the conversion only making the problem. Do we have any other way to convert InputStream to BufferedImage or Is there any way to get BUfferedImage instead of InputStream from the list item.
  • Zarkonnen
    Zarkonnen over 13 years
    It's quite possible there's some other issue as well. Are you really seeing an output size of 0 from using ImageIO.write on a freshly created BufferedImage with nonzero width/height? As in, what happens if you just go ImageIO.write(new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB), "jpg", new File("foo.jpg")); ?
  • Zarkonnen
    Zarkonnen over 13 years
    OK, try the code I've just edited in. That should give you a 100x100 scaled image, or an error message if the loading failed. It's also possible that ImageIO can't recognise the image, or that it did indeed get somehow corrupted.
  • Admin
    Admin over 13 years
    @Zarkonnen: May i know what you changed your in code than previous one. I cannot able to see any changes in your code. I am getting correct Output image. Very very Thanks man. Thanks for your support!! :)
  • Zarkonnen
    Zarkonnen over 13 years
    I actually mostly glued together your code and mine. There was probably a typo or a misunderstanding somewhere. Glad it works now. :)
  • Admin
    Admin over 13 years
    @Zarkonnen: Yes, you are right. It's also possible that ImageIO can't recognise the image, or that it did indeed get somehow corrupted. Its happening now.
  • Tom Lint
    Tom Lint over 9 years
    If cropping is allowed, you can simply call BufferedImage.subImage with the desired parameters, and save the resulting BufferedImage using ImageIO. Should be faster than drawing into a new BufferedImage.