Send moving images via JSP

xiaoxiao2021-03-06  56

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. After you create a BufferedImage; BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB): To your jsp page set the MIME type, you need to set the contentType attribute of the page: Then you need to create a dynamic image BufferedImage draw your You need to get a graphics environment to draw, a graphics or graphics2d object: graphics g = image.getgraphics (); // or graphics2d g2d = image.creategraphics (); From now you can draw the image content. Drawing a graphic environment will draw bufferedimage. The first 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 (); // OR G2D. 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); // or ImageIO.write (image, "JPEG", out); Here's a possible solution from all A complete example of (for example, g.dispose (); or g2d.dispose (); this example draws a random polygon with the Graphics object, the image is drawn through JPEGIMAGEENCODER, you can freely set the number of vertexes to get more Complex shapes, in other words, there are more vertices and edges.

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

New Post(0)