博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Java绘制验证码
阅读量:6975 次
发布时间:2019-06-27

本文共 2704 字,大约阅读时间需要 9 分钟。

效果图:

JDemo.java

import java.io.File;import java.io.IOException;import static java.lang.System.out;import javax.imageio.ImageIO;public class JDemo {    public static void main(String[] args) throws IOException {        VerificationCode verificationCode = new VerificationCode(7);        ImageIO.write(verificationCode.getImage(), "png", new File("C:\\Users\\BuYishi\\Desktop\\New folder\\JDemo.png"));        out.println(verificationCode.getContent());    }}

VerificationCode.java

import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.util.Random;public class VerificationCode {    private StringBuilder verificationCodeContent;    public VerificationCode(int length) {        verificationCodeContent = new StringBuilder();        Random random = new Random();        for (int i = 0; i < length; ++i) {            int charType = random.nextInt(3);  //随机的验证码字符类型,0表示数字,1表示小写字母,2表示大写字母            char c;            switch (charType) {                case 0:                    c = (char) (random.nextInt(10) + '0');                    break;                case 1:                    c = (char) (random.nextInt(26) + 'a');                    break;                default:                    c = (char) (random.nextInt(26) + 'A');            }            verificationCodeContent.append(c);        }    }    public String getContent() {        return verificationCodeContent.toString();    }    public BufferedImage getImage() {        int width = 200, height = 50;        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics graphics = image.getGraphics();        graphics.setColor(Color.red);        graphics.fillRect(0, 0, width, height);  //绘制验证码图像背景为红色        Font currentFont = graphics.getFont();        Font newFont = new Font(currentFont.getFontName(), currentFont.getStyle(), 30);        FontMetrics fontMetrics = graphics.getFontMetrics(newFont);        String string = verificationCodeContent.toString();        graphics.setColor(Color.green);        graphics.setFont(newFont);        //绘制绿色的验证码,并使其居中        graphics.drawString(string, (width - fontMetrics.stringWidth(string)) / 2, (height - fontMetrics.getHeight()) / 2 + fontMetrics.getAscent());        graphics.setColor(Color.blue);        Random random = new Random();        for (int i = 0; i < 20; ++i) {  //绘制20条干扰线,其颜色为蓝            int x1 = random.nextInt(width), y1 = random.nextInt(height);            int x2 = random.nextInt(width), y2 = random.nextInt(height);            graphics.drawLine(x1, y1, x2, y2);        }        return image;    }}

 

转载于:https://www.cnblogs.com/buyishi/p/9736146.html

你可能感兴趣的文章
asp.net 操作存储过程
查看>>
已结采购订单不显示
查看>>
js控制文本框只能输入整数或浮点数
查看>>
Python中的字符串与字符编码
查看>>
Python3之logging模块浅析
查看>>
四大组件之内容提供者
查看>>
LeetCode --- Pow(x, n)
查看>>
Ajax.BeginForm 使用过程中遇到的问题
查看>>
Ubuntu Linux系统下apt-get命令详解
查看>>
【spark 深入学习 03】Spark RDD的蛮荒世界
查看>>
Android之Service
查看>>
elasticsearch(1) 安装和使用
查看>>
Windows 平台下局域网劫持测试工具 – EvilFoca
查看>>
HDU 1071 The area ——微积分
查看>>
Windows API 编程----EnumWindows()函数的用法
查看>>
SGU 521 North-East ( 二维LIS 线段树优化 )
查看>>
mac下安装mysql
查看>>
Java
查看>>
Mac 抓包工具 Charles
查看>>
hdoj1014 互质
查看>>