<% @ page language = "java" pageencoding = "GB2312" ContentType = "text / html; charset = GB2312" Import = "java.awt. *, java.awt.image. *, javax.swing. *, COM. Sun.Image.codec.jpeg. *, javax.imageio. * "%>
System.out.println ( "----------"); int width = 400, height = 400; BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics ();
G.SetColor (color.white); g.fillRect (0, 0, width, height);
G.SetColor (color.ble); g.drawstring ("Transport Usage", 10, 10);
G.SetColor (Color.red); // Plip the graphic G.FillRect (30, 30, 100, 20); g.SetColor (color.black); // Select Border G. DrawRect (30, 30, 100, 20); // Writing g.drawstring ("Aircraft", 135, 45);
G.SetColor (color.blue); G.FillRect (30, 55, 120, 20); g.SetColor (color.black); g.drawRect (30, 55, 120, 20); g.drawstring ("Train", 155, 70);
G.SetColor (Color.Yellow); G.FillRect (30, 80, 90, 20); g.SetColor (color.black); g.drawRect (30, 80, 90, 20); g.drawstring ("ship ship ", 125, 95); / ** This method can also be output, often using // in JDK1.3 to" Image / JPEG "to enable the browser to identify the image format. // Response.setContentType ("Image / JPEG"); // Establish a JPEG image encoder, encoding the bufferedImage into a JPEG format output to the RESPONSE output stream. // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (response.getOutputStream ()); // JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam (image); // param.setQuality (1.0F, true); // encoder.encode (image, param) ** / / ** starts in JDK1.4, introducing a new ImageIO library, providing a new tool class imageio to perform image output and output operations. Use ImageIO to perform image output, which makes the code more simple, as follows: ** // Turns the cache (the default is open). In practical applications, we found that if the cache is not closed, this program may not be operated normally on IE 5.5. // and IE5.0 and IE6.0 do not have problems. Imageio.setuseCache (false); // Writes an image to the output stream in JPG format. Imageio.write (Image, "JPG", response.getOutputStream (); / ** Get images from the database and add water in the above, usually we exist in the database in binary format. With Imageio's Read methods, we can directly generate the images in the database. // We assume that there is a MyImage field in the database table TabImage to store with pictures. PreparedStatement PS = conn.preparestatement ("SELECT MyImage from TBLImage WHERE I ID = 119); ResultSet RS = ps.executeQuery (); InputStream Data = NULL; if (rs.next ()) {// Number of input stream DATA = rs.getBinaryStream ("SIGNET");}} rs.close (); ps.close (); if (data == null) {conn.close (); return null;} imageio.setusecache (false); // Read the picture into the BufferedImage object. BufferedImage Image = Imageio.Read (); // Get the image of the image, graphics graphics = image.creategraphics () // Write text graphics.drawstring over the Graphics object ("Hello World ! ", 10, 10); the remaining code is to output the image, please refer to the previous example. Since this example involves the operation of the database, a complete example cannot be given.