0

i am getting some problem to compess png image.

this is my code where i use to compress

BufferedImage img = ImageIO.read(fileinput)
    ByteArrayOutputStream baos = new ByteArrayOutputStream()

    ImageOutputStream ios = ImageIO.createImageOutputStream(baos)

    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg")
    ImageWriter writer = iter.next()
    ImageWriteParam iwp = writer.getDefaultWriteParam()
    iwp.setCompressionMode(2)
    iwp.setCompressionQuality(0.2)
    writer.setOutput(ios)
    writer.write(null, new IIOImage(img, null, null), iwp)
    writer.dispose()
    baos.flush()
    FileOutputStream fos = new FileOutputStream(fileinput)
    fos.write(baos.toByteArray())
    fos.close()

but i don't know why if i compress png file..it always become RED

this is my image before compress processing enter image description here

this is my image after compressed enter image description here

  • 1
    This question may be very close to your problem : http://stackoverflow.com/questions/17755036/imgscalr-with-background-red – Arnaud Jan 11 '17 at 10:24
  • 1
    It's because 1) your `img` contains alpha channel (even if it's all opaque), 2) you use JPEG compression *and* 3) JPEG does not have a way to specify RGBA, so most software (like your browser) will misinterpret it as CMYK. Either store in a different format (like PNG) *or* convert `img` to `TYPE_3BYTE_BGR` or `TYPE_INT_RGB`. – Harald K Jan 11 '17 at 10:26
  • @haraldK so i need to convert my image to `TYPE_INT_RGB` before compressing? –  Jan 11 '17 at 10:30
  • Yes. Sorry for answering in comment. I'm sure it's a duplicate, but had no time to look it up... – Harald K Jan 11 '17 at 10:34
  • oh okay, thank you @Berger –  Jan 11 '17 at 10:46

0 Answers0