2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
draw.go 3.38 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-04 11:45 . upd:目录结构调整
/**
* @Author Awen
* @Date 2024/06/01
* @Email wengaolng@gmail.com
**/
package slide
import (
"image"
"gitee.com/linqwen/momo/app/rbac/captcha/base/canvas"
"gitee.com/linqwen/momo/app/rbac/captcha/base/randgen"
"golang.org/x/image/draw"
)
// DrawImageParams .
type DrawImageParams struct {
Width int
Height int
Background image.Image
Alpha float32
CaptchaDrawBlocks []*DrawBlock
}
// DrawTplImageParams .
type DrawTplImageParams struct {
X int
Y int
Width int
Height int
Background image.Image
MaskImage image.Image
Alpha float32
CaptchaDrawBlock *DrawBlock
}
// DrawImage .
type DrawImage interface {
DrawWithNRGBA(params *DrawImageParams) (img image.Image, bgImg image.Image, err error)
DrawWithTemplate(params *DrawTplImageParams) (image.Image, error)
}
var _ DrawImage = (*drawImage)(nil)
// NewDrawImage .
func NewDrawImage() DrawImage {
return &drawImage{}
}
// drawImage .
type drawImage struct {
}
// DrawWithTemplate is drawing with a template
func (d *drawImage) DrawWithTemplate(params *DrawTplImageParams) (image.Image, error) {
block := params.CaptchaDrawBlock
bgImage := params.Background
cvs := canvas.CreateNRGBACanvas(params.Width, params.Height, true)
bgCvs := canvas.CreateNRGBACanvas(params.Width, params.Height, true)
tplImage, err := d.drawGraphImage(params.Width, params.Height, params.MaskImage)
if err != nil {
return nil, err
}
draw.Draw(bgCvs.Get(), bgCvs.Bounds(), bgImage, image.Pt(block.X, block.Y), draw.Src)
draw.DrawMask(cvs.Get(), tplImage.Bounds(), bgCvs.Get(), image.Point{}, tplImage, image.Point{}, draw.Over)
maskImage, err := d.drawGraphImage(params.Width, params.Height, block.Image)
if err != nil {
return nil, err
}
draw.Draw(cvs.Get(), maskImage.Bounds(), maskImage, image.Point{}, draw.Over)
return cvs, nil
}
// DrawWithNRGBA is drawing with a NRGBA
func (d *drawImage) DrawWithNRGBA(params *DrawImageParams) (img image.Image, bgImg image.Image, err error) {
blocks := params.CaptchaDrawBlocks
cvs := canvas.CreateNRGBACanvas(params.Width, params.Height, true)
for i := 0; i < len(blocks); i++ {
block := blocks[i]
var graphImage canvas.NRGBA
graphImage, err = d.drawGraphImage(block.Width, block.Height, block.Image)
if err != nil {
return nil, nil, err
}
graphBounds := graphImage.Bounds()
draw.Draw(cvs.Get(), image.Rect(block.X, block.Y, block.X+graphBounds.Dx(), block.Y+graphBounds.Dy()), graphImage.Get(), image.Point{}, draw.Over)
}
var rcm = canvas.CreateNRGBACanvas(params.Width, params.Height, true)
if params.Background != nil {
bgImage := params.Background
b := bgImage.Bounds()
m := canvas.CreateNRGBACanvas(b.Dx(), b.Dy(), true)
point := randgen.RangCutImagePos(params.Width, params.Height, bgImage)
draw.Draw(m.Get(), b, bgImage, point, draw.Src)
m.SubImage(image.Rect(0, 0, params.Width, params.Height))
draw.Draw(rcm.Get(), rcm.Bounds(), m.Get(), image.Point{}, draw.Over)
draw.Draw(m.Get(), cvs.Bounds(), cvs, image.Point{}, draw.Over)
return m.Get(), rcm, nil
}
return cvs, rcm, nil
}
// drawGraphImage is drawing graphics
func (d *drawImage) drawGraphImage(width, height int, img image.Image) (canvas.NRGBA, error) {
cvs := canvas.CreateNRGBACanvas(width, height, true)
draw.BiLinear.Scale(cvs.Get(), cvs.Bounds(), img, img.Bounds(), draw.Over, nil)
return cvs, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.1.20

搜索帮助