• 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();两句话没有的话时会报错的。

  • 相关阅读:
    oracle学习6
    oracle学习5
    oracle学习4
    oracle学习3
    oracle的过滤与排序
    poj1064 Cable master
    poj3169 Layout
    UVA
    poj2341 Expedition
    poj3617 Best Cow Line
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100575.html
Copyright © 2020-2023  润新知