Implementation of the image verification code function in Java Web

Source: Internet
Author: User

Users in the registration of the site information is basically the data verification code verification. So how to implement the image verification code function?

The approximate steps are:

1. Create a cached picture in memory

2. Setting the background color

3. Draw a border

4. Write Letters

5. Draw interference Information

6. Picture output

Nonsense not much to say 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 = {"/verificationcode"}) public class Verificationcode extends HttpServlet {private St           Atic final Long serialversionuid = 1L;        /** * @see httpservlet#httpservlet () */public Verificationcode () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws ServleteXception, IOException {//TODO auto-generated method stubint width = 120;int height = 30;//Create an in-memory cached picture BufferedImage Buffe Redimage = new BufferedImage (width, height, bufferedimage.type_int_rgb);///Background color//graphics Drawing graphics graphics Bufferedimage.getgraphics ();//Set Color Graphics.setcolor (color.yellow);//Fill graphics.fillrect (0, 0, width, height);/// Draw Border Graphics.setcolor (Color.Blue); graphics.drawrect (0, 0, width-1, height-1);//write the letter string content = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890 "; Random random = new random ();//Set Font color Graphics.setcolor (color.red);//Set font and size Graphics.setfont (new font ("Arial", Font.Bold ), 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 the disturbance 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 is output by the browser output stream 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}}


Implementation of the image verification code function in Java Web

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.