java gif 水印_JAVA利用Gif4j将GIF图片加水印

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 com.gif4j.GifDecoder;

import com.gif4j.GifEncoder;

import com.gif4j.GifImage;

import com.gif4j.GifTransformer;

import com.gif4j.TextPainter;

import com.gif4j.Watermark;

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

}

public static void main(String[] arg) throws Exception{

String path = "C:\\Documents and Settings\\Administrator\\桌面\\WB";

File dir = new File(path);

File[] fs = dir.listFiles();

File f = null;

String fn = null;

for(int i = 0; i < fs.length; i ++){

f = fs[i];

fn = f.getName();

if(f.isDirectory() || fn.substring(fn.lastIndexOf( ".")).toLowerCase().equals(".gif") == false){

continue;

}

System.out.println(path + "\\ok\\" + fn);

GifUtil.addTextWatermarkToGif(f, "@搞笑狂妞", new File(path + "\\OK\\" + f.getName()));

}

}

}

标签:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值