Solved

How to calculator to scale for limiting the image size!

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

729 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