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);//图标背景设置透名之后整张图片背景全是黑的,如果设置成别的颜色就只剩下图标背景颜色是设置的颜色,怎么修改才能让生成的整张图片背景是透明的?

解决方案 »

  1.   

    /**
     * 使图片中的某一种颜色透明
     * 
     * @param image Image
     * @param RGB16 String 十六进制的六位颜色值字符串
     * @param isFiltrate boolean
     * @return Image
     */
    public static Image setTranImage(Image image, String RGB16,
    boolean isFiltrate)
    {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    Image abufferedimage = new BufferedImage(width, height, 2);
    Graphics g = abufferedimage.getGraphics();
    g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);
    g.dispose();
    // 透明化处理
    PixelGrabber pgr = new PixelGrabber(abufferedimage, 0, 0, -1, -1, true);
    try
    {
    pgr.grabPixels();
    }
    catch (InterruptedException ex)
    {
    ex.getStackTrace();
    }
    int pixels[] = (int[]) pgr.getPixels();
    if (isFiltrate && RGB16.length() == 6)
    {
    // 循环像素
    for (int i = 0; i < pixels.length; i++)
    {
    // 去色
    if (((pixels[i] & 0x00ff0000) >> 16 == Integer.parseInt(
    RGB16.substring(0, 2), 16)
    && (pixels[i] & 0x0000ff00) >> 8 == Integer.parseInt(
    RGB16.substring(2, 4), 16) && (pixels[i] & 0x000000ff) == Integer
    .parseInt(RGB16.substring(4, 6), 16)))
    {
    // 透明化
    pixels[i] = 0;
    }
    }
    }
    ImageProducer ip = new MemoryImageSource(pgr.getWidth(),
    pgr.getHeight(), pixels, 0, pgr.getWidth()); return Toolkit.getDefaultToolkit().createImage(ip);
    }
      

  2.   

    private BufferedImage bi = setTranImage(bi, "c69155", true);
    g.drawImage(bib, 0, 30, 150, 120, this);