java gif图片_java 如何生成动态的gif图片

这篇博客介绍了一个使用Java实现的将动态GIF图片与静态图片合成,并在目标图片上添加水印的方法。通过 AnimatedGifEncoder 类,可以控制动态GIF的帧数、质量和延迟时间,实现动态图标与水印的完美融合。
摘要由CSDN通过智能技术生成

网上找了很多资料都没有生成gif图片的例子。但是生成水印的文件到不少,如果我们把gif图片合成水印后,图片就不动了,所以我写了个小例子供大家参考。(如果你想在某个图片上打印个动感图标,这个类就能很好地实现,前提是你必须先将动感图标分解成几张单独的gif图片。当然你可以用程序来实现。)

要用到  AnimatedGifEncoder 类自行下载

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

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

//参数列表:输出图片地址,加水印的图片地址,动态图标地址,纵坐标,横坐标

//注意:此 e:/pic/ 目录下的文件如下 1.gif 2.gif 3.gif 。。。。

}

public void CreateGIF(String outputFileName,String path1,String path2,int height,int weidth){

try {

// 指定Frame的文件

AnimatedGifEncoder e = new AnimatedGifEncoder();

OutputStream os = new FileOutputStream(outputFileName); //输出图片

e.start(os);// 开始处理

e.setQuality(15); //设置图片质量

e.setRepeat(0);  //设置循环

e.setDelay(500); // 设置延迟时间

MakeOver abc = new MakeOver();  //实例化图片合成类

String path3 = "";     //动态图片地址

for (int i = 1; i < 3; i++) {  //此处只添加 2 张gif图片

path3 = path2 + i +".gif";

BufferedImage im = abc.pressImage(path3,path1, weidth, height);

e.addFrame(im);// 循环加入Frame

}

e.finish();

} catch (Exception e) {

System.out.println(e);

e.printStackTrace();

}

}

public  BufferedImage pressImage(String pressImg,

String targetImg, int x, int y) {

try {

// 目标文件

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

// 水印文件

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

// 水印文件结束

g.dispose();

return image;

} catch (Exception e) {

System.out.println(e);

e.printStackTrace();

}

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值