点选式验证码
随机生成文本或图形,并按照顺序点选对应内容进行校验。
安装
shell
$ go get -u github.com/wenlng/go-captcha/v2@latest
$ go get -u github.com/wenlng/go-captcha/v2@latest
安装预置内嵌素材资源
shell
$ go get -u github.com/wenlng/go-captcha-assets@latest
$ go get -u github.com/wenlng/go-captcha-assets@latest
实例
go
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/bindata/chars"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
var textCapt click.Captcha
func init() {
builder := click.NewBuilder()
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
log.Fatalln(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
textCapt = builder.Make()
}
func main() {
captData, err := textCapt.Generate()
if err != nil {
log.Fatalln(err)
}
dotData := captData.GetData()
if dotData == nil {
log.Fatalln(">>>>> generate err")
}
dots, _ := json.Marshal(dotData)
fmt.Println(">>>>> ", string(dots))
err = captData.GetMasterImage().SaveToFile("./.caches/master.jpg", option.QualityNone)
if err != nil {
fmt.Println(err)
}
err = captData.GetThumbImage().SaveToFile("./.caches/thumb.png")
if err != nil {
fmt.Println(err)
}
}
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/bindata/chars"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
var textCapt click.Captcha
func init() {
builder := click.NewBuilder()
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
log.Fatalln(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
textCapt = builder.Make()
}
func main() {
captData, err := textCapt.Generate()
if err != nil {
log.Fatalln(err)
}
dotData := captData.GetData()
if dotData == nil {
log.Fatalln(">>>>> generate err")
}
dots, _ := json.Marshal(dotData)
fmt.Println(">>>>> ", string(dots))
err = captData.GetMasterImage().SaveToFile("./.caches/master.jpg", option.QualityNone)
if err != nil {
fmt.Println(err)
}
err = captData.GetThumbImage().SaveToFile("./.caches/thumb.png")
if err != nil {
fmt.Println(err)
}
}
使用
创建实例
- builder.Make() 中文文本、字母/数字混合 点选
- builder.MakeWithShape() 图形点选
配置
TIP
配置举例:click.NewBuilder(click.WithXxxx(), click.WithXxxx(), ....) 或 builder.SetOptions(click.WithXxxx(), click.WithXxxx(), ....)
主图配置
- click.WithImageSize(option.Size) 设置主图尺寸,默认 300x220
- click.WithRangeLen(option.RangeVal) 设置随机内容长度范围
- click.WithRangeAnglePos([]option.RangeVal) 设置随机角度范围
- click.WithRangeSize(option.RangeVal) 设置随机内容大小范围
- click.WithRangeColors([]string) 设置随机颜色
- click.WithDisplayShadow(bool) 设置是否显示阴影
- click.WithShadowColor(string) 设置阴影颜色
- click.WithShadowPoint(option.Point) 设置阴影偏移位置
- click.WithImageAlpha(float32) 设置主图透明度
- click.WithUseShapeOriginalColor(bool) 设置是否使用图形原始颜色,"图形点选"有效
缩略图
- click.WithThumbImageSize(option.Size) 设置缩略尺寸,默认 150x40
- click.WithRangeVerifyLen(option.RangeVal) 设置校验内容的随机长度范围
- click.WithDisabledRangeVerifyLen(bool) 禁用校验内容的随机长度,与主图内容的长度保持一致
- click.WithRangeThumbSize(option.RangeVal) 设置随机缩略内容随机大小范围
- click.WithRangeThumbColors([]string) 设置缩略随机颜色范围
- click.WithRangeThumbBgColors([]string) 设置缩略随机背景颜色范围
- click.WithIsThumbNonDeformAbility(bool) 设置缩略图内容不变形,不受背景影响
- click.WithThumbBgDistort(int) 设置缩略图背景扭曲 option.DistortLevel1 (1-5)
- click.WithThumbBgCirclesNum(int) 设置缩略图绘制小圆点数量
- click.WithThumbBgSlimLineNum(int) 设置缩略图绘制线条数量
设置资源
TIP
配置举例:builder.SetResources(click.WithXxxx(), click.WithXxxx(), ....)
- click.WithChars([]string) 设置文本种子
- click.WithShapes(map[string]image.Image) 设置图形种子
- click.WithFonts([]*truetype.Font) 设置字体
- click.WithBackgrounds([]image.Image) 设置主图背景
- click.WithThumbBackgrounds([]image.Image) 设置缩略图背景
验证码数据
- GetData() map[int]*Dot 获取当前校验的信息
- GetMasterImage() imagedata.JPEGImageData 获取主图
- GetThumbImage() imagedata.PNGImageData 获取缩略图
关于验证码 "imagedata" API可参考 验证码图像。