Java Verification Code Picture

Source: Internet
Author: User

Package Com.su.servlet;import Java.awt.color;import Java.awt.font;import java.awt.graphics;import Java.awt.graphics2d;import Java.awt.image.bufferedimage;import Java.io.ioexception;import java.util.ArrayList; Import Java.util.list;import java.util.random;import javax.imageio.imageio;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Checkcode extends httpservlet{/** * */private static final long Seri Alversionuid = 1l;public static final int width = 120;//The width of the generated picture public static final int height = 30;//the height of the resulting picture @overrideprote CTED void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doPost (Request, response);} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception,    IOException {request.setcharacterencoding ("utf-8"); Create a picture in memory bufferedimage bi = NEW bufferedimage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb);       Get picture Graphics graphics = Bi.getgraphics (); Set the background color of the picture Graphics.setcolor (Color.White); graphics.fillrect (0, 0, width, width);//Set the border of the picture Graphics.setcolor ( color.red); Graphics.drawrect (1, 1, WIDTH-2, HEIGHT-2); Color[] Color={color.black,color.blue,color.cyan,color.green,//define the color array color.orange,color.yellow,color.red, color.pink,color.light_gray};//picture of the interference line Graphics.setcolor (color.blue);//Set the number of lines and draw lines for (int i=0;i<10;i++) { Graphics.setcolor (Color[new random (). Nextint (9)]); int x1 = new Random (). Nextint (WIDTH); int x2 = new Random (). Nextint ( WIDTH); int y1 = new Random (). Nextint (height); int y2 = new Random (). Nextint (HEIGHT); Graphics.drawline (x1, y1, x2, y2);} StringBuilder sb = new StringBuilder ();//write the random number on the image Graphics.setcolor (Color.gray); Graphics.setfont ("Italic", Font.Bold, 20)); Graphics2D g = (graphics2d) graphics; Control word String str = "0123456789ABCDEFGHIGKLMNOPQRSTUVWRST";  int x = 5;   for (int i = 0; i < 4; i++) {       Set font rotation angle int degree = new Random (). Nextint ()% 30;          char ch = str.charat (new Random (). Nextint (36));          Sb.append (string.valueof (ch));          Positive angle g.rotate (degree * math.pi/180, X, 20);         Graphics.DrawString (string.valueof (CH), X, 20);          Reverse Angle g.rotate (-degree * math.pi/180, X, 20);        x + = 30; }//7. The random number exists in the session request.getsession (). SetAttribute ("Checkcode", sb.tostring ());//8. Set the response header to notify the browser to open Response.setcontenttype ("Image/jpeg") as a picture;//9. Setting the response header control browser do not cache Response.setdateheader ("Expries",-1) ; Response.setheader ("Cache-control", "No-cache"), Response.setheader ("Pragma", "No-cache");//10. Write the picture to the browser imageio.write (bi, "JPG", Response.getoutputstream ());}}

Java Verification Code Picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.