Home / Java Patterns and Pitfalls     frequal.com

Fix Broken JPEG Files Saved Using ImageIO

If your JPEG files saved using ImageIO.write() method are appearing corrupted in an image viewer, try this easy fix. Look for your code where you are creating the image to be saved:
    // Don't do this, the 'A' causes unreadable JPG files
    BufferedImage imageNew = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_ARGB);
Change TYPE_INT_ARGB to TYPE_INT_RGB. That's right, remove the "A" and try again. It should work much better.
    // Drop the 'A', get a readable JPG
    BufferedImage imageNew = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);

Brief Analysis

The 'A' is for the alpha channel. JPEGs only started supporting alpha channels in 2000, and apparently many programs don't support them properly. if you can do without the alpha channel this tip makes the JPEGs readable by a wider range of applications.
Last modified on 1 Nov 2009 by AO

Copyright © 2024 Andrew Oliver