Java Generate image Verification code 2

Source: Internet
Author: User
Tags getcolor

Photo class

 Packagecom.baowei.utils;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;ImportJavax.servlet.http.HttpServletResponse; Public classPhoto {Private Static Final intwidth = 120; Private Static Final intHeight = 25; Privatehttpservletresponse response; PrivateString Randomdata = "";  PublicPhoto (httpservletresponse response) { This. Response =response; }     Public voidCreateImage ()throwsIOException {//1. Create a picture inside the memoryBufferedImage Bimage =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //2.1 Getting picturesGraphics g =Bimage.getgraphics (); //2.2 Setting the background color of the pictureSetBackground (g); //3. Set picture BorderSetBorder (g); //4. Set the image interference lineSetline (g); //5. Write data to picturesSetrandomdata (g); //6. Write the picture to the clientResponse.setcontenttype ("Image/jpeg"); Imageio.write (Bimage,"JPG", Response.getoutputstream ()); }     Public voidSetBackground (Graphics g) {color color=G.getcolor ();        G.setcolor (Color.White); G.fillrect (0, 0, width, height);    G.setcolor (color); }     Public voidSetBorder (Graphics g) {color color=G.getcolor ();        G.setcolor (Color.Blue); G.drawrect (1, 1, width-2, height-2);    G.setcolor (color); }     Public voidSetline (Graphics g) { for(inti = 0; I < 4; i++) {            //The x, y obtained here should be associated with the height of the image above            intX1 =NewRandom (). Nextint (width); intx2 =NewRandom (). Nextint (width); intY1 =NewRandom (). Nextint (height); inty2 =NewRandom (). Nextint (height); Color Color=G.getcolor ();            G.setcolor (Color.green);            G.drawline (x1, y1, x2, y2);        G.setcolor (color); }    }     Public voidSetrandomdata (Graphics g) {//get Random data//String data = "";         for(inti = 0; I < 4; i++) {Randomdata+=NewRandom (). Nextint (10) + ""; }        //set the font and color of your dataColor color =G.getcolor ();        G.setcolor (color.red); G.setfont (NewFont ("Arial", Font.Bold, 20)); g.DrawString (Randomdata,20, 20);    G.setcolor (color); }         Publicstring Getrandomdata () {string data[]= Randomdata.split (""); String Returndata= "";  for(inti = 0; i < data.length; i++) {Returndata+=Data[i]; }                returnReturndata; }}

DEMO01 servlet for test purposes

 PackageCom.baowei.servlet;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportCom.baowei.utils.Photo; Public classDemo01extendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request, response); }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); Response.setcontenttype ("Image/jpeg"); Photo PT=NewPhoto (response);        Pt.createimage ();        System.out.println (Pt.getrandomdata ()); String Data=Pt.getrandomdata (); //Deposit Session Sessions//HttpSession session = Request.getsession (true); //Session.setattribute ("Data", data);Getservletcontext (). SetAttribute ("Data"), data); }}

Java Generate image Verification code 2

Related Article

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.