Java Implementation Verification code function--turn from the eyes

Source: Internet
Author: User
Tags rand

Tag:inline   font   oid    copy    new    puts   rip    width height    draw   

Package Com.verification_code.servlet;import Java.awt.color;import Java.awt.font;import java.awt.Graphics;import Java.awt.image.bufferedimage;import Java.io.ioexception;import Java.io.outputstream;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 ImgServlet Extends httpservlet{@Override protected void doget (HttpServletRequest req, HttpServletResponse resp) thr    oWS servletexception, IOException {//TODO auto-generated Method Stub doPost (REQ,RESP); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexcepti On, IOException {//TODO auto-generated Method Stub//Set the response at the beginning Resp.setcontenttype ("Image/jpeg        ");        The settings page does not cache Resp.setheader ("Pragma", "no-cache"); Resp.setheader("Cache-control", "No-cache");        Resp.setdateheader ("Expires", 0);        OutputStream out = Resp.getoutputstream ();        Sets the width height int width=88,height=20 of the picture;        BufferedImage image = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);        Graphics g = image.getgraphics ();        Generate Random class randomly random = new random ();        G.setcolor (Color.White);        G.fillrect (0, 0, width, height);        Set the font g.setfont (new Font ("Times New Roman", font.bold+font.italic,18)); Draw Border G.setcolor (Getrandcolor (0, 20));        ---2 g.drawrect (0, 0, width-1, height-1);        Randomly generates an interference line int count = 100; int linewidth = 3; Width of the interference line for (int i = 0; i < count; i++) {G.setcolor (Getrandcolor);//---3 in T x = Random.nextint (width-linewidth-1) + 1;            Make sure to draw within the bounding rectangle int y = random.nextint (height-linewidth-1) + 1;            int xl = Random.nextint (linewidth); int YL = Random.nextint (linewidth);        G.drawline (x, y, X + xl, y + yl);        }//Generate random verification code String srand= ""; String str[]={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",        "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};            for (int i=0;i<4;i++) {int rand = Random.nextint (36);            Srand+=str[rand];            Set the random color G.setcolor (new color (20+random.nextint, 20+random.nextint, 20+random.nextint (110)));        Draw g.DrawString (Str[rand], 20*i+8, 16);        } req.getsession (). SetAttribute ("Verification_code", SRand);        G.dispose ();    Imageio.write (Image, "JPEG", out);        } public color Getrandcolor (int fc, int BC) {//Get a random color for a given range randomly random = new random ();        if (FC > 255) {fc = 255;        } if (BC > 255) {BC = 255;        } int r = FC + Random.nextint (BC-FC);int g = FC + Random.nextint (BC-FC);        int B = FC + Random.nextint (BC-FC);    return new Color (R, G, b); }}

Page

<script type= "Text/javascript" >    function Refresh () {        document.getElementById ("Id_code"). src= " Image? " +math.random ();        loginform.code.src= "image?" +math.random ();    }
</script>

Java Implementation Verification code function--turn from the eyes

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.