Java Web picture verification feature implementation two

Source: Internet
Author: User

The previous implementation is the simplest picture verification, this article to achieve a complex point: the picture is rotated, and through a registered instance of the image validation nested in the Web page

The code to implement the image rotation function is:

Write the letter string content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890"; Random random = new random (); Graphics2D graphics2d = (graphics2d) graphics;//set Font color Graphics2d.setcolor (color.red);//Set font and size Graphics2d.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);d ouble jiaodu = random.nextint -30;double theta = Jiaodu/360*2*math.pi; Graphics2d.rotate (theta, x, y), graphics2d.drawstring (letter+ "", X, y), Graphics2d.rotate (-theta, x, y); x = x+20;}

To implement the servlet code with the Rotate Picture verification feature:

Package Com.lsgjzhuwei.servlet.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.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 a In-Memory cached picture BufferedImage BufferedImage = new BufferedImage (width, height, bufferedimage.type_int_rgb);///Background color// Draw an image through the graphics 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 (); Graphics2D graphics2d = (graphics2d) graphics;//set Font color Graphics2d.setcolor (color.red);//Set font and size Graphics2d.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);d ouble jiaodu = random.nextint -30;double theta = Jiaodu/360*2*math.pi; Graphics2d.rotate (theta, x, y), graphics2d.drawstring (letter+ "", X, y); graphics2d.rotatE (-theta, 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}}

Here is the new HTML page, and add a simple registration button, the source code is as follows:

<! DOCTYPE html>

The operating interface of the system is as follows:


Here is the function of complex points: Verification code refresh when clicking on the picture Verification code

Modify the HTML page code as follows:

<! DOCTYPE html>



Java Web picture verification feature implementation two

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.