A solution to Java graphics Verification code _java

Source: Internet
Author: User
Tags set background stringbuffer

Copy Code code as follows:

Package cn.response;

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.Random;

Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

@SuppressWarnings ("Serial")
public class Validatepicture extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
try {
Validateimage (request, response);
catch (Exception e) {
E.printstacktrace ();
}
}

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}

public static final int WIDTH = 120;
public static final int HEIGHT = 25;

public void Validateimage (HttpServletRequest request,
HttpServletResponse response) throws Exception {

BufferedImage bi = new BufferedImage (WIDTH, HEIGHT,
BUFFEREDIMAGE.TYPE_INT_RGB);//Build an image in memory
Graphics g = bi.getgraphics ();

SetBackground (g);/Set Background color
SetBorder (g);//Set border
Drawrandomline (g);//Draw Interference line
String random = Drawrandomnum ((graphics2d) g);//write random number

Request.getsession (). setattribute ("random", random);
Request.getsession (FALSE);
Response.setcontenttype ("Image/jpeg");
Set do not cache
Response.setdateheader ("Expries",-1);
Response.setheader ("Cache-control", "No-cache");

Imageio.write (BI, "JPG", Response.getoutputstream ());
}

Private String Drawrandomnum (graphics2d g) {
G.setcolor (color.red);
G.setfont (New Font ("Song Body", Font.Bold, 20));
[\U4E00-\U9FA5] Chinese digit range
String base = "\u4e00\u4f00\u5e00\u4e50\u4e89\u4f10\u4e09";

StringBuffer sb = new StringBuffer ();

int x = 5;
for (int i = 0; i < 4; i++) {
int degree = new Random (). Nextint ()% 30;

String ch = base.charat (new Random (). Nextint (Base.length ())) + "";
Sb.append (CH);
G.rotate (Degree * math.pi/180, X, 20);
g.DrawString (CH, x, 20);
G.rotate (-degree * math.pi/180, X, 20);

x = x + 30;
}
System.out.println (Sb.tostring ());
return sb.tostring ();
}

private void Drawrandomline (Graphics g) {
G.setcolor (Color.green);
for (int i = 0; i < 3; i++) {
int x1 = new Random (). Nextint (WIDTH);
int y1 = new Random (). Nextint (HEIGHT);

int x2 = new Random (). Nextint (WIDTH);
int y2 = new Random (). Nextint (HEIGHT);

G.drawline (x1, y1, x2, y2);
}
}

private void SetBorder (Graphics g) {
G.setcolor (Color.Blue);
G.drawrect (1, 1, WIDTH-2, HEIGHT-2);
}

private void SetBackground (Graphics g) {
G.setcolor (Color.White);
G.fillrect (0, 0, WIDTH, HEIGHT);
}
}

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.