jsp中动态生成图像

源码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="image/jpeg"%>
< import="java.awt.image.BufferedImage"%>
< import="java.awt.Graphics"%>
< import="java.awt.Color"%>
< import="java.awt.Polygon"%>
< import="com.sun.image.codec.jpeg.JPEGCodec"%>
< import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>
<%
int width = 200,height = 200;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,width,height);
Polygon poly = new Polygon();
Random random = new Random();
for(int i=0;i<15;i++){
poly.addPoint(random.nextInt(width),random.nextInt(height));

}
g.setColor(Color.cyan);
g.fillPolygon(poly);
g.dispose();

ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);

response.flushBuffer();
sos.close();
out.clear();
out = pageContext.pushBody();
%>

如果out.clear();out = pageContext.pushBody();两句话没有的话时会报错的。

posted on 2010-07-12 22:06  画一个圆圈  阅读(285)  评论(0编辑  收藏  举报

导航