First, let’s take a look at how to generate image verification code locally, and then write the verification code output to the web page to implement it.
Let’s first look at the simplest function - the implementation is to turn a string into an image and write it to a file.
Implementation code:
package cn.hncu.img;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;//This class contains some static and convenient methods for finding ImageReader and ImageWriter and performing simple encoding and decoding. import org.junit.Test;public class ImgDemo { //Learn how to turn a string into an image and write to a file @Test public void ImgDemo1() throws FileNotFoundException, IOException{ BufferedImage img = new BufferedImage(60, 30, BufferedImage.TYPE_INT_RGB); // Represents an image with 8-bit RGB color components of synthetic integer pixels. Graphics g = img.getGraphics(); g.drawString("Hello",10,20); //Draw the text given by the specified string using the current font and color of this graphic context. The baseline of the leftmost character is located at the (x, y) position of this graph context coordinate system. g.dispose();//// Similar to close() in the stream, it drives flush()---flashes the data into the img object//Releases the context of this graph and all the system resources it uses. After calling dispose, you can no longer use the Graphics object. ImageIO.write(img, "JPG", new FileOutputStream("img/a.jpg")); //Write an image to File using any ImageWriter that supports the given format. }}result:
The above is very simple, right? The verification code we see is not like this. OK, let's add some interference lines, background color, characters and y coordinates to it.
Verification code with interference lines and background colors - write to file
Demo code:
package cn.hncu.img;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import java.util.Random;import javax.imageio.ImageIO;//This class contains some used to find ImageReader and ImageWriter And static and convenient methods to perform simple encoding and decoding. import org.junit.Test;public class ImgDemo { //Change the above string into the verification code we usually use - generate several random numbers, with background color and interference lines @Test public void ImgDemo2() throws FileNotFoundException, IOException{ int width = 80; int height= 40; int lines = 10; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); //Set the background color g.setColor(Color.white); g.fillRect(0, 0, width, height);//Draw the background//Fill the specified rectangle. Fill the rectangle with the current color of the graphic context //Set the font g.setFont(new Font("宋体", Font.BOLD, 18)); //Random number Date d = new Date(); //System.out.println(d.getTime()); Random r = new Random(d.getTime()); for(int i=0;i<4;i++){ int a = r.nextInt(10); //Please an integer within 10[0, 9] int y = 10+r.nextInt(20); //A integer in the range 10~30, as the y coordinate Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g.setColor(c); g.drawString(""+a, 5+i*width/4, y); } //Interference line for(int i=0;i<lines;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)); } g.dispose();//Similar to close() in the stream, drives flush()---Flash data into the img object ImageIO.write(img, "JPG", new FileOutputStream("img/b.jpg")); }}Demonstration results:
Finally, let’s look at a verification code that can be rotated and scaled - write it into the local file of the picture
Demo code:
package cn.hncu.img;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import java.util.Random;import javax.imageio.ImageIO;//This class contains some static and convenient methods for finding ImageReader and ImageWriter and performing simple encoding and decoding. import org.junit.Test;public class ImgDemo { @Test//Verification code that can rotate and zoom public void ImgDemo3() throws FileNotFoundException, IOException{ int width = 80; int height = 40; int lines = 10; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D)img.getGraphics(); g2d.setFont(new Font("宋体", Font.BOLD, 20)); Random r = new Random(new Date().getTime()); //Set the background color g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g2d.drawRect(0, 0, width, height); //Draw the border of the specified rectangle. g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g2d.fillRect(0, 0, width, height);//Fill the specified rectangle. for(int i=0;i<4;i++){ String str = ""+r.nextInt(10); //Processing rotation AffineTransform Tx = new AffineTransform(); Tx.rotate(Math.random(), 5+i*15, height-5); //The rotation angle measured with radians, the X coordinate of the rotation anchor, and the Y coordinate of the rotation anchor //Tx.scale(0.7+Math.random(), 0.7+Math.random()); //The scaling multiple in the x coordinate direction, the scaling multiple in the y coordinate direction g2d.setTransform(Tx); Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g2d.setColor(c); g2d.drawString(str, 2+i*width/4, height-13); } //Interference line for(int i=0;i<lines;i++){ Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g2d.setColor(c); g2d.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } g2d.dispose(); ImageIO.write(img, "JPG", new FileOutputStream("img/c.jpg")); }}Demonstration results:
Now we will start demonstrating the front desk image verification technology.
Front desk picture verification technology
Structure diagram of this project:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <script type="text/javascript"> function changImg(){ var img = document.getElementById("servletImg"); var d = new Date(); var time = d.getTime();//If there is no this //The following sentence will not work, because the browser's caching technology, the image will not be refreshed //img.src="/myHelloWeb/servlet/ImageServlet"; img.src="/myHelloWeb/servlet/ImageServlet?"+time; //The thing behind the number is passed through get} </script> </head> <body> This is my manual homepage! <br/> <img id="servletImg" src="/myHelloWeb/servlet/ImageServlet" /><a href="javascript:changImg()">Unclear</a> </body></html>web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ImageServlet</servlet-name> <servlet-class>cn.hncu.img.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/ImageServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
ImageServlet.java
package cn.hncu.img;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;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 { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Tell the client that the output format response.setContentType("image/jpeg"); int width = 80; int height = 40; int lines = 10; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); //Set the background color g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); //Set the font g.setFont(new Font("宋体", Font.BOLD, 20)); //Random Random r = new Random(new Date().getTime()); for(int i=0;i<4;i++){ int a = r.nextInt(10); int y = 10+r.nextInt(20); // An integer in the range 10~30, as the y coordinate Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)); g.setColor(c); g.drawString(""+a, 5+i*width/4, y); } //The interference line for(int i=0;i<lines;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)); } g.dispose();//Similar to close() in the stream, drive flush()---Flash data into the img object ImageIO.write(img, "JPG", response.getOutputStream()); }}Demonstration results:
The following is in index.jsp:
If this sentence is used:
img.src=”/myHelloWeb/servlet/ImageServlet”;
You can check the response header:
Let’s take a look at the response header using this sentence:
img.src=”/myHelloWeb/servlet/ImageServlet?”+time;
There is an extra Date response!
Because time has been changing, every time I click to see it, I will request it to the server again, and I will not stop requesting it because of the browser's cache.
The verification code ends here.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.