Documentos de Académico
Documentos de Profesional
Documentos de Cultura
The GifDecoder java class lets programmers load and decode gif images from a gif file
formatted input stream with a single method call. The next methods to load and decode gif
images are available:
Note: If you load GIF images for displaying purposes (for example to display as image icons
within a swing application) then you should use the standard Java API:
java.awt.Toolkit#getImage, java.awt.Toolkit#createImage or
javax.swing.ImageIcon.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
// ...
public GifImage();
public GifImage(int resizeStrategy);
public GifImage(int width, int height);
public GifImage(int width, int height, int resizeStrategy);
The first two constructors create GifImage with indefinite general gif image width and height -
these parameters will be taken from the first added frame.
Hint: You can always get the current gif image width/height using the next methods:
Resize Strategies
Gif4J PRO undertakes to control general image size (take into consideration all internal frames'
size and their positions) using one of the predefined resize strategies: Crop to Fit Image Size,
Scale to Fit Image Size and Extend to Current. First two strategies allows to fix the general gif
image size (logical screen size). The last extends logical screen size to fit all added gif frames.
Below examples demostrate all strategies (see examples/ResizeStrategies):
Note: By default a new GifImage doesn't contain a Looping Application Extension Block at all
which is equal to 1 iteration. To set, for example, infinite looping you should call
gifImage.setLoopNumber(0).
Note: A lot of gif encoders and viewers (including Microsoft IE browser) ignore number of
iterations more than 1 and iterate an animated gif image indefinitely.
Hint: You can set default delay time between frames. This delay is automatically applied to
adding frames without delay parameter set. The next method should be used:
The next methods should be used to add, get and remove comments (please consult GifImage
API for more info):
import java.io.File;
import java.io.IOException;
// ...
public void addCommentToGifImage(File file, String comment)
throws IOException {
//read gif image from the file
GifImage gifImage = GifDecoder.decode(file);
// add comment
gifImage.addComment(comment);
// write commented gif image to the same file
GifEncoder.encode(gifImage, file);
}
Gif4J PRO Java Example: Double the total GIF animation time
import com.gif4j.GifImage;
import com.gif4j.GifFrame;
import java.util.Iterator;
// ...
GifFrame constructors can be divided into 2 types according to the desired frame position
(absolute or relative). "absolute positioning" costructors contain Point parameter and "relative
positioning" constructors contain layout constraint parameter.
Note: If you use "relative positioning" then the final position is calculated according to one of
the predifined layout constraint before the final encoding process starts.
image tour (size: 300x240, every frame is scaled down using ImageUtils to 150x130)
You can set disposal method through corresponding GifFrame constructors or using the next
method:
Delay Time - If not 0, this field specifies the number of hundredths (1/100) of a second to wait
before continuing with the processing of the Data Stream. The clock starts ticking immediately
after the graphic is rendered.
You can set delay time through corresponding GifFrame constructors or using the next method:
You can get frame's delay time using the next method:
Hint: You can use a copy of the frame's image color model to get frame's transparency
information through IndexColorModel methods: hasAlpha(), getTransparency(),
getTransparentPixel() etc.
Transform a GIF Image
The GifTransformer class is the helper java class containing static methods to affine transform
animated and non-animated GIF images represented by GifImage objects including resize, scale,
rotate and flip gif image transform operations. The next methods to transform gif images are
available (please consult the Gif4J PRO Java API for more info):
public static final GifImage rotate(GifImage gifImage, double theta, boolean smooth);
public static final GifImage rotate90Left(GifImage gifImage);
public static final GifImage rotate90Right(GifImage gifImage);
public static final GifImage rotate180(GifImage gifImage);
public static final GifImage scale(GifImage gifImage,
double xscale, double yscale, boolean smooth);
public static final GifImage resize(GifImage gifImage,
int width, int height, boolean smooth);
public static final GifImage flipHorizontal(GifImage gifImage);
public static final GifImage flipVertical(GifImage gifImage);
Note: smooth parameter (if presents) lets Java developers choose a gif image processing
algorithm: if 'true' then the higher priority is given to a gif image smoothness than processing
speed. But on the other hand it can bring about sizeable increase of the transformed GIF image
size.
// ...
Encode and Save a GIF Image
The GifEncoder java class lets programmers encode and save images out to a file or output
stream using the GIF file format (GIF89a version) with a single method call. The next methods
to encode and save images as GIFs are available (please consult the Gif4J PRO Java API for
more info):
The first three methods can be used to encode BufferedImage(s) directly to non-animated
(consists of one frame) gif image files. If the specified BufferedImage contains more than 256
unique colors then it'll be automatically quantized using the Gif4J Java Color Quantizer.
Parameter forceGlobalColorTableUsage forces Global Color Table use. If this parameter is set
to true then Local Color Tables from all frames will be union to one Global Color Table. It's
useful to optimize final image size (every Local Color Table takes up to 768 bytes). Especially
this option is recommended to set for encoding gif images with lots of internal frames (for
example, after applying Morphing Filters).
Gif4J PRO Java Example: Save Image as a GIF to a file
import com.gif4j.ImageUtils;
import com.gif4j.GifEncoder;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
// ...
import com.gif4j.GifEncoder;
import com.gif4j.GifFrame;
import com.gif4j.GifImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
// ...
To enable ImageIO support by the Gif4J PRO you just need to add library jar file to the
CLASSPATH. The Java Image I/O API should automatically resolve the Gif4J library as a plug-
in, load the Gif4J service provider classes, and registers the Gif4J service provider instance.
The next code checks that the Gif4J PRO has been resolved correctly:
If nothing has been found then you should call ImageIO.scanForPlugins() method to force the
ImageIO scans the application class path for plug-ins.
If the Gif4J service provider has been detected and loaded then an image Writing in the gif
format is equally simple:
BufferedImage bi = null;
// skip image initialization
try {
File f = new File("c:\\images\\myimage.gif");
ImageIO.write(bi, "gif", f);
} catch (IOException e) {
}
Construct a Watermark
The Watermark java class is used to prepare and apply (paint with the specified transparency)
images as watermarks to the whole gif images, separate gif image frames and BufferedImage(-
s). Absolute and relative (layout constraint based) positioning are supported.
The Watermark class constructors can be divided into 2 types according to the desired position
type (absolute or relative): "absolute positioning" costructors contain Point parameter and
"relative positioning" constructors contain layout constraint parameter.
Also you can specify watermark's transparency parameter which is represented by float value
between 0 (absolutely transparent) and 1 (absolutely opaque). Default is 0.5f.
(size: 150x150, 5 frames - each with different relative position and the last with
"cover consecutively" watermark)
Note: If you load GIF images for displaying purposes (for example to display as
image icons within a swing application) then you should use the standard Java API:
java.awt.Toolkit#getImage, java.awt.Toolkit#createImage or
javax.swing.ImageIcon.
import com.gif4j.Watermark;
import com.gif4j.TextPainter;
import java.awt.image.BufferedImage;
import java.awt.*;
import com.gif4j.Watermark;
import com.gif4j.TextPainter;
import com.gif4j.GifImage;
import java.awt.image.BufferedImage;
import java.awt.*;
(CellFilter) (CurtainFilter)
(MillFilter) (RadarFilter)
(MozaicFilter) (TunnelFilter)
(SnakeFilter)
(Cell and Curtain filters have been used)
Note: For every filter you can specify 'delay time between frames' parameter (in 1/100 seconds).
Default delay is equal to 10. To do a gif animation smoother please specify this parameter small
but note that some gif viewers and browsers (including Microsoft IE) can ignore very small
delay between frames. Experience has shown that, for example, Microsoft Internet Explorer
ignores delay between frames less than 6 and in this case uses own default delay. So if you want
to get the fastest gif animation then set the delay to 6.
Note: Morphing filtering is based on fragmentation one gif frame to several and it increases the
total number of gif frames greatly. As you know by default every output gif frame has own local
color table which can take up to 768 bytes in the result file. To prevent this process encode the
GifImage with "force global color table usage" option (see GifEncoder class API for more
information).
Text Rendering
The TextPainter java class is intended for drawing a text across the single or multiple lines on the
BufferedImage(s). It can be very convenient for adding some text information to images, creation
text-based watermarks etc.
The TextPainter class in its basis uses Java 2D lays and supports some Java 2D API concepts
such as Paint etc.
This java class actually represents a small superstructure above Java2D and uses only a small
part of Java2D functionality.
The TextPainter context contains the next attributes: font (instance of java.awt.Font),
foreground paint (instance of java.awt.Color, java.awt.GradientPaint or java.awt.TexturePaint),
backgroung color (instance of java.awt.Color), outline paint (instance of java.awt.Color,
java.awt.GradientPaint or java.awt.TexturePaint) and antialiasing (on/off).
You can get and set these attributes using the corresponding constructors and getter/setter
methods. There are 2 main rendering methods (please consult TextPainter Java API for more
options):
The first method renders single string using the specified attributes from the TextPainter context.
Size of the returned BufferedImage object is equal to visual bounds of the specified string to
render.
The second method renders line-wraped to the specified width text using the specified attributes
from the TextPainter context. Width of the returned BufferedImage object is equal to the
specified width. Height is automatically extended to contain all text lines. Below examples
demostrate some TextPainter features (see examples/TextRendering, examples/TextBanner,
examples/ProductIcon1 and examples/ProductIcon2):
Hint: To render outlined text with better quality use BOLD font instances.
Hint: It is no secret that any published on a site text information are not protected and can be
easily grabbed by special scripts or even by copy/paste. You can prevent the grabbing of any
privacy information (passwords, e-mails, price lists, financial reports etc.) by publishing
such information as gif images. With the help of the Gif4J PRO for Java such functionality can
be added easily for short time.
Note: Sometimes you can get ugly rendered text on the other platform while on your own
platform text is rendered well. Such problems are usually connected with failing one or another
font. You can get all supported fonts on the exact platform by calling getAllFonts() method of the
GraphicsEnvironment class:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allfonts = ge.getAllFonts();