Skip to content

Instantly share code, notes, and snippets.

@xtraclass
Created May 25, 2013 19:47
Show Gist options
  • Save xtraclass/5650524 to your computer and use it in GitHub Desktop.
Save xtraclass/5650524 to your computer and use it in GitHub Desktop.
Java: saves a JPEG Buffered Image to a file. Uses the old Sun packages :-)
import com.sun.image.codec.jpeg.*;
...
public static void saveImageToFile( BufferedImage image, File file )
throws IOException
{
if ( image == null || file == null ) return;
file.delete();
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( file ) );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( image );
param.setQuality( 1, false );
encoder.setJPEGEncodeParam( param );
encoder.encode( image );
out.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment