Java implementation of picture verification code specific Codes _java

Source: Internet
Author: User

The example of this article for you to share the Java Image Verification code for your reference, the specific content as follows

Web page Display effect:

index.jsp
to force a picture update in two ways:
1, set the picture output is not cached.
2, with JS change the requested address.

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>  

Java code to generate Authenticode automatically

Key classes (the contents of the base section for the following classes)
BufferedImage
ImageIO
Graphics/graphics2d

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.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; public class Imageservlet extends httpservlet{@Override protected void Service (HttpServletRequest req, Httpservletresp
  Onse resp) throws Servletexception, IOException {//System.out.println ("Jinlaile"); Resp.setcontenttype ("Image/jpeg");//jpeg is a picture format.
  Set the response content to the type of JPEG picture int width=64;
  int height=40;
  BufferedImage bimg=new bufferedimage (width, height, bufferedimage.type_int_rgb);

  Graphics G=bimg.getgraphics ();
  Background g.setcolor (color.white);
  G.fillrect (0, 0, width, height);
  Font color g.setfont (new font ("AA", font.bold,18));
  Generate verification code with random number: integer Random r=new Random () within 4 0~9; for (int i=0;i<=4;i++) {int t=R.nextint (10); random integer int y=10+r.nextint (20) within//10;/up/down position: 10~30 color C=new color (r.nextint (255), R.nextint (255), r.ne
   Xtint (255));
   G.setcolor (c);
  g.DrawString ("" +t, i*16, y);
   ///Draw interference line for (int i=1;i<8;i++) {color C=new color (r.nextint (255), R.nextint (255), R.nextint (255));
   G.setcolor (c);
  G.drawline (R.nextint (width), r.nextint (height), r.nextint (width), r.nextint (height));
  ///Brush the graph to the Bimg object G.dispose ()//The Close () method in IO is with automatic flush ();
 Imageio.write (bimg, "JPEG", Resp.getoutputstream ());//RESP Get Req object, the encapsulation of socket to client, write to client}

Web.xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" xml Ns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> <!-- Access b.jsp files in the security directory Web-inf--> <servlet> <servlet-name>bbbb</servlet-name> <jsp-file>/ Web-inf/b.jsp</jsp-file> </servlet> <!--access to the Java code for generating Authenticode--> <servlet> <servlet-name> Img</servlet-name> <servlet-class>cn.hncu.servlet.ImageServlet</servlet-class> </servlet > <servlet-mapping> <servlet-name>bbbb</servlet-name> <url-pattern>/bb.asp</ url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>img</servlet-name> < Url-pattern>/confirm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
 

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.