Home  >  Article  >  Backend Development  >  JSP Tips: Send Dynamic Images_PHP Tutorial

JSP Tips: Send Dynamic Images_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:46785browse

Send dynamically generated images to a page (or servlet)? This tip shows you how to do it. To run the code here, you will need a Tomcat or other web server that supports JSP 1.1.
When a web page is sent with the image/jpeg (or other image format) MIME type, your browser treats that return result as an image, and the browser displays the image as part of the page or Completely as an image in itself. To set the MIME type for your jsp page, you need to set the contentType attribute of the page:
 
Then you need to create a BufferedImage to draw your dynamic image:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
After creating a BufferedImage, you need to get the graphics environment for drawing, a Graphics or Graphics2D object:
Graphics g = image.getGraphics();
// or
Graphics2d g2d = image.createGraphics();
From now on you can draw the image content. Drawing to the graphics environment will draw to BufferedImage. Initially the image will be all black, so it's a good idea to fill the image with your desired background color. Then, when you're done drawing the image, you need to dispose the graphics environment:
g.dispose();
// or
g2d.dispose();

Once you finish drawing the image, you return that image in the response. You can encode images using the non-standard JPEGImageEncoder class from the com.sun.image.codec.jpeg package, or if you are using JDK1.4, you can use the standard ImageIO class. There is a trick when using JPEGImageEncoder. You must get the ServletOutputStream from ServletResponse and cannot use the 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 chosen from all possible solutions (such as g.dispose(); or g2d.dispose();). This example uses a Graphics object to draw a random Polygons and images are drawn through JPEGImageEncoder. You can freely set the number of vertices of the polygon to get a more complex shape, in other words, with more vertices and edges.
To run this example, put the jsp code between "" into a file called image.jsp. Put that file somewhere where your web server can find it. When using Tomcat In this case, it is the ROOT directory. Start Tomcat and visit http://localhost:8080/image.jsp
 <%@ page contentType="image/jpeg"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445187.htmlTechArticleSend dynamically generated images in the page (or servlet)? This tip tells you how to do it. To run the code here, you will need a Tomcat or other web server that supports JSP 1.1. When...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn