Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Compression

 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to compress all types of images, JPEG/GIF/TIFF/PNG etc in Java. JAI seems to be using deprecated classes and methods. Is there any other option?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain in more detail what exactly you want to do?

Saving an image in a file is very easy with the ImageIO API. It supports a number of different formats (the API documentation explains which ones). If you just want to save an image in for example JPG format, without worrying about the details, it's just one line of code:

If you want to be able to set parameters, for example the compression quality, then you'll have to look a little bit deeper into the ImageIO API. Here is an example that shows how to set the compression quality for a JPEG image:
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly what I was looking for... Thanks!

But I am getting an error when I am trying to compress images of types other than JPEG.

I am getting the following error

I even tried the other 3 compression types viz. BI_RGB, BI_RLE8, BI_RLE4. But I am getting the same error.
I am also not able to compress other images like GIF/PNG/TIFF etc.

Any workaround???
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compression quality is unique to JPEG; note that while it does reduce the image size, it also reduces image quality. For all other image formats, use a value of 0.0 to indicate that you want high compression, just like the javadocs say. Also try setting the compression type before setting the compression quality; that's also mentioned in the javadocs.

Also note that ImageIO does not support TIFF images at all unless you make it so.
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This seems to be working.

Just a few questions.
  • On successful compression, the size of the image is not reducing!!! Why is it so?
  • Are there any other "compressionType" other than LZW for GIF?
  •  
    Ralph Cook
    Ranch Hand
    Posts: 479
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Keep in mind that one major reason for image filetypes in general is their compression -- most of them are already compressed, and if there were an easy way to compress them further, the designers of that file format would already have used it.

    Compression algorithms tend to take streams of bytes and find ways to represent the same information without repeating bytes, patterns, etc.; the GIF and PNG and other graphic formats have already done that, so they don't have many of the patterns other compression algorithms look for.

    rc
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic