Solved

How to calculator to scale for limiting the image size!

Posted on 2006-11-13
6
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

696 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