Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Image Compression

 
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
  •  
    Ranch Hand
    Posts: 479
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Your mind is under my control .... your will is now mine .... read this tiny ad
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic