Ce servlet Java permet de créer des images en les encodant au format JPEG.
// JPEGenerator.java
// version 1.0 Björn Nilsson, june 2000
// version 1.1 Gustavo Muñoz, nov 2000
// jalajala@mail.ru
// Servlet Classes
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
// JPEG Codec Classes
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
// Graphics and Image Classes
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
// IO Classes
import java.io.IOException;
// Servlet that returns a JPEG to the Client
public class JPEGenerator extends HttpServlet {
// Handle HTTP GET
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
int ImageWidth = 300, ImageHeight = 200, shapeRadius, startAngle, stopAngle;
// Convert The Radius Parameter to an int
try {
this.shapeRadius = new Integer(req.getParameter("radius")).intValue();
this.startAngle = new Integer(req.getParameter("start")).intValue();
this.stopAngle = new Integer(req.getParameter("stop")).intValue();
}
catch (NumberFormatException exc){
this.shapeRadius = 0;
this.startAngle = 0;
this.stopAngle = 360;
}
// Create a JPEG Encoder
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(res.getOutputStream());
// Create a Buffered Image
BufferedImage img = new BufferedImage(this.ImageWidth, this.ImageHeight,
BufferedImage.TYPE_INT_RGB);
// Create a Graphics Object from the Buffered Image
Graphics2D graphics = img.createGraphics();
// Draw to The Circle
graphics.drawArc(this.ImageWidth/2-shapeRadius, this.ImageHeight/2-shapeRadius,
this.shapeRadius*2, this.shapeRadius*2, this.startAngle,
this.stopAngle-this.startAngle);
// Set MIME type and header
res.setContentType("image/jpeg");
res.setHeader("Content-disposition", "inline; filename=circle.jpg");
// Encode The Image and Write it to The Client
encoder.encode(img);
}
}
|