Here you can find the source of encodeAndWriteJPEGFile(File file, BufferedImage image, float quality)
private static void encodeAndWriteJPEGFile(File file, BufferedImage image, float quality) throws IOException
//package com.java2s; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageOutputStream; public class Main { private static void encodeAndWriteJPEGFile(File file, BufferedImage image, float quality) throws IOException { Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output);//from w w w .ja v a 2 s . co m IIOImage iioi = new IIOImage(image, null, null); writer.write(null, iioi, iwp); writer.dispose(); } }