zoukankan      html  css  js  c++  java
  • 彩色画图验证码

    废话少说,看看效果

     前端页面:

     

    后端详细源码与注释贴上

    package com.controller;


    import com.tools.InterLine;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.Random;

    @Controller
    public class GetImageCode {

    /**
    * 获取图片验证码
    */
    @RequestMapping("/getImageCode")
    @ResponseBody
    public void getImageCode(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
    // System.out.println("获取图片验证码");

    /**
    * 自定义绘图
    */
    //图片宽像素
    int wpx = 80 ;
    // 高像素
    int hpx = 30;

    // 1.创建一个不带透明色的BufferedImage对象,即图片对象
    BufferedImage bfi = new BufferedImage(wpx, hpx, BufferedImage.TYPE_INT_RGB);
    //绘画工具,对图片画图
    Graphics g = bfi.getGraphics();
    // 用预先指定的颜色填充矩形
    g.fillRect(0, 0, wpx, hpx);
    //验证码字符范围
    char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    //随机数类
    Random r = new Random();
    int index;
    //保存字符串
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 4; i++) {
    // 生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。
    index = r.nextInt(ch.length);
    //对每个字符设置不同颜色
    g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
    //设置字体
    // Font font = new Font("宋体", 30, 30);
    //宋体,加粗,大小为30
    Font font = new Font("宋体", Font.BOLD, 27);
    g.setFont(font);
    // 用预先设置好的颜色和字体来绘制文本str,文本左下角的坐标为(x,y)
    g.drawString(ch[index] + "", (i * 20) + 2, 23);
    //拼接字符串
    sb.append(ch[index]);
    }
    // System.out.println(sb);


    //添加噪点,噪点越多,越不好识别
    //噪点面积,这里设为整个图片的百分之2
    int area = (int) (0.04 * wpx * hpx);
    for (int i = 0; i < area; i++) {
    //随机设置噪点坐标,不可超出图片像素宽高
    int x = (int) (Math.random() * wpx);
    int y = (int) (Math.random() * hpx);
    //将此 BufferedImage 中的像素设置为指定的 RGB 值,屏幕上的任何一个颜色都可以由一组RGB值来记录和表达。
    //参数意思:x坐标,y坐标,RGB颜色亮度值,这不属于画画,这是噪点设置是图片
    bfi.setRGB(x, y, (int) (Math.random() * 255));
    }

    //设置验证码中的干扰线
    //干扰线条数
    int count = 6;
    //调用设置RGB方法
    InterLine interLine = new InterLine();
    for (int i = 0; i < count; i++) {
    //随机获取干扰线的起点和终点,即开始和结束点的两个坐标
    //开始坐标
    int xstart = (int) (Math.random() * wpx);
    int ystart = (int) (Math.random() * hpx);
    //结束坐标
    int xend = (int) (Math.random() * wpx);
    int yend = (int) (Math.random() * hpx);
    //设置线条颜色
    Color color = interLine.interLine(1, 255);
    g.setColor(color);
    g.drawLine(xstart, ystart, xend, yend);
    }
    //设置session
    HttpSession httpSession = request.getSession();
    httpSession.setAttribute("verificationCode", sb);
    // String verificationCode =new String((StringBuffer) session.getAttribute("verificationCode"));
    // System.out.println("输出sb:"+verificationCode);
    //图片输出流 ,参数意思:图片对象,图片后缀格式,输出流以什么途径传输,如果是远程控制项目,可以websokect传输
    ImageIO.write(bfi, "JPG", response.getOutputStream());

    }
    }

     另一个工具类:

    package com.tools;

    import java.awt.*;

    public class InterLine {
    public Color interLine(int Low, int High) {
    if (Low > 255)
    Low = 255;
    if (High > 255)
    High = 255;
    if (Low < 0)
    Low = 0;
    if (High < 0)
    High = 0;
    //RGB参数范围,最大范围在0~255
    int interval = High - Low;
    int r = Low + (int) (Math.random() * interval);
    int g = Low + (int) (Math.random() * interval);
    int b = Low + (int) (Math.random() * interval);
    return new Color(r, g, b);
    }
    }
  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/11601284.html
Copyright © 2011-2022 走看看