A simple method to realize the function of picture verification code in Java Web _java

Source: Internet
Author: User

The user registers the website information when basically all wants the data authentication code verification. So how does the image verification code function be realized?

The approximate steps are:

1. Create a cached picture in memory

2. Set Background color

3. Draw a border

4. Write Letters

5. Draw Jamming Information

6. Picture output

Nonsense not much to say that directly on the code

Package com.lsgjzhuwei.servlet.response;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;

Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; /** * Servlet Implementation class Verificationcode/@WebServlet (asyncsupported = true, Urlpatterns = {"/verification
    
  Code "}) public class Verificationcode extends HttpServlet {private static final long serialversionuid = 1L;
    /** * @see httpservlet#httpservlet () * * * Public verificationcode () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, Httpservletrespon SE response) */protected void doget (HttpServletRequest request, HttpServletResponse ResponSE) throws Servletexception, IOException {//TODO auto-generated method Stub int width = 120;
		
		int height = 30;
		
		Create an in-memory cache picture BufferedImage BufferedImage = new BufferedImage (width, height, bufferedimage.type_int_rgb);
		Background color//Graphics drawing image Graphics Graphics = Bufferedimage.getgraphics ();
		Set color Graphics.setcolor (Color.yellow);
		
		Filling graphics.fillrect (0, 0, width, height);
		Draw a Border Graphics.setcolor (Color.Blue);
		
		Graphics.drawrect (0, 0, width-1, height-1);
		Write Letter String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";
		Random Random = new Random ();
		Sets the font color Graphics.setcolor (color.red);
		Sets the font and size Graphics.setfont (new font ("Arial", Font.Bold, 20));
		int x=20;
		int y=20;
			for (int i = 0; i < 4; i++) {int index = Random.nextint (Content.length ());
			Char letter = Content.charat (index);
			Graphics.DrawString (letter+ "", X, y);
		x = x+20;
		///Draw interference line int x1;
		int x2;
		int y1;
		int y2; GrAphics.setcolor (Color.light_gray);
			for (int i = 0;i <50;i++) {x1=random.nextint (width);
			X2=random.nextint (width);
			Y1=random.nextint (height);
			Y2=random.nextint (height);
		Graphics.drawline (x1, y1, x2, y2);
		
		
	///Output the picture to the browser///The image of the memory through the browser output into a JPG image imageio.write (bufferedimage, "JPG", Response.getoutputstream ()); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated
 Method stub}}

Above this Java Web Image Verification code function is a simple way to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.

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.