JSP Tips: Send Dynamic Images

xiaoxiao2021-03-05  23

Have you ever thought about sending dynamically generated images from the JSP page (or servlet)? This skill tells you how to do it. To run the code here, you need a Tomcat or other WEB server that supports JSP 1.1. When a web page with image / jpeg (or other image format) is sent, your browser will use the return result as an image, then the browser displays the image, as part of the page or is completely image itself. To set the MIME type for your JSP page, you need to set the contentType property of the page:

<% @ Page ContentType = "Image / JPEG" ...%> Then you need to create a bufferedimage to draw your moving image: BufferedImage Image = New BufferedImage (Width, Height, BufferedImage.Type_INT_RGB);

After you have created a bufferedimage, you need to get a graphics environment to draw, a graphics or graphics2d object:

Graphics g = image.getgraphics (); // Orgraphics2D g2d = image.creategraphics ();

You can draw the image content from now. Drawing a graphic environment will draw bufferedimage. The most beginning this image is black, so the image of the color fill image you want is a good idea, then, when you complete the image drawing, you need a Dispose graphic environment:

g.dispose (); // ORG2D.Dispose ();

Once the image is completed, you return to That image in Response. You can use the JPEGImageEncoder class encoded image in a non-standard com.sun.image.codec.jpeg package, or if you use JDK1.4, you can use the standard ImageIO class. There is a trick when using JPEGIMAGEENCoder, you must take ServletOutputStream from ServletResponse without using an implicit JSP output variable OUT.

ServletOutputStream Sos = response.getOutputStream (); JPEGIMAGEENCODER Encoder = JPEGCODEC.CREATEJPEGENCODER (SOS); Encoder.Encode (Image); // OrimageIo.write (Image, "JPEG", OUT);

Here is a complete example from all possible schemes (such as g.dispose (); or g2d.dispose ();) selects a random polygon with Graphics objects, images drawn through JPEGIMAGEENCODER, you can Freely set the number of vertices of polygons to get more complex shapes, in other words, there are more vertices and edges. To run this example, place the JSP code between "<% @" to the last "%>" in a file called Image.jsp, put that file to your web server can find, In the case of using Tomcat, the root directory is started, start Tomcat, access http: // localhost: 8080 / image.jsp. <% @ Page contenttype = "image / jpeg" import = "java.Awt. *, Java.awt. Image. *, com.sun.image.codec.jpeg. *, java.util. * "%> <% // CREATE IMAGEINT WIDTH = 200, Height = 200; bufferedimage Image = New BufferedImage (Width, Height, BufferedImage. TYPE_INT_RGB); // Get drawing contextGraphics g = image.getGraphics (); // Fill backgroundg.setColor (Color.white); g.fillRect (0, 0, width, height); // Create random polygonPolygon poly = new Polygon (); Random random = new random (); for (int i = 0; i <5; i ) {poly.addpoint (random.nextint (width), random.nextint (heiGHT));} // Fill Polygon. setColor (Color.cyan); g.fillPolygon (poly); // Dispose contextg.dispose (); // Send back imageServletOutputStream sos = response.getOutputStream (); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (sos); encoder.encode ( iMage);%>

转载请注明原文地址:https://www.9cbs.com/read-33062.html

New Post(0)