The first is to generate the verification code:
Package XX. Xx
Import java.io.*;
Import java.util.*;
Import javax.imageio.*;
Import java.awt.*;
Import java.awt.image.*;
public class imageensure ... {
Public Imageensure () ... {
}
Private char maptable[] = ... {' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
Public String getensure (int width, int height, outputstream os) ... {
if (width <= 0)
width = 60;
if (height <= 0)
height = 20;
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Get Graphics context
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCCCCC));
G.fillrect (0, 0, width, height);
Draw a border
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Take a randomly generated authentication code
String strensure = "";
4 represents 4-bit captcha
for (int i = 0; i < 4; ++i) ... {
Strensure + = maptable[(int) (Maptable.length * Math.random ())];
}
Display the authentication code in the image
G.setcolor (color.red);
G.setfont (New Font ("Atlantic Inline", Font.plain, 14));
The exact coordinates of the painting
String str = strensure.substring (0, 1);
g.DrawString (str, 8, 14);
str = strensure.substring (1, 2);
g.DrawString (str, 20, 15);
str = strensure.substring (2, 3);
g.DrawString (str, 35, 18);
str = strensure.substring (3, 4);
g.DrawString (str, 45, 15);
Releasing the graphics context
G.dispose ();
Try ... {
Output image to Page
Imageio.write (Image, "JPEG", OS);
catch (IOException e) ... {
Return "";
}
return strensure;
}
}