对图片添加文字的封装类

 

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下需要安装对应的字库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值