package lava.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
public class GIFUtil {
public static void main(String[] args) {
List<DrawTextBean> beans = new ArrayList<DrawTextBean>();
Font font=new Font("msyh",Font.PLAIN,12);
DrawTextBean bean1 = new DrawTextBean(103,113,"你好你好(12345)",Color.black,font);
DrawTextBean bean2 = new DrawTextBean(85,139,"123456",Color.black,font);
DrawTextBean bean3 = new DrawTextBean(270,138,"1231-14567889",Color.black,font);
beans.add(bean1);
beans.add(bean2);
beans.add(bean3);
GIFUtil.GifAddText("E:/Others/pic/buyself.gif","E:/Others/pic/test2.gif", beans );
}
public static void GifAddText(String srcImgPath,String destImgPath,List<DrawTextBean> beans) {
try {
File _file = new File(srcImgPath);
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);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
for(int i=0;i<beans.size();i++){
g.setColor(beans.get(i).getColor());
g.setFont(beans.get(i).getFont());
g.drawString(beans.get(i).getText(), beans.get(i).x,beans.get(i).y);
}
g.dispose();
GIFEncoder encode = new GIFEncoder(image);
OutputStream output = new BufferedOutputStream(
new FileOutputStream(destImgPath));
encode.Write(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package lava.util;
import java.awt.Color;
import java.awt.Font;
public class DrawTextBean {
public DrawTextBean(){
}
public DrawTextBean(int x,int y ,String text,Color color,Font font){
this.x=x;
this.y=y;
this.font=font;
this.text=text;
this.color = color;
}
int x;
int y;
Font font;
String text;
Color color;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
需要用到GIFEncoder,Linux下需要安装对应的字库