JSP transmission dynamic picture

zhaozj2021-02-16  69

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:

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 ();

// OR

Graphics2d 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 ();

// 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 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 from "" to a file named image.jsp, put the file in the place where your web server can be found, in the case of using Tomcat, the root directory, Start Tomcat, visit http: // localhost: 8080 / image.jsp

<% @ Page ContentType = "Image / JPEG"

Import = "java.awt. *, java.awt.image. *,

com.sun.image.codec.jpeg. *, java.util. * "

%>

<%

// CREATE Image

INT width = 200, Height = 200;

BufferedImage Image = New BufferedImage (width, height, bufferedimage.type_int_rgb);

// Get Drawing Context

Graphics g = image.getgraphics ();

// Fill Background

g.setcolor (color.white);

g.fillRect (0, 0, Width, Height);

// Create Random Polygon

POLYGON POLY = New Polygon ();

Random Random = new random ();

For (int i = 0; i <5; i ) {

Poly.AddPoint (random.nextint (width),

Random.nextint (HEIGHT));

}

// Fill Polygon

g.setcolor (color.cyan);

g.FillPolygon (POLY);

// Dispose Context

g.dispose ();

// send Back Image

ServletOutputStream Sos = response.getOutputStream ();

JPEGIMAGEENCODER ENCODER =

JPEGCODEC.CREATEJPEGENCODER (SOS);

Encoder.encode (image);

%>

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

New Post(0)