Thursday, October 14, 2010

How to send a dynamic image JSP page



When a web page with an image / jpeg (or other image format) of the MIME type is sent, your browser will that return results as an image, then the browser to display images, as part of a page or completely as the image itself. Jsp page for you to set MIME types, you need to set page contentType attributes:



You need to create a BufferedImage and then draw your dynamic image:

BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);

After you finish creating a BufferedImage, you need to draw the graphic environment, a Graphics or Graphics2D object:

Graphics g = image.getGraphics ();

/ / Or

Graphics2d g2d = image.createGraphics ();

From now on you can draw the image content. Drawing on the graphical environment will be drawn to the BufferedImage. Initially the image is black, so you want to use the background color fills the image is a good idea, then, when you have finished rendering, you need to dispose graphical environment:

g.dispose ();

/ / Or

g2d.dispose ();

Once the image of the drawing, you return the images in response. You can use non-standard com.sun.image.codec.jpeg package JPEGImageEncoder class code image, or if you use JDK1.4, you can use the standard ImageIO class. JPEGImageEncoder when using a skill, you must take to ServletOutputStream from ServletResponse not use the JSP implicit output variable out.

ServletOutputStream sos = response.getOutputStream ();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (sos);

encoder.encode (image);

/ / Or

ImageIO.write (image, "JPEG", out);

There is a program from all possible (for example g.dispose (); or g2d.dispose ();) selected a complete sample. This example uses the Graphics object to draw a random polygon, image by JPEGImageEncoder drawing, you can free to set the polygon vertices more complex shapes, in other words, more vertices and edges.

To run this example, from the "" between the jsp code into a file named image.jsp, will that file into your web server can find the place where the use of Tomcat is the ROOT directory, start Tomcat, visit http://localhost:8080/image.jsp.







Recommended links:



Neusoft NetEye win opener



What is the JPEG2000



DivX to iPod



Directory Science Education



How to promote sales will not die



OPEN source model, the wonderful work of education



hotmail email reverse lookup easily catch a



Joint Commerce and Industry of India: India will rely heavily on computer hardware in China



No trace of her it just so to find The original 1000 Baidu



With A Good Wind Power, Send Me Albatron



Picked Audio Recorders



OGM to AVI



UML in practice, the current situation and some of the recommendations [2]



ASF to MPG



Simple Vehicles - Screen Savers



No comments:

Post a Comment