验证码与base64转换的工具类

谁践踏了优雅 2022-11-27 00:56 355阅读 0赞

验证码与base64转换的工具类

    • 1、关于验证码的工具类
    • 2、关于stringBase64与图像的互转
    • 3、验证码效果图

1、关于验证码的工具类

  1. package com.test.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. /** * 验证码生成工具 */
  8. public class Captcha {
  9. private BufferedImage image;// 图像
  10. private String captcha;// 验证码
  11. private static char code[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray();
  12. public static final String SESSION_CODE_NAME = "code";
  13. private Captcha() {
  14. init();// 初始化属性
  15. }
  16. /* * 取得RandomNumUtil实例 */
  17. public static Captcha Instance() {
  18. return new Captcha();
  19. }
  20. /* * 取得验证码图片 */
  21. public BufferedImage getImage() {
  22. return this.image;
  23. }
  24. /* * 取得图片的验证码 */
  25. public String getString() {
  26. return this.captcha;
  27. }
  28. private void init() {
  29. // 在内存中创建图象
  30. int width = 170, height = 40;
  31. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  32. // 获取图形上下文
  33. Graphics graphics = image.getGraphics();
  34. // 生成随机类
  35. Random random = new Random();
  36. // 设定背景色
  37. graphics.setColor(getRandColor(245, 250));
  38. graphics.fillRect(0, 0, width, height);
  39. // 设定字体
  40. graphics.setFont(new Font("Times New Roman", Font.PLAIN, 36));
  41. for (int i = 0; i < 500; i++) {
  42. // 随机产生500条干扰线,使图象中的认证码不易被其它程序探测到
  43. graphics.setColor(getRandColor(150, 255));
  44. int x1 = random.nextInt(width);
  45. int y1 = random.nextInt(height);
  46. int x2 = random.nextInt(12);
  47. int y2 = random.nextInt(12);
  48. graphics.drawLine(x1, y1, x1 + x2, y1 + y2);
  49. }
  50. // 取随机产生的认证码(4位数字)
  51. String sRand = "";
  52. for (int i = 0; i < 4; i++) {
  53. String rand = String.valueOf(code[random.nextInt(code.length)]);
  54. sRand += rand;
  55. // 将认证码显示到图象中
  56. graphics.setColor(new Color(1 + random.nextInt(255), 1 + random.nextInt(255), 1 + random.nextInt(255)));
  57. // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  58. graphics.drawString(rand, 26 * i + 40, 32);
  59. }
  60. // 赋值验证码
  61. this.captcha = sRand;
  62. // 图象生效
  63. graphics.dispose();
  64. this.image = image;// 赋值图像
  65. }
  66. /* * 给定范围获得随机颜色 */
  67. private Color getRandColor(int start, int end) {
  68. Random random = new Random();
  69. if (start > 255)
  70. start = 255;
  71. if (end > 255)
  72. end = 255;
  73. int r = start + random.nextInt(end - start);
  74. int g = start + random.nextInt(end - start);
  75. int b = start + random.nextInt(end - start);
  76. return new Color(r, g, b);
  77. }
  78. }

2、关于stringBase64与图像的互转

  1. package com.test.utils;
  2. import sun.misc.BASE64Decoder;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.RenderedImage;
  6. import java.io.*;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.Base64;
  9. import javax.imageio.ImageIO;
  10. import javax.swing.*;
  11. public class Base64ToString {
  12. //把图片转换成string类型
  13. public static String imgToBase64String(final RenderedImage img) {
  14. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  15. try {
  16. ImageIO.write(img, "jpg", Base64.getEncoder().wrap(os));
  17. return os.toString(StandardCharsets.ISO_8859_1.name());
  18. } catch (final IOException ioe) {
  19. throw new UncheckedIOException(ioe);
  20. }
  21. }
  22. //把string类型转换成图片
  23. public static BufferedImage base64StringToImg(final String base64String) {
  24. try {
  25. BASE64Decoder decoder = new BASE64Decoder();
  26. byte[] bytes = decoder.decodeBuffer(base64String);
  27. ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  28. return ImageIO.read(bais);
  29. } catch (final IOException ioe) {
  30. throw new UncheckedIOException(ioe);
  31. }
  32. }
  33. //绘制图片
  34. public static void draw_image(BufferedImage image_buffer) throws IOException {
  35. ImageIcon icon = new ImageIcon(image_buffer);
  36. JFrame frame = new JFrame();
  37. frame.setLayout(new FlowLayout());
  38. frame.setSize(image_buffer.getWidth(), image_buffer.getHeight());
  39. JLabel lbl = new JLabel();
  40. lbl.setIcon(icon);
  41. frame.add(lbl);
  42. frame.setVisible(true);
  43. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. }
  45. }

3、验证码效果图

在这里插入图片描述

发表评论

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

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

相关阅读