为什么我下载<高级图像处理图像I/O  API  RC  1.0 >处理GIF还是有问题,本来动态变成静态啦?JAVA还有好点的图像处理.要支持格式越多越好

解决方案 »

  1.   

    import java.io.*;
    import java.sql.*;
    import javax.naming.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.sql.*;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;import com.sun.image.codec.jpeg.*;
    import com.sun.image.codec.jpeg.JPEGCodec.*;/***
     *date:2008-5-12
     *author:East(张栋芳)
     **/public class ImageHandlerServlet extends HttpServlet{
    public Connection getConnection(){
    Connection conn=null;
    try{
    //搞连接
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs";
    conn=DriverManager.getConnection(url,"sa","");
    }catch(ClassNotFoundException ce){
    ce.printStackTrace();
    }catch(SQLException se){
    se.printStackTrace();
    }
    return conn;
    }
    public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    try{
       conn=getConnection();
       pstmt=conn.prepareStatement("select data from uploadfile where id=?");
       pstmt.setInt(1,5);
       rs=pstmt.executeQuery();
       if(rs.next()){
        InputStream is=rs.getBinaryStream(1);
        //通过JPEG搞JPEG数据流解码器
        JPEGImageDecoder jpegDecoder=JPEGCodec.createJPEGDecoder(is);
        //解码当前数据流,返回BufferedImage对像
        BufferedImage buffImg=jpegDecoder.decodeAsBufferedImage();
        //搞到Graphics对像,用于在BufferedImage对像绘制图和文字
        Graphics g=buffImg.getGraphics();
        //搞ImageIcon logo.gif作为水印图片
        ImageIcon imgicon=new ImageIcon("F:logo.gif");
        //得到image
        Image img=imgicon.getImage();
        //水印绘制到图片上
        g.drawImage(img,80,80,null);
        //设置图形上下文的当前Color为red
        g.setColor(Color.red);
        //搞字体
        Font font=new Font("Courier New",Font.BOLD,20);
        //设置图形上下文的字体为指定的字体
        g.setFont(font);
        //在图片上绘制文字
        g.drawString("http://East271536394.com",10,20);
        g.dispose();
        resp.setContentType("image/jpeg");
        ServletOutputStream sos=resp.getOutputStream();
        //创建jpeg图像编码器,用于编码内存中的图像数据到JPEG数据输出流
        JPEGImageEncoder jpegEncoder=JPEGCodec.createJPEGEncoder(sos);
        //编码BufferedImage对像到JPEG数据输出流
        jpegEncoder.encode(buffImg);
        is.close();
        sos.close();
       }
    }catch(Exception e){
    e.printStackTrace();
    }
    }