验证码

复制代码
package javabao;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class text {
    private int wither = 150;// 定义宽
    private int height = 70;// 定义高
    private Color bjColor = new Color(235, 235, 235);// 定义颜色信息
    private String text="";
    //定义一个验证码字符库
    private String codes="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWSYZ";
    //定义一个随即顺生成亲
    private Random r=new Random();
    
    //生成随机字符
    char randomchar() {
        int index=r.nextInt(codes.length());//字符串的长度
        return codes.charAt(index);
        }
    //获取颜色
    private Color randColor() {
        //rgb混合在一起
        int red=r.nextInt(150);
        int green=r.nextInt(150);
        int blue=r.nextInt(150);
        return new Color(red, green, blue);
    }//获取字体
    private Font randomFont() {
        int index=r.nextInt(ziku.length);
        String zi=ziku[index];
        int stly =r.nextInt(4);//字体样式
        int size=r.nextInt(10)+45;
        return new Font(zi, stly, size);
        }
    //字体样式
    private String ziku[]={"宋体","黑体","楷体","微软雅黑","华文楷体","隶属","华文彩云"};
    //画干扰线
    private void drawLine(BufferedImage image) {
        int num=5;
        Graphics2D g2=(Graphics2D)image.getGraphics();//得到对应的画笔
        for(int i=0;i<num;i++) {
            int x=r.nextInt(wither/5);
            int y=r.nextInt(height);
            int x1=r.nextInt(wither/5)+wither/5*4;//x周4/5以右
            int y1=r.nextInt(height)+height/4*3;//y轴以下
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(randColor());//随机颜色
            g2.drawLine(x, y, x1, y1);//两点一线
            }
            
    }//定义一个噪点方法 
    private void zaodian(BufferedImage image) {
        int num=500;
            Graphics2D g2=(Graphics2D)image.getGraphics();
            
        for(int j=0;j<num;j++){    
            g2.setColor(randColor());
            g2.drawOval(r.nextInt(wither),r.nextInt(height),1,2);
             } 
    }
    public BufferedImage shengchegn() {
        BufferedImage image = new BufferedImage(wither, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();// 获取一只画笔
        g2d.setColor(bjColor);//随机背景颜色
        g2d.fillRect(0, 0, wither, height);// 填充一个矩形起始坐标
        g2d.setColor(randColor());//让画笔再取一个颜色
        g2d.setFont(randomFont());//随机数
        g2d.setColor(Color.green);//让画笔再取一个绿颜色
        g2d.drawRect(0, 0, wither-1, height-1);//给矩形描边
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<4;i++) {
            //循环四次
            String s=randomchar()+"";
            stringBuilder.append(s);//这个s存的一个字符的字符串就是答案的一部分
            g2d.setColor(randColor());
            int x=wither/4*i;
            int y=height/2+height/5;
            g2d.drawString(s, x, y);//1字符是什么x,y轴i固定
            
        }    
        text=stringBuilder.toString();//将stringBuilder类型的字符串转换成String的字符串
        //drawLine(image);
        zaodian(image);
        return image;
    }

    public void output(BufferedImage image, OutputStream Out) throws IOException {
        ImageIO.write(image, "jpg", Out);// 图片 格式 路径和文件名 方法有可能会异常 抛出异常

    }

    public String getset() {
        return text;
    }

}
复制代码

定义一个测试类

复制代码
package javabao;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class text2 {

    public static void main(String[] args) {
        text a = new text();// 创建对象
        BufferedImage image = a.shengchegn();// 对象调用对象shengcheng方法

        try {
            a.output(image, new FileOutputStream("F:\\zhandian\\images\\index1.jpg"));// 注意地址
            System.out.println("验证码的正确答案是"+a.getset());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 定义try catch异常
    }

}
复制代码

 

posted @   麦当劳在逃鸡块  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示