效果图:

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;
}
}

最新文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (23) -----第五章 加载实体和导航属性之预先加载与Find()方法
  2. nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
  3. Interoperability between Java and SharePoint 2013 on Premises
  4. hadoop(三):hdfs 机架感知
  5. some resource favor
  6. c#枚举自定义,用于数据绑定。 z
  7. tomcat 内存配置
  8. centos 5.4 安装nodejs + npm(转)
  9. SRM475 - SRM479(1-250pt,500pt)
  10. 表单验证插件jquery.validate的使用方法演示
  11. fastclick 源码阅读备份
  12. java自动化测试-http请求post
  13. oracle参数MEMORY_TARGET太小无法启动的解决过程
  14. JS生成URL二维码
  15. 迅为4412开发板QtE系统源码-屏幕横竖屏切换修改方法
  16. consul初步学习
  17. Linux中THIS_MODULE宏定义详解
  18. 【转载整理】mysql权限分配详解
  19. input 与raw_input的区别
  20. [SDOI2017]硬币游戏

热门文章

  1. heartbeat负载均衡详解
  2. 【Luogu】P2894酒店Hotel(线段树)
  3. BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸 ——动态规划
  4. [BZOJ1584] [Usaco2009 Mar]Cleaning Up 打扫卫生(DP)
  5. [BZOJ1583] [Usaco2009 Mar]Moon Mooing 哞哞叫(队列)
  6. [ZJOI2007]时态同步 (树形DP)
  7. python练习之-计算器
  8. Linux下二进制包、源代码包、rpm包
  9. ROS安装环境配置及多版本的切换
  10. ubuntu下安装翻译软件