欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java代码生成图片验证码

程序员文章站 2022-06-30 11:41:50
1.jsp页面显示验证码,src跳转到Controller里的:getImg_validate,reloadImg()作用是点击验证码就会换一张(重新请求getImg_validate)
  • 验证码:
  • 1.jsp页面显示验证码,src跳转到controller里的:getimg_validate,reloadimg()作用是点击验证码就会换一张(重新请求getimg_validate)

    <ul>
    <li >验证码: </li>
    <li >
    <img id="img_validate" src="<%=basepath%>image/getimg_validate" onclick="reloadimg()">
    <input type="text" name="yanzhengma" id="yanzhengma" placeholder="请输入验证码"/>
    </li>
    </ul>


    <script type="text/javascript">

    function reloadimg() {

    document.getelementbyid("img_validate").src="<%=basepath%>image/getimg_validate";
    }

    </script>

    Java代码生成图片验证码

     

    2.写一个imageutil工具类,在里面可以自定义图片生成的大小,字体大小,背景色,验证码个数,干扰线条条数等等

    package com.svse.house.util;

    import java.awt.color;
    import java.awt.font;
    import java.awt.graphics;
    import java.awt.image.bufferedimage;
    import java.util.random;

    public class imageutil {

    //1.定义变量保存生成后的验证码字符串
    static string code = "";

    //2.生成验证码
    public static string createcode() {
    code = "";
    string a = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";//随机生成的字符范围(0-9,a-z,a-z)

    //生成验证码的位数(这里是4位)
    for (int i = 0; i < 4; i++) {
    int index = (int) (math.random() * 62);//会产生一个[0,62)的数,不包含小数
    char b = a.charat(index);
    code = code + b;
    }
    return code;
    }

    //查看已经生成的验证码
    public static string getcode() {
    return code;
    }

    //3.生成图片
    public static bufferedimage createimage() {
    // 前2个参数为:width, height.后面是图像类型
    //创建一个不带透明色的bufferedimage对象,type_int_argb为带透明色
    bufferedimage bi = new bufferedimage(130,50, bufferedimage.type_int_rgb);

    //1.得到一个画布
    graphics g = bi.getgraphics();
    //2.添加背景颜色
    g.setcolor(color.white);
    g.fillrect(0, 0, 130, 50);

    //3.添加干扰线
    for (int i = 0; i < 10; i++) {
    random r = new random();
    int red = r.nextint(256);
    int green = r.nextint(256);
    int blue = r.nextint(256);
    color c = new color(red, green, blue);
    g.setcolor(c);
    int x1 = r.nextint(131);
    int y1 = r.nextint(51);
    int x2 = r.nextint(131);
    int y2 = r.nextint(51);
    g.drawline(x1,y1,x2,y2);//画线
    //g.drawoval(x1, y1, x2, y2);//画曲线
    }


    //3.添加文字
    g.setcolor(color.black);
    g.setfont(new font("宋体", font.bold, 50));

    string str = getcode();
    //4.将文字填充到画板中
    g.drawstring(str, 15, 40);

    //5.关闭画布
    g.dispose();
    return bi;
    }

    }

    3.在controller层调用getimg_validate,试用过jpeg和bmp格式的setcontenttype,都可以

    package com.svse.house.controller;

    import java.awt.image.bufferedimage;
    import java.io.bytearrayoutputstream;
    import java.io.ioexception;

    import javax.imageio.imageio;
    import javax.servlet.servletoutputstream;
    import javax.servlet.http.httpservletresponse;

    import org.springframework.stereotype.controller;
    import org.springframework.web.bind.annotation.requestmapping;

    import com.svse.house.util.imageutil;

    @controller
    @requestmapping("/image")
    public class imagecontroller {
    @requestmapping("/getimg_validate")
    public void yanzheng(httpservletresponse response){
    try {
    imageutil.createcode();
    bufferedimage image = imageutil.createimage();
    //以流的方式返回给客户端
    response.setcontenttype("image/jpeg");
    //response.setcontenttype("image/bmp");
    bytearrayoutputstream bt = new bytearrayoutputstream();
    //将图片转换成字节流
    imageio.write(image,"jpeg",bt);
    //imageio.write(image,"bmp",bt);
    //得到输出流,返回客户端
    servletoutputstream outputstream = response.getoutputstream();
    outputstream.write(bt.tobytearray());
    } catch (ioexception e) {
    e.printstacktrace();
    }
    }
    }

    Java代码生成图片验证码