A more succinct way is to get the ImageWriter directly from ImageIO:
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.7f);
ImageOutputStream outputStream = createOutputStream(); // For example implementations see below
jpgWriter.setOutput(outputStream);
IIOImage outputImage = new IIOImage(image, null, null);
jpgWriter.write(null, outputImage, jpgWriteParam);
jpgWriter.dispose();
The call to ImageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) is needed in order to explicitly set the compression’s level (quality).
In ImageWriteParam.setCompressionQuality() 1.0f is maximum quality, minimum compression, while 0.0f is minimum quality, maximum compression.
ImageWriter.setOutput should be passed an ImageOutputStream. While the method accepts Object, according to documentation it’s usually not supported:
Use of a general
Objectother than anImageOutputStreamis intended for writers that interact directly with an output device or imaging protocol. The set of legal classes is advertised by the writer’s service provider’sgetOutputTypesmethod; most writers will return a single-element array containing onlyImageOutputStream.classto indicate that they accept only anImageOutputStream.
Most cases should be handled by these two classes:
FileImageOutputStream– an implementation ofImageOutputStreamthat writes its output directly to aFileorRandomAccessFile.MemoryCacheImageOutputStream– an implementation ofImageOutputStreamthat writes its output to a regularOutputStream. Usually used withByteArrayOutputStream(thanks for the tip, @lmiguelmh!).