<% @ 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 .*"
%>
<HTML>
<Head>
<Title> my pictures </title>
</Head>
<Body>
<%
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. Black );
G. drawstring ("vehicle usage rate", 10, 10 );
G. setcolor (color. Red );
// Fill the image
G. fillrect (30,30, 100,20 );
G. setcolor (color. Black );
// Draw a border
G. drawrect (30,30, 100,20 );
// Write
G. drawstring ("airplane );
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 );
/**
This method can also be output, which is often used in jdk1.3.
// Set contenttype to "image/JPEG" for the browser to recognize the image format.
// Response. setcontenttype ("image/JPEG ");
// Create a JPEG image encoder to encode bufferedimage into JPEG format and output it to the response output stream.
// Required imageencoder encoder = Response codec. createjpegencoder (response. getoutputstream ());
// Export encodeparam Param = encoder. getdefaultjpegencodeparam (image );
// Param. setquality (1.0f, true );
// Encoder. encode (image, Param );
**/
/**
In jdk1.4, a new ImageIO library was introduced to provide a new tool class ImageIO for image output and output operations. Using ImageIO to output images makes the code more concise, as shown below:
**/
// Disable the cache (enabled by default ). In practical applications, we find that,
// If the cache is not disabled, the program may not run properly on ie5.5.
// Ie5.0 and ie6.0 are not problematic.
ImageIO. setusecache (false );
// Write the image in JPG format to the output stream.
ImageIO. Write (image, "jpg", response. getoutputstream ());
/**
Obtain the image from the database and add the watermark to it.
Generally, images are stored in the database in binary format. With the read method of ImageIO, we can directly generate bufferedimage for images in the database.
// Here we suppose there is a myimage field in the database table tabimage that stores images.
Preparedstatement PS = conn. preparestatement ("select myimage from tblimage where id = 119 ");
Resultset rs = ps.exe cutequery ();
Inputstream DATA = NULL;
If (Rs. Next ())
{
// Obtain the input stream of the image
Data = Rs. getbinarystream ("signet ");
}
Rs. Close ();
PS. Close ();
If (Data = NULL ){
Conn. Close ();
Return NULL;
}
ImageIO. setusecache (false );
// Read the image into the bufferedimage object.
Bufferedimage image = ImageIO. Read (data );
Conn. Close ();
// Obtain the graphics object of the image,
Graphics graphics = image. creategraphics ()
// Write text on the image through the graphics object
Graphics. drawstring ("Hello world! ", 10, 10 );
The remaining code is to output the image. See the previous example. As this example involves database operations, a complete example cannot be provided.
Create a thumbnail
On many online shopping websites, the product introduction images are usually displayed in the product catalog list as thumbnails. After you click the thumbnail, the product details and original images are displayed.
The so-called thumbnail is to reduce the source image to a certain proportion. Similarly, with ImageIO, we first read an image from a database (or file) (refer to the above example ),
// Generate a thumbnail
// Read the image object of the original image
Inputstream = new fileinputstream ("E: // tomcat5 // webapps // testset // web.gif ");
Bufferedimage src = ImageIO. Read (inputstream );
// Obtain the width and height of the source image.
Int Wideth = SRC. getwidth ();
Int Height = SRC. getheight ();
// Generate a memory image of 1/2 size for the source image. You can also specify a fixed size.
Bufferedimage taget = new bufferedimage (Wideth/2, height/2, bufferedimage. type_int_rgb );
// Draw the source image on the reduced memory image
Taget. getgraphics (). drawimage (SRC, 0,0, Wideth/2, height/2, null );
ImageIO. setusecache (false );
ImageIO. Write (taget, "jpg", response. getoutputstream ());
%>
</Body>
</Html>