Java随机图片验证码

快来打我* 2023-06-22 13:50 146阅读 0赞

20191216165602151.png

  1. package com.ak.util;
  2. import javax.imageio.ImageIO;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.Random;
  12. /**
  13. * 验证码工具类
  14. *
  15. * @author Akeung
  16. * 2019/12/11 0011
  17. */
  18. public class ImageVerifyCodeUtil {
  19. private static final Random random = new Random();
  20. private static final String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312", "Georgia"};
  21. public static String drawImage(ByteArrayOutputStream output) {
  22. String code;
  23. int width = 60;
  24. int height = 30;
  25. //创建图片缓冲区
  26. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  27. Graphics2D g = bi.createGraphics();
  28. //设置背景颜色
  29. g.setBackground(new Color(255, 255, 255));
  30. g.clearRect(0, 0, width, height);
  31. StringBuilder stringBuilder = new StringBuilder();
  32. //这里只画入四个字符
  33. for (int i = 0; i < 4; i++) {
  34. String s = randomChar() + ""; //随机生成字符,因为只有画字符串的方法,没有画字符的方法,所以需要将字符变成字符串再画
  35. stringBuilder.append(s); //添加到StringBuilder里面
  36. float x = i * 1.0F * width / 4; //定义字符的x坐标
  37. g.setFont(randomFont()); //设置字体,随机
  38. g.setColor(randomColor()); //设置颜色,随机
  39. g.drawString(s, x, height-8);
  40. }
  41. code = stringBuilder.toString();//获取验证码字符串
  42. //定义干扰线的数量(5条)
  43. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  44. for (int i = 0; i < 5; i++) {
  45. int x1 = random.nextInt(width);
  46. int y1 = random.nextInt(height);
  47. int x2 = random.nextInt(width);
  48. int y2 = random.nextInt(height);
  49. graphics.setColor(randomColor());
  50. graphics.drawLine(x1, y1, x2, y2);
  51. }
  52. // 释放图形上下文
  53. g.dispose();
  54. try {
  55. ImageIO.write(bi, "png", output);
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. return code;
  60. }
  61. //随机字体
  62. private static Font randomFont() {
  63. int index = random.nextInt(fontNames.length);
  64. String fontName = fontNames[index];
  65. int style = random.nextInt(4); //随机获取4种字体的样式
  66. int size = random.nextInt(20) % 6 + 15; //随机获取字体的大小(15-20之间的值)
  67. return new Font(fontName, style, size);
  68. }
  69. //随机颜色
  70. private static Color randomColor() {
  71. int r = random.nextInt(225);
  72. int g = random.nextInt(225);
  73. int b = random.nextInt(225);
  74. return new Color(r, g, b);
  75. }
  76. //随机字符
  77. private static char randomChar() {
  78. //A-Z,a-z,0-9,可剔除一些难辨认的字母与数字
  79. String str = "0123456789ABCdefghiDEFGHIJopPQRVWXYZabcjklSTUmnqrstKLMNOvuwxyz";
  80. return str.charAt(random.nextInt(str.length()));
  81. }
  82. public static void main(String[] args) throws IOException {
  83. File file = new File("D://code.png");
  84. FileOutputStream fileOutputStream = new FileOutputStream(file);
  85. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  86. String s = drawImage(byteArrayOutputStream);
  87. System.out.println(s);
  88. byteArrayOutputStream.writeTo(fileOutputStream);
  89. byteArrayOutputStream.flush();
  90. byteArrayOutputStream.close();
  91. }
  92. }

发表评论

表情:
评论列表 (有 0 条评论,146人围观)

还没有评论,来说两句吧...

相关阅读