生成一个简单验证码

1 篇文章 0 订阅
package eofficialsite.utils

import org.apache.commons.lang.RandomStringUtils

import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.geom.Rectangle2D
import java.awt.image.BufferedImage

class CaptchaTools {
    private final static String CHAR_SET = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ23456789"
    private static final int RANDOM_STR_LENGTH = 6
    static def newCaptcha() {
        final int height = 150
        final int width = 200
        final int fontSize = 30

        final int bottomPadding = 16
        final int lineSpacing = 10
        // Generate the CAPTCHA solution
        String captchaSolution = generateRandomString()
        System.setProperty("java.awt.headless", "true")
        BufferedImage captchaImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
        Font font = new Font("Serif", Font.BOLD, fontSize)

        Graphics2D g2d = createGraphic(captchaImage, font)
        Rectangle2D fontRect = font.getStringBounds(captchaSolution, g2d.fontRenderContext)

        // Create a graphic "space" pixels wider and taller than the the font
        captchaImage = new BufferedImage((int) fontRect.width + bottomPadding, (int) fontRect.height + bottomPadding, BufferedImage.TYPE_INT_RGB)
        g2d = createGraphic(captchaImage, font)
        // Draw the background
        g2d.color = Color.WHITE
        g2d.fillRect(0, 0, width, height)
        // Draw the lines
        g2d.color = Color.GRAY
        int y1 = lineSpacing
        int x2 = lineSpacing
        int x1 = 0
        int y2 = 0
        while (x1 < width || x2 < width || y1 < height || y2 < height) {
            g2d.drawLine(x1, y1, x2, y2)
            if (y1 < height) {
                x1 = 0
                y1 += lineSpacing
            } else if (x1 < width) {
                y1 = height
                x1 += lineSpacing
            } else {
                x1 = width
                y1 = height
            }
            if (x2 < width) {
                y2 = 0
                x2 += lineSpacing
            } else if (y2 < height) {
                x2 = width
                y2 += lineSpacing
            } else {
                y2 = height
                x2 = width
            }
        }
        // Draw the String
        g2d.color = Color.BLACK
        g2d.drawString(captchaSolution, (int) (bottomPadding / 2), (int) (bottomPadding / 4) + (int) fontRect.height)
        return [value: captchaSolution, image: captchaImage]
    }

    private static Graphics2D createGraphic(BufferedImage image, Font font) {
        Graphics2D g2d = image.createGraphics()
        g2d.font = font
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
        g2d
    }

    static String generateRandomString(int length = RANDOM_STR_LENGTH){
        return  RandomStringUtils.random(length, CHAR_SET.toCharArray())
    }
}


上面的这个类是生成验证码的工具类,调用方式如下:

def captcha = CaptchaTools.newCaptcha()
ImageIO.write(captcha.image, "PNG", response.outputStream)

如果需要图片为base64位的,那么采用下面的方式:

  def captcha = CaptchaTools.newCaptcha()
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
  ImageIO.write(captcha.image, "PNG", outputStream)
  def base64Img = new BASE64Encoder().encode(outputStream.toByteArray())
  render base64Img

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值