GifUtil给gif图片添加文字、图片水印
GifUtil给gif图片添加文字、图片水印
gif4j给gif图片添加文字、图片水印
最近在做一个给gif图片添加图片水印的工作,在网上查了下好多童鞋推荐使用gif4j。在此将自己的使用过程在此做下笔记。
操作步骤
- 下载gif4j jar
官方下载地址:
http://www.gif4j.com/download.htm
由于该软件是非开软项目因此直接使用时会有gif4j相应的标记 - 代码【代码里边已经做了相应的注释大概应该可以看懂,在此就不做过多说明了】
package com.lenovo;
import java.awt.Color;import java.awt.Font;import java.awt.Point;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;
import javax.imageio.ImageIO;
import com.gif4j.GifDecoder;
import com.gif4j.GifEncoder;
import com.gif4j.GifImage;
import com.gif4j.GifTransformer;
import com.gif4j.TextPainter;
import com.gif4j.Watermark;/*** * @author liao**/
public class GifUtil {/*** 缩放gif图片** @param src* @param dest* @param width* @param height* @throws IOException*/public static void makeGif(File src, File dest, int width, int height)throws IOException {GifImage gifImage = GifDecoder.decode(src);// 创建一个GifImage对象.GifImage resizeIMG = GifTransformer.resize(gifImage, width, height,true);GifEncoder.encode(resizeIMG, dest);}public static void makeGif(String src, String dest, int width, int height)throws IOException {GifImage gifImage = GifDecoder.decode(new File(src));// 创建一个GifImage对象.makeGif(new File(src), new File(dest), gifImage.getScreenWidth() / 2,gifImage.getScreenHeight() / 2);}public static void makeGif(File src, File dest) throws IOException {GifImage gifImage = GifDecoder.decode(src);// 创建一个GifImage对象.makeGif(src, dest, gifImage.getScreenWidth() / 2, gifImage.getScreenHeight() / 2);}public static void makeGif(String src, String dest) throws IOException {makeGif(new File(src), new File(dest));}/*** 在图片中加文字水印* @param src* @param watermarkText* @param dest* @throws IOException*/public static void addTextWatermarkToGif(File src, String watermarkText, File dest)throws IOException {//水印初始化、设置(字体、样式、大小、颜色)TextPainter textPainter = new TextPainter(new Font("黑体", Font.ITALIC, 12));textPainter.setOutlinePaint(Color.WHITE);BufferedImage renderedWatermarkText = textPainter.renderString(watermarkText, true);//图片对象GifImage gf = GifDecoder.decode(src);//获取图片大小int iw = gf.getScreenWidth();int ih = gf.getScreenHeight();//获取水印大小int tw = renderedWatermarkText.getWidth();int th = renderedWatermarkText.getHeight();//水印位置Point p = new Point();p.x = iw - tw - 5;p.y = ih - th - 4;//加水印Watermark watermark = new Watermark(renderedWatermarkText, p);gf = watermark.apply(GifDecoder.decode(src), true);//输出GifEncoder.encode(gf, dest);}/*** 在图片中加图片水印* @param src* @param watermarkText* @param dest* @throws IOException*/public static void addImageWatermarkToGif(File src, String watermarkPath, File dest)throws IOException {BufferedImage renderedWatermarkText =ImageIO.read(new File(watermarkPath));//图片对象GifImage gf = GifDecoder.decode(src);//获取图片大小int iw = gf.getScreenWidth();int ih = gf.getScreenHeight();//获取水印大小int tw = renderedWatermarkText.getWidth();int th = renderedWatermarkText.getHeight();//水印位置Point p = new Point();p.x = 10;p.y = 20;//加水印Watermark watermark = new Watermark(renderedWatermarkText, p);//水印透明度watermark.setTransparency(1);gf = watermark.apply(GifDecoder.decode(src), false);//输出GifEncoder.encode(gf, dest);}public static void main(String[] arg) throws Exception{// GifUtil.addTextWatermarkToGif(new File("d:\\10.gif"), "@搞笑狂妞", new File("d:\\11.gif"));GifUtil.addImageWatermarkToGif(new File("d:\\gif\\10.gif"), "d:\\gif\\3.png", new File("d:\\gif\\4.gif"));}}