Solved

How to calculator to scale for limiting the image size!

Posted on 2006-11-13
6
823 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 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