资讯专栏INFORMATION COLUMN

图片验证码的JAVA工具类

SKYZACK / 765人阅读

摘要:我们平时开发时经常会遇到需要图片验证码,基础的验证码包括了数字字母甚至可能有汉字。下面我给出一个简单的工具类。验证码生成器图片的宽度。

我们平时开发时经常会遇到需要图片验证码,基础的验证码包括了数字、字母、甚至可能有汉字。下面我给出一个简单的工具类。

</>复制代码

  1. package com..ankang.tony.util;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.util.Date;
  11. import java.util.Random;
  12. import javax.imageio.ImageIO;
  13. /**
  14. * 验证码生成器
  15. */
  16. public class ValidateCode {
  17. // 图片的宽度。
  18. private int width = 160;
  19. // 图片的高度。
  20. private int height = 40;
  21. // 验证码字符个数
  22. private int codeCount = 5;
  23. // 验证码干扰线数
  24. private int lineCount = 150;
  25. // 验证码
  26. private static String code = null;
  27. // 验证码图片Buffer
  28. private BufferedImage buffImg = null;
  29. private char[] codeSequence = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R",
  30. "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9" };
  31. public ValidateCode() {
  32. this.createCode();
  33. }
  34. /**
  35. *
  36. * @param width
  37. * 图片宽
  38. * @param height
  39. * 图片高
  40. */
  41. public ValidateCode(int width, int height) {
  42. this.width = width;
  43. this.height = height;
  44. this.createCode();
  45. }
  46. /**
  47. *
  48. * @param width
  49. * 图片宽
  50. * @param height
  51. * 图片高
  52. * @param codeCount
  53. * 字符个数
  54. * @param lineCount
  55. * 干扰线条数
  56. */
  57. public ValidateCode(int width, int height, int codeCount, int lineCount) {
  58. this.width = width;
  59. this.height = height;
  60. this.codeCount = codeCount;
  61. this.lineCount = lineCount;
  62. this.createCode();
  63. }
  64. public void createCode() {
  65. int x = 0, fontHeight = 0, codeY = 0;
  66. int red = 0, green = 0, blue = 0;
  67. x = width / (codeCount + 1);// 每个字符的宽度
  68. fontHeight = height - 2;// 字体的高度
  69. codeY = height - 3;
  70. // 图像buffer
  71. buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  72. Graphics2D g = buffImg.createGraphics();
  73. // 生成随机数
  74. Random random = new Random();
  75. // 将图像填充为白色
  76. g.setColor(Color.WHITE);
  77. g.fillRect(0, 0, width, height);
  78. // 创建字体
  79. ImgFontByte imgFont = new ImgFontByte();
  80. Font font = imgFont.getFont(fontHeight);
  81. g.setFont(font);
  82. for (int i = 0; i < lineCount; i++) {
  83. int xs = random.nextInt(width);
  84. int ys = random.nextInt(height);
  85. int xe = xs + random.nextInt(width / 8);
  86. int ye = ys + random.nextInt(height / 8);
  87. red = random.nextInt(255);
  88. green = random.nextInt(255);
  89. blue = random.nextInt(255);
  90. g.setColor(new Color(red, green, blue));
  91. g.drawLine(xs, ys, xe, ye);
  92. }
  93. // randomCode记录随机产生的验证码
  94. StringBuffer randomCode = new StringBuffer();
  95. // 随机产生codeCount个字符的验证码。
  96. for (int i = 0; i < codeCount; i++) {
  97. String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
  98. // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。
  99. red = random.nextInt(255);
  100. green = random.nextInt(255);
  101. blue = random.nextInt(255);
  102. g.setColor(new Color(red, green, blue));
  103. g.drawString(strRand, (i + 1) * x, codeY);
  104. // 将产生的四个随机数组合在一起。
  105. randomCode.append(strRand);
  106. }
  107. // 将四位数字的验证码保存到Session中。
  108. code = randomCode.toString();
  109. }
  110. public void write(String path,String fileName) throws IOException {
  111. File folder = new File(path);
  112. if(!folder.exists()){
  113. folder.mkdirs();
  114. }
  115. OutputStream sos = new FileOutputStream(path+fileName);
  116. this.write(sos);
  117. }
  118. public void write(OutputStream sos) throws IOException {
  119. ImageIO.write(buffImg, "png", sos);
  120. sos.close();
  121. }
  122. public BufferedImage getBuffImg() {
  123. return buffImg;
  124. }
  125. public String getCode() {
  126. return code;
  127. }
  128. public static void main(String[] args) {
  129. ValidateCode vCode = new ValidateCode(120,40,5,50);
  130. try {
  131. String path="D:
  132. eportimagecode";
  133. System.out.println(vCode.getCode()+" >"+path);
  134. vCode.write(path,new Date().getTime()+".png");
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }

下面这个类主要是用作字体的设置,大家也可以直接拿过来用。

</>复制代码

  1. package com.ankang.tony.util;
  2. import java.awt.Font;
  3. import java.io.ByteArrayInputStream;
  4. public class ImgFontByte {
  5. public Font getFont(int fontHeight){
  6. try {
  7. Font baseFont = Font.createFont(Font.ITALIC, new ByteArrayInputStream(hex2byte(getFontByteStr())));
  8. return baseFont.deriveFont(Font.PLAIN, fontHeight);
  9. } catch (Exception e) {
  10. return new Font("Consola",Font.PLAIN, fontHeight);
  11. }
  12. }
  13. private byte[] hex2byte(String str) {
  14. if (str == null)
  15. return null;
  16. str = str.trim();
  17. int len = str.length();
  18. if (len == 0 || len % 2 == 1)
  19. return null;
  20. byte[] b = new byte[len / 2];
  21. try {
  22. for (int i = 0; i < str.length(); i += 2) {
  23. b[i/2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
  24. }
  25. return b;
  26. } catch (Exception e) {
  27. return null;
  28. }
  29. }
  30. /**
  31. * ttf字体文件的十六进制字符串
  32. * @return
  33. */
  34. private String getFontByteStr(){
  35. return null;
  36. }
  37. }

点我啦!赠送大疆无人机

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64991.html

相关文章

  • Java虚拟机:Java二进制字节码的结构、加载

    摘要:这个庞大的结构主要包含以下几个部分魔数和版本号基本的信息用于确定二进制字节码的特征和加载可行特征。 这篇文章的素材来自周志明的《深入理解Java虚拟机》。 作为Java开发人员,一定程度了解JVM虚拟机的的运作方式非常重要,本文就一些简单的虚拟机的相关概念和运作机制展开我自己的学习过程,是这个系列的第二篇。 我们在文件里写入了java的源代码,源代码写就后存入磁盘,磁盘上的源代码经过j...

    CHENGKANG 评论0 收藏0
  • ThinkPHP验证码不显示的解决方案

    摘要:今天遇到一个很奇怪的,就是我写了一个程序本地运行正常,但是发布到甲方的服务器上出现无法显示验证码的。适用于验证码图片在新窗口打开的时候显示了报错信息以及一堆乱码的情况下,如果有报错信息,请根据报错信息进行检查验证码所调用的字体是否缺失。 今天遇到一个很奇怪的BUG,就是我写了一个PHP程序本地运行正常,但是发布到甲方的服务器上出现无法显示验证码的BUG。 showImg(https:/...

    Meils 评论0 收藏0
  • node识别验证

    摘要:验证码的识别成功率跟图片质量关系密切,一般拿到后的验证码都得经过灰度化,二值化,去噪,利用就可以很方便的做到。 了解验证码 什么是验证码? 所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片,图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能,通俗说就是一种区分用户是计算机和人的公共全自动程序 验证码的作用 可以...

    levy9527 评论0 收藏0
  • 15分钟破解网站验证

    摘要:目前花费了两分钟。我这地方使用卷积神经网络,。这地方对卷积神经网络算法就不做详细介绍,感兴趣的同学,可以学习一下。 概述   很多开发者都讨厌网站的验证码,特别是写网络爬虫的程序员,而网站之所以设置验证码,是为了防止机器人访问网站,造成不必要的损失。现在好了,随着机器学习技术的发展,机器识别验证码的问题比较好解决了。 样本采集工具   这里我们采用wordpress的Really Si...

    hlcfan 评论0 收藏0

发表评论

0条评论

SKYZACK

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<