 |
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.
Below examples demostrate positioning and transparency opportunities (see examples/Watermarks and
examples/ImageTour1):

(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.
Apply a Watermark to a BufferedImage
Below example demostrates creating and adding a text watermark to a BufferedImage:
Gif4J PRO Java Example: Add text watermark to a BufferedImage
import com.gif4j.Watermark;
import com.gif4j.TextPainter;
import java.awt.image.BufferedImage;
import java.awt.*;
public BufferedImage addTextWatermarkToImage(BufferedImage image, String text) {
//create new TextPainter
TextPainter textPainter = new TextPainter(new Font("Verdana", Font.BOLD, 10));
textPainter.setOutlinePaint(Color.WHITE);
//render the specified text outlined
BufferedImage renderedWatermarkText = textPainter.renderString(text,true);
//create new Watermark
Watermark watermark =
new Watermark(renderedWatermarkText, Watermark.LAYOUT_BOTTOM_RIGHT);
//apply watermark to the specified image and return the result
return watermark.apply(image);
}
Apply a Watermark to a GIF image
Below example demostrates creating and adding a text watermark to a GifImage:
Gif4J PRO Java Example: Add text watermark to a GifImage
import com.gif4j.Watermark;
import com.gif4j.TextPainter;
import com.gif4j.GifImage;
import java.awt.image.BufferedImage;
import java.awt.*;
public GifImage addTextWatermarkToGifImage(GifImage gifImage, String text) {
//create new TextPainter
TextPainter textPainter = new TextPainter(new Font("Verdana", Font.BOLD, 10));
textPainter.setOutlinePaint(Color.WHITE);
//render the specified text outlined
BufferedImage renderedWatermarkText = textPainter.renderString(text, true);
//create new Watermark
Watermark watermark =
new Watermark(renderedWatermarkText, Watermark.LAYOUT_BOTTOM_RIGHT);
//apply watermark to the specified gif image and return the result
return watermark.apply(gifImage, true);
}
| Watermarked GIF Images (the test image is out logo) |
|
|
|
[LAYOUT_TOP_LEFT, 0.7f transparency]
|
[LAYOUT_TOP_CENTER, 0.7f transparency]
|
|
|
|
[LAYOUT_TOP_RIGHT, 0.7f transparency]
|
[LAYOUT_MIDDLE_LEFT, 0.7f transparency]
|
|
|
|
[LAYOUT_MIDDLE_CENTER, 0.7f transparency]
|
[LAYOUT_MIDDLE_RIGHT, 0.7f transparency]
|
|
|
|
[LAYOUT_BOTTOM_LEFT, 0.7f transparency]
|
[LAYOUT_BOTTOM_CENTER, 0.7f transparency]
|
|
|
|
[LAYOUT_BOTTOM_RIGHT, 0.7f transparency]
|
[LAYOUT_COVER_CONSECUTIVELY, 0.3f transparency]
|
Apply a Watermark to a separate GIF Image Frame
To apply watermark(s) to the specified frame you should add this frame to a gif image using the next methods:
public GifImage addGifFrame(GifFrame frame, Watermark watermark)
public GifImage addGifFrame(GifFrame frame, MorphingFilter filter, Watermark watermark)

(size: 160x120, animated gif image tour with separately watermarked frames)
|  |