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