Resize Animated Gif Image

Hi Experts,Is there anyway to resize animated gif image using java tech.Thanks in advanceDishan
[123 byte] By [code4livea] at [2007-11-26 18:17:23]
# 1

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

Prashant_SDNa at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 2
Can you pls post some code sample ?
code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 3

// 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;

}

Prashant_SDNa at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 4

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)

code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 5
Is there anyway to resize gif image without using JAI?Thanks in advance
code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 6
Do you want to resize it for display, or save the resized gif to a file?
Rodney_McKaya at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 7
Yes. I want to resize animated gif image and save it to disk.
code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 8

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

Rodney_McKaya at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 9

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!");

-

code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 10
Thanks Rodney. It works!
code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 11

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();

}

code4livea at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...
# 12
This will not work because getScaledInstance returns 1 image that is not animated.Also I don't think GifEncoder supports animated gifs.
Rodney_McKaya at 2007-7-9 5:50:58 > top of Java-index,Security,Cryptography...