JavaWeb生成图片验证码

  • 注意:此图片验证码没有进行美化,以教学为主,目的是为了知道图片验证码的原理,从而能达到读懂他人较为美观的代码
  • 步骤:新建一个Servlet对象,利用Response把验证码输出到请求Servlet的页面上
    1. 创建一个对象,能在内存中画图(验证码图片对象)
      • 代码:BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);三个参数分别是:图片宽度,图片高度,图片类型(这里选用RGB)
    2. 美化图片(图片底色,边框,写入字符串验证码,画干扰线),可以新建一个画笔对象Graphics graphics = img.getGraphics();,画笔对象可以画图形,画线,填充颜色等
      2.1填充背景色: graphics.setColor(Color.pink);//设置画笔颜色 ,graphics.fillRect(0, 0,width,height);//填充范围
      2.2画一个边框:graphics.setColor(Color.blue); graphics.drawRect(0,0,width-1,height-1);
      2.3写入验证码:
String str="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random ran=new Random();//随机数
        for(int i=1;i<=4;i++){
            int index = ran.nextInt(str.length());
            //获取字符
            char ch = str.charAt(index);//随机字符
            graphics.drawString(ch+"",(width/5)*i,height/2);
        }

2.4画干扰线:

graphics.setColor(Color.green);//s设置干扰线颜色
        //随机生成线的起止坐标点
        for(int i=0;i<10;i++){
            int x1=ran.nextInt(width);
            int x2=ran.nextInt(width);
            int y1=ran.nextInt(height);
            int y2=ran.nextInt(height);
            graphics.drawLine(x1,y1,x2,y2);
        }
  1. 将图片输出到页面展示:ImageIO.write(img,"jpg",response.getOutputStream());
  • 源码
    Servlet:
package Response;

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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/YzmServlet")
public class YzmServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int width=100;
        int height=50;
        //1.创建一个对象,能在内存中画图(验证码图片对象)
        BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //2.m美化图片
        //2.1 填充背景色
        Graphics graphics = img.getGraphics();//画笔对象
        graphics.setColor(Color.pink);//设置画笔颜色
        graphics.fillRect(0, 0,width,height);//填充范围
        //2.2 画一个边框
        graphics.setColor(Color.blue);
        graphics.drawRect(0,0,width-1,height-1);
        //2.3 写验证码
        String str="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random ran=new Random();//随机数
        for(int i=1;i<=4;i++){
            int index = ran.nextInt(str.length());
            //获取字符
            char ch = str.charAt(index);//随机字符
            graphics.drawString(ch+"",(width/5)*i,height/2);
        }
        //2.4画干扰线
        graphics.setColor(Color.green);//s设置干扰线颜色
        //随机生成线的起止坐标点
        for(int i=0;i<10;i++){
            int x1=ran.nextInt(width);
            int x2=ran.nextInt(width);
            int y1=ran.nextInt(height);
            int y2=ran.nextInt(height);
            graphics.drawLine(x1,y1,x2,y2);
        }
        //3.将图片输出到页面展示
        ImageIO.write(img,"jpg",response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

Html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script !src="">
<!--        点击图片或者超链接换一张图片-->
        //给图片和超链接绑定单击事件,重新设置src
        window.onload=function () {
            //获取图片对象
            var img=document.getElementById("checkCoda");
            img.onclick=function (ev) {
                //在src后加上时间戳,防止src一样浏览器直接从缓存调用达不到切换的效果
                var date =new Date().getTime();
                img.src="/hellotomcat/YzmServlet?"+date;
            }
            var a=document.getElementById("change");
            a.onclick=function (ev) {
                var date =new Date().getTime();
                img.src="/hellotomcat/YzmServlet?"+date;
            }
}
    </script>
</head>
<body>
    <img id="checkCoda" src="/hellotomcat/YzmServlet" alt="">
    <a id="change" href="">看不清?换一张</a>
</body>
</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值