使用Java绘制验证码
效果图:
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;
}
}
使用Java绘制验证码的更多相关文章
- java Web 请求servlet绘制验证码简单例子
主要用来了解java代码怎么绘制验证码图片,实际开发中不会这样用 protected void doGet(HttpServletRequest request, HttpServletRespons ...
- 【开发技术】Java生成验证码
Java生成验证码 为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下; ...
- java制作验证码(java验证码小程序)
手动制作java的验证码 Web应用验证码的组成: (1)输入框 (2)显示验证码的图片 验证码的制作流程: 生成验证码的容器使用 j2ee的servlet 生成图片需要的类: (1) Buffere ...
- 使用Java设计验证码生成程序
我们来设计一个简单的验证码生成程序:验证码一个由4位的数字.字母随机组合而成图像,为了避免被光学字元识别(OCR,Optical Character Recognition)之类的程序识别出图片中的数 ...
- 用canvas绘制验证码
在通常的登录界面我们都可以看到验证码,验证码的作用是检测是不是人在操作,防止机器等非人操作,防止数据库被轻而易举的攻破. 验证码一般用PHP和java等后端语言编写: 但是在前端,用canva或者SV ...
- Java web验证码
绘制验证码的主要步骤: 1,设置宽度高度,验证码个数,干扰线个数,可选字符,背景颜色,字体格式 2,画干扰线,随机生成颜色,字体,字符 3,设置缓冲区,得到画笔,设置边框,读写数据,存储图片. 1,S ...
- java图形验证码
用java实现验证码的生成,以下代码是一个controller,可以直接使用 package org.jxnd.tongxuelu.controller; import java.awt.Color; ...
- java实现验证码功能
java实现验证码功能 通过java代码实现验证码功能的一般思路: 一.通过java代码生成一张验证码的图片,将验证码的图片保存到项目中的指定文件中去,代码如下: package com.util; ...
- java生成图片验证码(转)--封装生成图片验证码的工具类
博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...
随机推荐
- redis2.3.7安装时出现undefined reference to `clock_gettime'
(转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...
- Unity3D for iOS初级教程:Part 2/3
转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...
- POJ——3126Prime Path(双向BFS+素数筛打表)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16272 Accepted: 9195 Descr ...
- Spring-IOC源码解读2-容器的初始化过程
1. IOC容器的初始化过程:IOC容器的初始化由refresh()方法启动,这个启动包括:BeanDifinition的Resource定位,加载和注册三个过程.初始化的过程不包含Bean依赖注入的 ...
- 征途(bzoj 4518)
Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...
- C#.net的常用函数列表
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 1、DateTime 数字型 System.DateTime currentTime=new System.Dat ...
- Day 6 Linux基础之正文处理、vi编辑和系统初始化和服务
Linux基础之正文处理.vi编辑和系统化服务 一.正文处理命令及tar命令 1.归档 定义:归档(archiving)就是将许多文件(或目录)打包成一个文件. 目的:归档的目的就是方便备份.还原及文 ...
- Mac OS X 10.10.5 中 VirtualBox 5.0 里的 Win7 虚拟机无法使用 USB 3.0 设备的解决办法(补充说明)
上一篇文章中,我说到了如何在Mac OS X 10.10.5 中让 VirtualBox 5.0 里的 Win7 虚拟机使用 USB 3.0.最近碰巧升级 MacBook Pro 为最新的 15 吋 ...
- CodeForces - 813C The Tag Game (树的dfs遍历)
[传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...
- 高性能mysql之schema与数据类型优化
1.数据类型 http://www.cnblogs.com/YDDMAX/p/4937770.html