AnimatedGifEncoder 生成的图片背景颜色与图标背景颜色如何设置透明
zxred 2011-01-25 03:32:33 package cn.isty.browser.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.AnimatedGifEncoder;
public class GifServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static AnimatedGifEncoder ag = null;
private static String FILE_URL = "images/004.png";
public static int IMG_WIDTH = 250; //设置生成图片的宽度
public static int IMG_HEIGHT = 300; //设置生成图片的高度
public static int MAIN_IMG_WIDTH = 88; //图标宽
public static int MAIN_IMG_HEIGHT = 60; //图标高
public static int ICOX =(IMG_WIDTH / 2) - (MAIN_IMG_WIDTH / 2);//图标的x位置
public static int ICOY =50; //图标的y 位置
public static BufferedImage mainImage = null;
public static String TITLE = "今天晴转多云10-20C.";
public static Color BG_COLOR = new Color(Color.TRANSLUCENT);
public static int FONT_SIZE = 14;
public static int FONT_X=5;
public static int FONT_Y =150;
public static double TITLE_WIDTH = 100;
public static float RUN_SIZE =10;
public static String FONT_FAMILY ="楷体";
public static String FONT_COLOR="FF0000";
public static String direction ="right";
public static int rate = 1000;
Font font = null;
public static int X=0;
public static Graphics2D g = null;
public void init() throws ServletException {
try {
mainImage = ImageIO.read(new File(this.getServletContext().getRealPath(FILE_URL)));
MAIN_IMG_WIDTH = mainImage.getWidth();
MAIN_IMG_HEIGHT = mainImage.getHeight();
RUN_SIZE = (FONT_SIZE + 0F) / 2;
TITLE_WIDTH = RUN_SIZE * TITLE.getBytes().length;
if(ICOX < 0 || ICOX > IMG_WIDTH )
ICOX = (IMG_WIDTH / 2) - (MAIN_IMG_WIDTH / 2);
if(ICOY <0 || ICOY >IMG_HEIGHT)
ICOY = 50;
} catch (IOException e) {
e.printStackTrace();
}
}
public void service(HttpServletRequest request, HttpServletResponse response) {
try {
getImage(TITLE, FONT_X, FONT_Y,response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public void getImage(String title, int x, int y,OutputStream out) {
ag = new AnimatedGifEncoder();
ag.setTransparent(new Color(Color.TRANSLUCENT));//设置背景透明
ag.start(out);
ag.setDelay(rate);//设置速度
X = x;
int img =IMG_WIDTH;
for(int i = 0; X < img; i++) {
font = new Font(FONT_FAMILY,Font.BOLD,FONT_SIZE);
BufferedImage imgTmep = this.getTransparentImg();
g.setColor(new Color(Integer.parseInt(FONT_COLOR,16)));
g.setFont(font);
if(direction.equals("right") || direction.equals("down"))
X = (int)(X+i);
if(direction.equals("left") || direction.equals("up"))
X= (int)(X-i);
if(X < 0){
X =img;
}
if(direction.equals("right")){
g.drawString(title, X, y);
if(X - IMG_WIDTH + TITLE_WIDTH > 0){
g.drawString(title, X - IMG_WIDTH, y);
}
}
ag.addFrame(imgTmep);
}
ag.finish();
}
private static BufferedImage getTransparentImg() {
BufferedImage bi = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
BufferedImage.TYPE_4BYTE_ABGR);
g = (Graphics2D)bi.getGraphics();
g.draw3DRect(0, 0, IMG_WIDTH, IMG_HEIGHT, true);
g.drawImage(mainImage, ICOX, ICOY, MAIN_IMG_WIDTH, MAIN_IMG_HEIGHT,new Color(Color.TRANSLUCENT), null);//设置图标
return bi;
}
}
各位大哥大姐快快帮我解决这个问题,先谢谢各位了!
我要动态生成一下透明的gif图片,我把ag = new AnimatedGifEncoder();ag.setTransparent(new Color (Color.TRANSLUCENT)); 设置为透明,把 g.drawImage(mainImage, ICOX, ICOY, MAIN_IMG_WIDTH, MAIN_IMG_HEIGHT,new Color(Color.TRANSLUCENT), null);//图标背景设置透名之后整张图片背景全是黑的,如果设置成别的颜色就只剩下图标背景颜色是设置的颜色,怎么修改才能让生成的整张图片背景是透明的?