2

I'm trying to compress images with this method: Setting jpg compression level with ImageIO in Java.

The solution works fine, but I use ObjectOutputStream instead of FileImageOutputStream, when I set the ObjectOutputStream as the writer output I get an java.lang.IllegalArgumentException: Illegal output type! exception. I need to use ObjectOutputStream.

This works perfectly ImageIO.write(img, "jpg", outstream);.

Exxul
  • 508
  • 5
  • 10

1 Answers1

4

When you call ImageIO.write(img, "jpg", outstream);, the method will invoke ImageIO.createImageOutputStream(outstream) and pass the result to the ImageWriter. When you call setOutput directly, you must do that yourself.

ImageWriter w = ImageIO.getImageWritersByFormatName("jpg").next();
w.setOutput(ImageIO.createImageOutputStream(outputStream));
w.write(image);
Holger
  • 285,553
  • 42
  • 434
  • 765