使用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 ...
随机推荐
- 虚拟机安装centos6.5
最近想搞一下代码覆盖率的jacoco,需要在linux环境下部署一套jenkins.故需要装一个centos的虚拟机. 一.安装虚拟机. 下载后安装一个虚拟机,我选择的是VMware虚拟机 二.安装c ...
- poj2945 Find the Clones
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8490 Accepted: 3210 D ...
- 【2018.10.18】noip模拟赛Day2 地球危机(2018年第九届蓝桥杯C/C++A组省赛 三体攻击)
题目描述 三体人将对地球发起攻击.为了抵御攻击,地球人派出了 $A × B × C$ 艘战舰,在太 空中排成一个 $A$ 层 $B$ 行 $C$ 列的立方体.其中,第 $i$ 层第 $j$ 行第 $k ...
- C++练习,计算间隔天数
time_t time(time_t *time) 从1970年1月1日到到现在的秒数,如果系统时间不存在,返回1char *ctime(const time_t *time) 返回以:day mon ...
- Laravel 控制器的request
public function request1(Request $request){ //取值 $name = Request::input('name'); //是否有值 if($request- ...
- python3.6安装遇到的问题
Ubuntu16.04版本最新的Python 3.x版本3.5 . 可以从源代码执行安装最新稳定版本3.6. 要安装Python 3.6 ,请运行以下命令: # wget https://www.py ...
- GIL锁、死锁、递归锁、定时器
GIL (Global Interpreter Lock) 锁 '''定义:In CPython, the global interpreter lock, or GIL, is a mutex th ...
- Working with multiple environments
ASP.NET Core引入了对多个环境(例如开发,暂存和生产环境)的支持. 可以用环境变量来指示应用程序正在运行的环境,从而让app来做相应的配置. Development, Staging, Pr ...
- nyoj_90_整数划分_201403161553
整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk, 其中n1≥n2≥…≥nk≥1,k≥1. 正整数 ...
- 前端跨域调请求 nginx反向代理
用 本地pc的目录,请求192.168.3.246的接口,以/api为标识 运行命令: 启动 nginx -s start 重启 nginx -s relaod 停止 nginx -s stop 查看 ...