首页 技术 正文
技术 2022年11月7日
0 收藏 790 点赞 297 浏览 1617 个字
<%@ page language="java"  pageEncoding="UTF-8"  %><%@ page contentType="image/jpeg"  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %><%!    Color getRandColor(int fc, int bc) {//给定范围获得随机颜色        Random random = new Random();        if (fc > 255)            fc = 255;        if (bc > 255)            bc = 255;        int r = fc + random.nextInt(bc - fc);        int g = fc + random.nextInt(bc - fc);        int b = fc + random.nextInt(bc - fc);        return new Color(r, g, b);    }%><%    //设置页面不缓存    response.setHeader("Pragma", "No-cache");    response.setHeader("Cache-Control", "no-cache");    response.setDateHeader("Expires", 0);    //生成随机验证码    Random random = new Random();    String sRand = "";    for (int i = 0; i < 4; i++) {        String rand = String.valueOf(random.nextInt(10));        sRand += rand;    }    // 将认证码存入SESSION    session.setAttribute("CHECK_NUMBER_KEY", sRand);    // 在内存中创建图象    int width = 55;    int height = 20;    BufferedImage image = new BufferedImage(width, height,            BufferedImage.TYPE_INT_RGB);    // 获取图形上下文    Graphics g = image.getGraphics();    // 设定背景色    g.setColor(getRandColor(200, 250));    g.fillRect(0, 0, width, height);    //设定字体    g.setFont(new Font("serif", Font.CENTER_BASELINE, 16));    //画边框    //g.setColor(new Color(1));    //g.drawRect(0,0,width-1,height-1);    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到    g.setColor(getRandColor(160, 200));    for (int i = 0; i < 100; i++) {        int x = random.nextInt(width);        int y = random.nextInt(height);        int xl = random.nextInt(12);        int yl = random.nextInt(12);        g.drawLine(x, y, x + xl, y + yl);    }    for (int i = 0; i < 4; i++) {        String rand = sRand.substring(i, i + 1);        // 将认证码显示到图象中        g.setColor(new Color(20 + random.nextInt(110), 20 + random                .nextInt(110), 20 + random.nextInt(110)));        //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成        g.drawString(rand, 13 * i + 6, 16);    }    // 图象生效    g.dispose();    // 输出图象到页面    try {        ImageIO.write(image, "JPEG", response.getOutputStream());    } catch (Exception e) {    }    out.clear();    out = pageContext.pushBody();%>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905