hutool.captcha.captcha

Module Contents

Classes

LineCaptcha

线段干扰验证码。

ArithmeticCaptcha

算术验证码。

CircleCaptcha

圆干扰验证码

CaptchaUtil

验证码工厂类。

API

class hutool.captcha.captcha.LineCaptcha(width: int = 100, height: int = 35, code_count: int = 4, line_count: int = 5)

线段干扰验证码。

在图片上绘制随机字符,并添加随机干扰线段以增加识别难度。

Initialization

初始化线段干扰验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

  • code_count – 验证码字符个数,默认4

  • line_count – 干扰线条数,默认5

create_code() str

生成随机验证码字符串并绘制验证码图片。

返回:

生成的验证码字符串

verify(code: str) bool

校验输入的验证码是否正确(不区分大小写)。

参数:

code – 用户输入的验证码

返回:

验证成功返回True,否则返回False

get_image_bytes(fmt: str = 'png') bytes

生成验证码图片的字节数据。

参数:

fmt – 图片格式,默认”png”

返回:

图片的字节数据

get_image_base64(fmt: str = 'png') str

获取验证码图片的Base64编码

参数:

fmt – 图片格式,默认”png”

返回:

Base64编码字符串

write(path: str, fmt: str = 'png') None

将验证码图片写入文件

参数:
  • path – 输出文件路径

  • fmt – 图片格式,默认”png”

class hutool.captcha.captcha.ArithmeticCaptcha(width: int = 100, height: int = 35)

算术验证码。

生成简单的数学表达式(如”3+5=?”),用户需计算结果。

Initialization

初始化算术验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

create_code() str

生成随机算术表达式,并计算结果。

返回:

生成的算术表达式(如”3+5=?”)

get_result() str

获取算术表达式的正确结果。

返回:

表达式结果字符串

verify(code: str) bool

校验用户输入的答案是否正确。

参数:

code – 用户输入的答案

返回:

答案正确返回True,否则返回False

get_image_bytes(fmt: str = 'png') bytes

生成验证码图片的字节数据。

参数:

fmt – 图片格式,默认”png”

返回:

图片的字节数据

get_image_base64(fmt: str = 'png') str

获取验证码图片的Base64编码

参数:

fmt – 图片格式,默认”png”

返回:

Base64编码字符串

write(path: str, fmt: str = 'png') None

将验证码图片写入文件

参数:
  • path – 输出文件路径

  • fmt – 图片格式,默认”png”

class hutool.captcha.captcha.CircleCaptcha(width: int = 100, height: int = 35, code_count: int = 4, circle_count: int = 3)

圆干扰验证码

在图片上绘制随机字符,并添加随机圆圈干扰。

Initialization

初始化圆干扰验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

  • code_count – 验证码字符个数,默认4

  • circle_count – 干扰圆圈数,默认3

create_code() str

生成随机验证码字符串并绘制验证码图片。

返回:

生成的验证码字符串

verify(code: str) bool

校验输入的验证码是否正确(不区分大小写)。

参数:

code – 用户输入的验证码

返回:

验证成功返回True,否则返回False

get_image_bytes(fmt: str = 'png') bytes

生成验证码图片的字节数据。

参数:

fmt – 图片格式,默认”png”

返回:

图片的字节数据

get_image_base64(fmt: str = 'png') str

获取验证码图片的Base64编码

参数:

fmt – 图片格式,默认”png”

返回:

Base64编码字符串

write(path: str, fmt: str = 'png') None

将验证码图片写入文件

参数:
  • path – 输出文件路径

  • fmt – 图片格式,默认”png”

class hutool.captcha.captcha.CaptchaUtil

验证码工厂类。

提供各种验证码的便捷创建方法。

static create_line_captcha(width: int = 100, height: int = 35, code_count: int = 4, line_count: int = 5) hutool.captcha.captcha.LineCaptcha

创建线段干扰验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

  • code_count – 验证码字符个数,默认4

  • line_count – 干扰线条数,默认5

返回:

LineCaptcha实例

static create_arithmetic_captcha(width: int = 100, height: int = 35) hutool.captcha.captcha.ArithmeticCaptcha

创建算术验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

返回:

ArithmeticCaptcha实例

static create_circle_captcha(width: int = 100, height: int = 35, code_count: int = 4, circle_count: int = 3) hutool.captcha.captcha.CircleCaptcha

创建圆干扰验证码。

参数:
  • width – 图片宽度,默认100像素

  • height – 图片高度,默认35像素

  • code_count – 验证码字符个数,默认4

  • circle_count – 干扰圆圈数,默认3

返回:

CircleCaptcha实例

static create_gif_captcha(width: int = 200, height: int = 80, code_count: int = 4, line_count: int = 5)

创建 GIF 动画验证码。

使用多帧图片模拟动态效果。返回的验证码对象包含 create_code()get_image_bytes()verify(code) 方法。

参数:
  • width – 图片宽度

  • height – 图片高度

  • code_count – 验证码字符个数

  • line_count – 干扰线条数

返回:

帧式验证码对象

static create_shear_captcha(width: int = 200, height: int = 80, code_count: int = 4, thickness: int = 1)

创建扭曲验证码。

使用圆干扰来模拟扭曲效果。返回的验证码对象包含 create_code()get_image_bytes()verify(code) 方法。

参数:
  • width – 图片宽度

  • height – 图片高度

  • code_count – 验证码字符个数

  • thickness – 干扰强度

返回:

圆干扰验证码对象