Solved

How to calculator to scale for limiting the image size!

Posted on 2006-11-13
6
817 Views
Last Modified: 2013-11-22
Hi Experts,
I need to limit the size of the out put animated GIF file <=100 Kb,
So, when the output GIF image size > 100 Kb I need to scale this to make the size reduce to about 100Kb,

But I don't know how to calculator for ratios for scaling (width and height for scaling).

Please help me with this.

Thank in advance!
0
Comment
Question by:CongUan
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 17937302
If the image is larger than 100kb, read it in using the ImageIO class. The call getScaledInstance on the BufferedImage received from calling the read method on ImageIO. Calculate the scaling factor using the code below;

      int width = image.getWidth();
      int height = image.getHeight();

      int desiredWidth = (int) (width / ((double) file.length() / 1024) * DESIRED_FILE_SIZE_IN_KB);
      int desiredHeight = (int) (height / ((double) file.length() / 1024) * DESIRED_FILE_SIZE_IN_KB);

Only one problem remains; there is no GIF encoder available in the JDK or the JRE. You have to either save as a different file format (I recommend PNG), or you have to license a GIF encoder.
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 500 total points
ID: 17937347
Hmm, just realised that you asked for how to reduce the size of an ANIMATED GIF... AFAIK the individual images that make up a GIF animation are basically listed after each other in the file. You should be able to read and render each individual frame/image and scale them individually, then reassemble them for output. You will still have to solve the problem with a GIF encoder.
0
 
LVL 6

Expert Comment

by:superm401
ID: 17944467
orangehead911, your calculation will result in the scaling being squared.  

I.E. if he has a

500x500 file and the size is 200 kb:

Your code will make it 250x250, and thus 1/4 the size, and about 50 kb.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 17952781
Indeed, common mistake in recalculating square entities.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now