首页 > 开发 > 综合 > 正文

Servlet动态产生JPEG图像的例子

2024-07-21 02:14:31
字体:
来源:转载
供稿:网友

//generateimage.java    
 /* 动态产生jpeg图像的例子   */
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;  //编码类
java.util.*;
javax.servlet.*;
javax.servlet.http.*;
java.io.*;


class generateimage extends httpservlet
public void doget(httpservletrequest request,httpservletresponse response)
               throws servletexception, java.io.ioexception {

 response.setcontenttype("image/jpeg"):
 int width=200, height=200;
 bufferedimage image = bufferedimage(width,height,bufferedimage.type_int_rgb);
 graphics g = image.getgraphics();
 g.setcolor(color.white);
 g.fillrect(0,0,width,heitht);
 polygon poly = new polygon();
 random random = new random();
 for(int i=0;i<5;i++)
 {
 poly.addpoint(random.nextint(width),random.nextint(height));
 }
  g.setcolor(color.cyan);
  g.fillpolygon(poly);
  g.dispose(); //消拙graphics 对图形没有肖毁
 
  servletoutputstream sos =response.getoutputstream();  //二进制
  jpegimageencoder encoder = jpegcodec.createjpegencoder(sos); //创建编码器,输出到sos那里
  encoder.encode(image);
  }

  public void dopost(httpservletrequest request,httpservletresponse response)
       throws servletexception, java.io.ioexception {
   doget(request,response);
  }

}

http://blog.csdn.net/laiahu/archive/2007/01/23/1491681.aspx

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表