It's not a secret anymore!*
The moose likes Java in General and the fly likes Image Compression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Image Compression" Watch "Image Compression" New topic
Author

Image Compression

Mahendra Kariya
Greenhorn

Joined: Aug 25, 2008
Posts: 26

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mahendra Kariya
Greenhorn

Joined: Aug 25, 2008
Posts: 26

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
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
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.


Ping & DNS - my free Android networking tools app
Mahendra Kariya
Greenhorn

Joined: Aug 25, 2008
Posts: 26

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

    Joined: May 29, 2005
    Posts: 479
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Image Compression
     
    Similar Threads
    DICOM
    Help for java ImageIO API for PNG Images
    WA #1.....word association
    code needed
    Image compression and decompression