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.