How to generate dynamic GIF images in Java

Source: Internet
Author: User

Examples of generating GIF images are not found on the Internet. However, many watermark files are generated. If we make GIF images into watermarks, the images will not move. So I wrote a small example for your reference. (If you want to print a dynamic icon on an image, this class can be well implemented, provided that you must first break down the dynamic icon into several separate GIF images. Of course, you can use a program .)

To useThe animatedgifencoder class is automatically downloaded.

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

Package com. test;

Import java. AWT. graphics;
Import java. AWT. image;
Import java. AWT. image. bufferedimage;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import javax. ImageIO. ImageIO;

Public class makeover {
 
Public static void main (string [] ARGs ){

Makeover mo = new makeover ();
Mo. creategif ("E:/PIC/test.gif", "E:/map.gif", "E:/PIC/", 82,395 );
// Parameter list: output image address, watermark image address, dynamic icon address, ordinate, and abscissa
// Note: the files in this E:/PIC/directory are as follows: 1.gif 2.gif 3.gif ....
}

 
Public void creategif (string outputfilename, string path1, string path2, int height, int weidth ){
Try {
// Specify the frame File
Animatedgifencoder E = new animatedgifencoder ();
Outputstream OS = new fileoutputstream (outputfilename); // output image
E. Start (OS); // start processing
E. setquality (15); // sets the image quality.
E. setrepeat (0); // sets the loop
E. setdelay (500); // sets the delay time.
Makeover abc = new makeover (); // instantiate the image merging class
String path3 = ""; // dynamic image address
For (INT I = 1; I <3; I ++) {// Add only two GIF images here
Path3 = path2 + I + ". GIF ";
Bufferedimage im = ABC. pressimage (path3, path1, weidth, height );
E. addframe (IM); // Add frame to the loop
}
E. Finish ();

} Catch (exception e ){
System. Out. println (E );
E. printstacktrace ();
}
}
 
 
Public bufferedimage pressimage (string pressimg,
String targetimg, int X, int y ){
Try {
// Target file
File _ file = new file (targetimg );
Image src = ImageIO. Read (_ file );
Int Wideth = SRC. getwidth (null );
Int Height = SRC. getheight (null );
Bufferedimage image = new bufferedimage (Wideth, height,
Bufferedimage. type_int_rgb );
Graphics G = image. creategraphics ();
G. drawimage (SRC, 0, 0, Wideth, height, null );

// Watermark File
File _ filebiao = new file (pressimg );
Image src_biao = ImageIO. Read (_ filebiao );
Int wideth_biao = src_biao.getwidth (null );
Int height_biao = src_biao.getheight (null );
G. drawimage (src_biao, X,
Y, wideth_biao, height_biao, null );
// Watermark file ended
G. Dispose ();

Return image;

} Catch (exception e ){
System. Out. println (E );
E. printstacktrace ();
}
Return NULL;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.