zoukankan      html  css  js  c++  java
  • Java 生成验证码

    package com.lf.testvity;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.Robot;
    import java.awt.image.BufferedImage;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.security.auth.message.callback.PrivateKeyCallback.Request;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import javax.sound.midi.Patch;
    
    import org.junit.Test;
    
    public class Vitiry {
        
        //验证码字符的个数
        private static int count = 4;
        //干扰线的条数
        private static int lines = 2;
        /**
         * 随机产生颜色
         * @return 颜色
         */
        private static Color getRandomColor(){
            Random random = new Random();
            Color color = new Color(random.nextInt(255)+1, random.nextInt(255)+1, random.nextInt(255)+1);
            return color;
        }
        /**
         * 获取四个字符的字符串
         * @return 字符串
         */
        public static String getForthWord() {
            String string = "23456789abcdefghijkmnpqrstuvwxyz";
            StringBuilder newStr = new StringBuilder("");
            //随机获取count个数字,根据count个随机数产生字符串
            Random random = new Random();
            for (int i = 0; i < count; i++) {
                int ranNum=random.nextInt(string.length());
                newStr.append(string.charAt(ranNum));
            }
            String str = new String(newStr);
            return str;
        }
        /**
         * 绘画验证码
         * @return BufferedImage
         */
        public static BufferedImage productImage() {
            
            int width = 70;
            int height = 30;
            // 得到图片缓存区
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            
            // 得到它的绘制环境(这张图片的笔)
            Graphics2D g2 = (Graphics2D)bi.getGraphics();
            
            // 设置颜色
            g2.setColor(Color.WHITE);
            // 填充整张图片(其实就是设置背景颜色)
            g2.fillRect(0, 0, width, height);
            // 设置字体
            g2.setFont(new Font("宋体", Font.BOLD, 25));
            //设置颜色
            g2.setColor(Vitiry.getRandomColor());
            // 向图片写字符串
            g2.drawString(Vitiry.getForthWord(), 7, 25);
            //画两条干扰线
            Random random = new Random();
            for (int i = 0; i < lines; i++) {
                g2.drawLine(2, random.nextInt(height-10)+10, width-5, random.nextInt(height));
            }
            
            return bi;
        }
        
    }
  • 相关阅读:
    WPF多线程问题
    SQL 使用经验
    [转]express 路由控制--next
    [转]浅谈Web缓存
    [转]一份优秀的前端开发工程师简历是怎么样的?
    http
    [转]HTTP详解(1)-工作原理
    [转]使用Flexible实现手淘H5页面的终端适配
    [转]理解$watch ,$apply 和 $digest --- 理解数据绑定过程
    GMT时间
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6192509.html
Copyright © 2011-2022 走看看