yes you can scale the image with help of JAI.
// Create a ParameterBlock and specify the source and
// parameters
ParameterBlock pb = new ParameterBlock();
pb.addSource(im);// The source image
pb.add(1.2F);// The xScale
pb.add(1.2F);// The yScale
pb.add(0.0F);// The x translation
pb.add(0.0F);// The y translation
pb.add(new InterpolationNearest()); // The interpolation
// Create the scale operation
im = JAI.create("scale", pb, null);
for detail visit this
http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html
~ Prashant
// InFile is the path of image
PlanarImage src = loadImage(inFile);
BufferedImage bi = src.getAsBufferedImage();
ParameterBlock pb = new ParameterBlock();
pb.addSource(bi);
pb.add(.50F); // half the x dimension
pb.add(.50F); // half the y dimension
PlanarImage image = JAI.create("scale", pb, null).getRendering();
// now you have image and you can store it whereever you want
// Load the source image.
private PlanarImage loadImage(String imageName)
{
ParameterBlock pb = (new ParameterBlock()).add(imageName);
PlanarImage src1 = JAI.create("fileload", pb);
if (src1 == null)
{
System.out.println("Error in loading image " + imageName);
System.exit(1);
}
return src1;
}
Thanks for your reply
It gives me --
Error: Could not find mediaLib accelerator wrapper classes. Continuing in pure Java mode.
Occurs in: com.sun.media.jai.mlib.MediaLibAccessor
java.lang.NoClassDefFoundError: com/sun/medialib/mlib/Image
at com.sun.media.jai.mlib.MediaLibAccessor$1.run(MediaLibAccessor.java:248)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.media.jai.mlib.MediaLibAccessor.setUseMlib(MediaLibAccessor.java:245)
at com.sun.media.jai.mlib.MediaLibAccessor.useMlib(MediaLibAccessor.java:177)
at com.sun.media.jai.mlib.MediaLibAccessor.isMediaLibCompatible(MediaLibAccessor.java:357)
at com.sun.media.jai.mlib.MediaLibAccessor.isMediaLibCompatible(MediaLibAccessor.java:315)
at com.sun.media.jai.mlib.MlibScaleRIF.create(MlibScaleRIF.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888)
at JAITest.save(JAITest.java:32)
at JAITest.main(JAITest.java:41)
No way to do this in java today.
You can't even save a GIF in java without using JAI.
You can use [url http://www.gif4j.com/java-gif4j-pro-gif-image-encode-save.htm]Gif4J[/url], it's commercial but it works and works very well.
There's another option:
Read the GIF images one by one using ImageIO (GIFImageDecoder), and also read the metadata.
And then use this code to write the new GIF after scaling every image on its own.
http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm
Message was edited by:
Rodney_McKay
Thank 'Rodney_McKay' for ur reply.
Now im tring to resize it using acme GifEncoder class. Its resize, but not animated :(
my code as follows:
-
Image oldImage = Toolkit.getDefaultToolkit().createImage("E:\\Dog.gif");
Image newImage = oldImage.getScaledInstance(64,64,1);
File outputFile = new File("E:\\DogZ.gif");
FileOutputStream fos = new FileOutputStream(outputFile);
DataOutputStream dos = new DataOutputStream(fos);
GifEncoder gifEn = new GifEncoder(newImage, dos, true);
gifEn.encode();
dos.flush();
dos.close();
System.out.println("Done!");
-
This is the code :
try {
FileOutputStream out = new FileOutputStream("E:\\DogZ.gif");
GifDecoder d = new GifDecoder();
AnimatedGifEncoder e = new AnimatedGifEncoder();
d.read("E:\\Dog.gif");
int n = d.getFrameCount();
int loopCount = d.getLoopCount();
e.setRepeat(loopCount);
e.start(out);
for (int i = 0; i < n; i++) {
BufferedImage frame = d.getFrame(i); // frame i
int t = d.getDelay(i); // display duration of frame in milliseconds
e.setDelay(t);// 1 frame per sec
e.addFrame(frame.getSubimage(0, 0, 64, 46));
}
e.finish();
out.flush();
out.close();
System.out.println("done!");
} catch (Exception e) {
e.printStackTrace();
}