Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

scaleing of images

Thread view: 
Oliver Schalch - 08 Dec 2005 11:07 GMT
Hi List,

I'm trying to scale an image. I work with:

java.awt.Image;
java.awt.image.BufferedImage;
javax.imageio.ImageIO;

I didnt find a method to scale by percent, probably I have to take
another class, to scale my images iE. 50% bigger or smaller?

Or should I do just with some simple math .getWidth() * 2 and
.getHight)() * 2 or / 2

TIA

--
oli
Andrey Kuznetsov - 08 Dec 2005 12:41 GMT
> I'm trying to scale an image. I work with:
>
> java.awt.Image;
> java.awt.image.BufferedImage;
> javax.imageio.ImageIO;

with Image you can use:
java.awt.image.ReplicateScaleFilter
or java.awt.image.AreaAveragingScaleFilter

with BufferedImage use
java.awt.image.AffineTransformOp

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Rhino - 09 Dec 2005 15:06 GMT
> Hi List,
>
[quoted text clipped - 9 lines]
> Or should I do just with some simple math .getWidth() * 2 and
> .getHight)() * 2 or / 2

I recently had to do something similar to what you want to do. Your code
should look like very similar to this:

===========================================================
     BufferedImage bufferedImage = null;
     float scaleFactor = 0.5f;

     try {
               BufferedImage unalteredImage = reader.read(0);
               AffineTransformOp op = new
AffineTransformOp(AffineTransform.getScaleInstance(scaleFactor,
scaleFactor), AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                   BufferedImage scaledImage = op.filter(unalteredImage,
null);
                   bufferedImage = scaledImage;
           } catch (IOException io_excp) { //error handling }

===========================================================

Using 'AffineTransform.getScaleInstance(scaleFactor, scaleFactor)' ensures
that the scaled image has the same aspect ratio as the original image.

You may want to experiment with different values for the second parameter in
the AffineTransformOp instantiation; there are other possibilities besides
AffineTransformOp.TYPE_NEAREST_NEIGHBOR but I'm not particularly clear from
the API how they differ from one another in effect. It might be worthwhile
for you to try a few different values and see which ones give the
fastest/clearest results.

Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.