Dynamically generate pictures using Java Servlet

xiaoxiao2021-03-05  26

In web applications, you often need to dynamically generate pictures, such as real-time stock market markets, various statistics, etc. In this case, the picture can only be dynamically generated and sent to the user in server memory, and then displayed in the browser. Essentially, the browser requests a static image to the server as JPEG, the server returns to the standard HTTP response, but the HTTP header's ContentType is not text / html, but iMage / JPEG, so we will set it in servlet. Good contentType, then send the data stream of the image, the browser can correctly resolve and display the picture. In Java, java.awt and java.awt.Image package provide basic ability to draw images, we can draw a desired graphic in memory, then encode into JPEG or other image format, and finally send the corresponding to the browser can. Here is the detailed step of using the servlet to dynamically create an image: 1. Create a bufferedimage object that exists in memory and is responsible for saving the drawing image; 2. Create a Graphics2D object, which is responsible for drawing the desired image; 3. When the draw is completed, the JPEG encoder of the com.sun.Image.codec.jpeg package is called; 4. Finally, the encoded data is output to httpresponse. Note com.sun.mage.codec.jpeg In the RT.jar package of the JDK directory, it is not an open API, and Rt.jar needs to be copied to the web-inf / lib of the web application. Let's create a simpler servlet:

public class CreateImageServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "image / jpeg");}} We first set of contentType response to image / jpeg, so the browser Can be identified correctly. Then, create a BufferedImage object that is 100x100, ready to draw:

INT width = 100; int Height = 100; bufferedimage bi = new bufferedimage (width, height, bufferedimage.type_int_rgb), then get a Graphics2D object in the BufferedImage object and draw:

Graphics2d g = bi.creategraphics (); // Create a Graphics2D object // Pack background is white: g.setBackground (color.blue); g.clearRect (0, 0, width, height); // Setting prospects: g .SETCOLOR (Color.red); // Start Draw: G. Drawline (0, 0, 99, 99); // Draw a straight // Draw completion, release resources: g.dispose (); bi.flush () ; Then, JPEG encoding for BufferedImage:

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam (bi); param.setQuality (1.0f, false); encoder.setJPEGEncodeParam (param); try {encoder.encode (bi);} catch (IOException IOE) {IOE.PrintStackTrace ();} The encoded JPEG image is directly outputted in the OUT object, and we can only pass the response. getOutputStream () can be output directly to httpResponse. Here is a complete code: package com.crackj2ee.web.util; import java.io. *; Import java.awt. *; Import java.Awt.image. *; Import javax.servlet. *; Import javax.servlet. http *;. import com.sun.image.codec.jpeg *;. / *** @author Liao Xue Feng * / public class CreateImageServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response .setContentType ( "image / jpeg"); createImage (response.getOutputStream ());} private void createImage (OutputStream out) {int width = 100; int height = 100; BufferedImage bi = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2D g = bi.creategraphics (); // set background: g.setBackground (color.blue); g.clearRect (0, 0, width, height); // SET Fore Color: G.SetColor (Color .Red; // Start Draw: g.drawline (0, 0, 99, 199); // end draw: g.dispose (); bi.flush (); // eNCode: JPEGIMAGEENCODER Encoder = JPEGCODEC.CREATEJPEGENCODER ( OUT; JPEGENCODEPARAM PARAM = Encoder.GetDefau LTJPEGENCODEPARAM (BI); param.setquality (1.0f, false); Encoder.setjpegencodepAram (param); try {encoder.encode (bi);} catch (ooException ie) {ion (}});}}} Servlet is compiled, registered into web.xml, mapping path is / createImage, write a simple index.html test:

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

New Post(0)