Image verification code generation

xiaoxiao2021-03-06  41

1 package util;

2

3 Import javax.imageio.imageio;

4 Import Javax.Servlet.ServletException;

5 Import javax.servlet.http.httpservlet;

6 Import Javax.Servlet.http.httpservletRequest;

7 Import javax.servlet.http.httpservletResponse;

8 Import java.awt. *;

9 Import Java.awt.Image.BufferedImage;

10 Import java.io.ioException;

11 Import Java.util.random;

12

13

14 / **

15 * Description:

16 *

17 * @Author $ Author $

18 * @version $ revision $

19 * /

20 Public Class Piccheckcode Extends httpservlet {

twenty one

22 private font mfont = new font ("arial black", font.plain, 15); // set font

23 private int lineWidth = 2; // Distress line length = 1.414 * LineWidth

24 private int width = 60; // Define graphical size

25 private int Height = 20; // Define graphical size

26 private int count = 200;

27

28 / **

29 * Description:

30 *

31 * @Param FC description:

32 * @Param BC Description:

33 *

34 * @return description:

35 * /

36 Private Color GetrandColor (int FC, int BC) {// get a given range random color

37

38 Random Random = new random ();

39

40 IF (fc> 255) {

41

42 fc = 255;

43}

44

45 IF (BC> 255) {

46

47 bc = 255;

48}

49

50 int R = fc random.nextINT (BC - FC);

51 INT g = fc random.nextint (BC - FC);

52 int b = fc random.nextint (BC - FC);

53

54 RETURN New Color (R, G, B);

55}

56

57 // Processing POST

58 Public Void Dopost (httpservletRequest Request, httpservletResponse response)

59 THROWS servletexception, ioException {

60

61 Doget (Request, Response); 62}

63

64 / **

65 * Description:

66 *

67 * @Param Request Description:

68 * @Param Response Description:

69 *

70 * @Throws ServletException Description:

71 * @Throws IOException Description:

72 * /

73 public void doget (httpservletRequest Request, HTTPSERVLETRESPONSE RESPONSE)

74 THROWS servletexception, ioException {

75

76 //Response.reset ();

77 // Settings page does not cache

78 Response.setheader ("Pragma", "No-Cache");

79 Response.setheader ("Cache-Control", "No-Cache");

80 response.SetDateHeader ("Expires", 0);

81 Response.setContentType ("Image / GIF");

82

83 // Create image in memory

84 BufferedImage Image = New BufferedImage (Width, Height,

85 BufferedImage.Type_Int_RGB);

86

87 // Get the graphical context

88 graphics2d g = (graphics2d) image.getgraphics ();

89

90 // Generate random class

91 Random Random = new random ();

92

93 // Setting the background color

94 G.SetColor (Getrandcolor (200, 250)); // --- 1

95

96 g.fillRect (0, 0, width, height);

97

98 // Setting the font

99 G.SetFont (mfont);

100

101 // Sea Box

102 G.SetColor (GetrandColor (0, 20)); // --- 2

103 g.drawRect (0, 0, Width - 1, HEIGHT - 1);

104

105 // randomly generates the interference line, causing the authentication code in the image to be probed by other programs

106 for (int i = 0; i

107

108 G.SetColor (GetrandColor (150, 200)); // --- 3

109

110 int x = random.nextint (width - linewidth - 1) 1; // guaranteed to draw within the border

111 int y = random.nextint (HEIGHT - LINEWIDTH - 1) 1; 112 INT XL = Random.Nextint (LineWIDTH);

113 int yl = random.nextint (LINEWIDTH);

114 g.drawline (X, Y, X XL, Y YL);

115}

116

117 // Take the randomly generated authentication code (4 digits)

118 string srand = ""

119

120 for (int i = 0; i <4; i ) {

121

122 string rand = string.valueof (random.nextint (10));

123 SRAND = RAND;

124

125 // Display the authentication code to the image, the color of the call function is the same, may be because the seed is too close, so only directly generate

126 G.SetColor (New Color (20 random.nextint (130),

127 20 random.nextint (130), 20 random.nextint (130))); // - 4--50-100

128

129 g.drawstring (rand, (13 * i) 6, 16);

130

131

132}

133

134 // Deposit the authentication code into the session

135 Request.getSession () .SetAttribute ("getimg", srand;

136

137 // Image takes effect

138 g.dispose ();

139

140 // Output image to page

141 Imageio.write (Image, "PNG", response.getOutputStream ());

142}

143} - How to use it in jsp? Assume the servlet name is piccheckcodeservlet

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

New Post(0)