I\'ve seen several examples of making compressed JPG images from Java BufferedImage
objects by writing to file, but is it possible to perform JPG compression wi
Just pass your ByteArrayOutputStream
to ImageIO.createImageOutputStream(...)
like this:
// The important part: Create in-memory stream
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
try (ImageOutputStream outputStream = ImageIO.createImageOutputStream(compressed)) {
// NOTE: The rest of the code is just a cleaned up version of your code
// Obtain writer for JPEG format
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("JPEG").next();
// Configure JPEG compression: 70% quality
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.7f);
// Set your in-memory stream as the output
jpgWriter.setOutput(outputStream);
// Write image as JPEG w/configured settings to the in-memory stream
// (the IIOImage is just an aggregator object, allowing you to associate
// thumbnails and metadata to the image, it "does" nothing)
jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam);
// Dispose the writer to free resources
jpgWriter.dispose();
}
// Get data for further processing...
byte[] jpegData = compressed.toByteArray();
PS: By default, ImageIO
will use disk caching when creating your ImageOutputStream
. This may slow down your in-memory stream writing. To disable it, use ImageIO.setCache(false)
(disables disk caching globally) or explicitly create an MemoryCacheImageOutputStream
(local), like this:
ImageOutputStream outputStream = new MemoryCacheImageOutputStream(compressed);