Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to calculator to scale for limiting the image size!

Posted on 2006-11-13
6
Medium Priority
?
828 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

916 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