java图片验证码生成教程详解

网友投稿 271 2023-07-10


java图片验证码生成教程详解

首先,我们先来看本地如何生成图片验证码的,再来写输出到网页的验证码如何实现。

先来看最简单的—实现的功能是,将一个字符串变成图片写入到文件中

实现代码:

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;

//该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。

import org.junit.Test;

public class ImgDemo {

//学习如何把一个字符串变成图片写到一个文件

@Test

public void ImgDemo1() throws FileNotFoundException, IOException{

BufferedImage img = new BufferedImage(60, 30, BufferedImage.TYPE_INT_RGB);

// 表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。

Graphics g = img.getGraphics();

g.drawString("Hello",10,20);

//使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处。

g.dispose();////类似于流中的close()带动flush()---把数据刷到img对象当中

//释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象。

ImageIO.write(img, "JPG", new FileOutputStream("img/a.jpg"));

//使用支持给定格式的任意 ImageWriter 将一个图像写入 File。

}

}

结果:

上面那个很简单,对不对,我们看到的验证码都不是这样的,那好,我们给它加点干扰线,背景色,字符和y坐标随机生成。

有干扰线、背景色的验证码-写入文件

演示代码:

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;

//该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。

import org.junit.Test;

public class ImgDemo {

//把上面的字符串改成我们平时用的验证码---生成几个随机数字,有背景色和干扰线

@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();

//设置背景色

g.setColor(Color.white);

g.fillRect(0, 0, width, height);//画背景

//填充指定的矩形。使用图形上下文的当前颜色填充该矩形

//设置字体

g.setFont(new Font("宋体", Font.BOLD, 18));

//随机数字

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);//取10以内的整数[0,9]

int y = 10+r.nextInt(20); //10~30范围内的一个整数,作为y坐标

Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));

g.setColor(c);

g.drawString(""+a, 5+i*width/4, y);

}

//干扰线

for(int i=0;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();//类似于流中的close()带动flush()---把数据刷到img对象当中

ImageIO.write(img, "JPG", new FileOutputStream("img/b.jpg"));

}

}

演示结果:

最后来看一个可以旋转和放缩的验证码-写到图片本地文件中

演示代码:

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;

//该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。

import org.junit.Test;

public class ImgDemo {

@Test//可以旋转和放缩的验证码

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());

//设置背景色

g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

g2d.drawRect(0, 0, width, height);//绘制指定矩形的边框。

g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

g2d.fillRect(0, 0, width, height);//填充指定的矩形。

for(int i=0;i<4;i++){

String str = ""+r.nextInt(10);

//处理旋转

AffineTransform Tx = new AffineTransform();

Tx.rotate(Math.random(), 5+i*15, height-5);

//用弧度测量的旋转角度,旋转锚点的 X 坐标,旋转锚点的 Y 坐标

//Tx.scale(0.7+Math.random(), 0.7+Math.random());

//x坐标方向的缩放倍数,y坐标方向的缩放倍数

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);

}

//干扰线

for(int i=0;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"));

}

}

演示结果:

下面就要开始演示前台的图片验证技术了。

前台的图片验证技术

这个项目的结构图:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

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();//类似于流中的close()带动flush()---把数据刷到img对象当中

ImageIO.write(img, "JPG", new FileOutputStream("img/b.jpg"));

}

}

演示结果:

最后来看一个可以旋转和放缩的验证码-写到图片本地文件中

演示代码:

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;

//该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。

import org.junit.Test;

public class ImgDemo {

@Test//可以旋转和放缩的验证码

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());

//设置背景色

g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

g2d.drawRect(0, 0, width, height);//绘制指定矩形的边框。

g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

g2d.fillRect(0, 0, width, height);//填充指定的矩形。

for(int i=0;i<4;i++){

String str = ""+r.nextInt(10);

//处理旋转

AffineTransform Tx = new AffineTransform();

Tx.rotate(Math.random(), 5+i*15, height-5);

//用弧度测量的旋转角度,旋转锚点的 X 坐标,旋转锚点的 Y 坐标

//Tx.scale(0.7+Math.random(), 0.7+Math.random());

//x坐标方向的缩放倍数,y坐标方向的缩放倍数

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);

}

//干扰线

for(int i=0;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"));

}

}

演示结果:

下面就要开始演示前台的图片验证技术了。

前台的图片验证技术

这个项目的结构图:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

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"));

}

}

演示结果:

下面就要开始演示前台的图片验证技术了。

前台的图片验证技术

这个项目的结构图:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

这是我的手动主页!


看不清

web.xml:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://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">

This is the description of my J2EE component

This is the display name of my J2EE component

ImageServlet

cn.hncu.img.ImageServlet

ImageServlet

/servlet/ImageServlet

index.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://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">

This is the description of my J2EE component

This is the display name of my J2EE component

ImageServlet

cn.hncu.img.ImageServlet

ImageServlet

/servlet/ImageServlet

index.jsp

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 {

//告诉客户端,输出的格式

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();

//设置背景色

g.setColor(Color.WHITE);

g.fillRect(0, 0, width, height);

//设置字体

g.setFont(new Font("宋体", Font.BOLD, 20));

//随机数字

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);//10~30范围内的一个整数,作为y坐标

Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));

g.setColor(c);

g.drawString(""+a, 5+i*width/4, y);

}

//干扰线

for(int i=0;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();//类似于流中的close()带动flush()---把数据刷到img对象当中

ImageIO.write(img, "JPG", response.getOutputStream());

}

}

演示结果:

下面这个是在index.jsp中:

如果是用这句:

img.src=”/myHelloWeb/servlet/ImageServlet”;

大家可以看看响应头:

再看看用这句的响应头:

img.src=”/myHelloWeb/servlet/ImageServlet?”+time;

多了个Date响应!

因为时间一直在变,所以每次点看不清,都会再向服务器请求一次,而不会因为浏览器的缓存,而不去请求了。

验证码就先到这里结束啦。

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();//类似于流中的close()带动flush()---把数据刷到img对象当中

ImageIO.write(img, "JPG", response.getOutputStream());

}

}

演示结果:

下面这个是在index.jsp中:

如果是用这句:

img.src=”/myHelloWeb/servlet/ImageServlet”;

大家可以看看响应头:

再看看用这句的响应头:

img.src=”/myHelloWeb/servlet/ImageServlet?”+time;

多了个Date响应!

因为时间一直在变,所以每次点看不清,都会再向服务器请求一次,而不会因为浏览器的缓存,而不去请求了。

验证码就先到这里结束啦。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基础之打印万年历的简单实现(案例)
下一篇:深入介绍Java对象初始化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~