JSP random verification picture How to make _jsp programming

Source: Internet
Author: User
Tags int size set background sin

Small series of an article on The imitation of Tencent random verification pictures. Let's go directly to the code below! A Java class and a JSP page.

Java Class Code:

The following is a JSP imitate Tencent random validation picture quote fragment:

Package icewee.image;
Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import java.awt.RenderingHints;
Import Java.awt.geom.Line2D;
Import Java.awt.image.BufferedImage;

Import Java.util.Random;
  public class Tokenutil {private static final String base = "23456789ABCDEFGHJKLMNPQRSTUVWXYZABCDEFHIJKLMNPQRSTUVWXYZ";

  private static final int length = Base.length (); public static bufferedimage createtokenimage (int width, int height, String randomcode) {BufferedImage image = N
    EW bufferedimage (width, height, bufferedimage.type_int_rgb);
    Graphics2D g = image.creategraphics ();
    Random Random = new Random (); G.setcolor (New Color (230, 230, 250)); Set background color g.fillrect (0, 0, width, height);
    Fill background g.setcolor (color.black);
    Draw Edge G.drawrect ( -1,-1, Width + 1, height + 1);
    G.setcolor (Color.gray);
  Sets the font, randomly selects the font, tentatively sets 8 fonts font font = new Font (new string[] {"Arial", "Arial Black",      "Arial Italic", "Courier new", "Courier new Bold Italic", "Courier New Italic", "Franklin Gothic Medium",
    "Franklin Gothic Medium Italic"}) [Random.nextint (8)], Font.plain, 30);
    G.setstroke (new Basicstroke (float) (Math.random ()),//Basicstroke.cap_butt, basicstroke.join_bevel));
    G.setfont (font);
    Define font Color range int red = random.nextint (160), green = random.nextint, blue = random. Nextint (50);
    Defines a disturbance-free line interval and a starting position of int nor = Random.nextint (m), Rsta = Random.nextint (131); Draw interference sine wave M: curve flatness, d:y axis constant V:X axis focal length int M = Random.nextint + 5, D = Random.nextint (%) +, V = random. NE
    Xtint (5) + 1;
    Double x = 0.0;
    Double y = M * Math.sin (Math.toradians (V * x)) + D;
    Double px, py;
      for (int i = 0; i < 131 i++) {px = x + 1;
      PY = M * Math.sin (Math.toradians (V * px)) + D;
      if (Rsta < i && i < (RSTA + NOR)) G.setcolor (new Color (230, 230, 250)); Else
        G.setcolor (New Color (red, green, blue));
      Randomly set pixel point broadband (line width) g.setstroke (new Basicstroke (float) (Math.random () + 1.5f));
      G.draw (New line2d.double (x, y, px, py));
      x = px;
    y = py;
    } char[] codes = Randomcode.tochararray ();
      for (int i = 0; i < codes.length i++) {//rotate graphics int degree = (Random.nextint (20)-10)% 360; Double ang = degree * 0.0174532925;
      Convert Angle to Radian g.rotate (Ang, WIDTH/2, HEIGHT/2);
      G.setcolor (New Color (red, green, blue));
      G.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on);
      int gr = Random.nextint (8);
    g.DrawString (String.valueof (codes[i]), * i + + GR, 38);
    } g.dispose ();
  return image; public static String createrandomcode (int size, int width, int height) {StringBuffer Randomcode = new Stringbuff
    ER ();
      for (int i = 0; i < size; i++) {Random Random = new Random (); int start = RandOm.nextint (length);
      String Strrand = base.substring (Start, start + 1);
    Randomcode.append (Strrand);
  return randomcode.tostring ();
 }
}

JSP page:

The following is a JSP imitate Tencent random validation picture code quote fragment:

<%@ page contenttype= "image/jpeg" pageencoding= "GBK"%> <%@
page import= "java.awt.image.BufferedImage"% >
<% @page import= "Javax.imageio.ImageIO"%>
<%@ page import= "Com.icesoft.image.TokenUtil"%>

<%
  //Settings page does not cache
  Response.setheader ("Pragma", "No-cache");
  Response.setheader ("Cache-control", "No-cache");
  Response.setdateheader ("Expires", 0);
  int width =, height = m;
  String Randomcode = Tokenutil.createrandomcode (4, width, height);
  System.out.println ("Generated random code:" + randomcode);
  BufferedImage image = Tokenutil.createtokenimage (width, height, randomcode);
  Imageio.write (Image, "JPEG", Response.getoutputstream ());
  Response.flushbuffer ();
  Out.clear ();
  out = Pagecontext.pushbody ();
%>

The above is JSP imitate Tencent random verification picture all code, hope to everybody's study help.

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.