更改包名

This commit is contained in:
Cai1Cai1 2026-06-30 14:43:40 +08:00
parent 2530e84422
commit c7ff3998ec

View File

@ -1,15 +1,13 @@
package imglib
import (
"image"
"image/color"
"os"
"testing"
"github.com/golang/freetype"
"golang.org/x/image/draw"
"github.com/fogleman/gg"
)
// TestAddChineseWatermark 给桌面 kbk.png 添加中文文字水印
// TestAddChineseWatermark 给桌面 kbk.png 添加豆包AI风格斜纹平铺水印
func TestAddChineseWatermark(t *testing.T) {
srcPath := `C:\Users\Administrator\Desktop\kbk.png`
@ -19,35 +17,42 @@ func TestAddChineseWatermark(t *testing.T) {
}
bounds := srcImg.Bounds()
dst := image.NewRGBA(bounds)
draw.Draw(dst, bounds, srcImg, bounds.Min, draw.Src)
dc := gg.NewContext(bounds.Dx(), bounds.Dy())
dc.DrawImage(srcImg, 0, 0)
font, err := GetFont()
if err != nil {
t.Skipf("获取字体失败: %v", err)
fontPath := getDefaultFontPath()
if fontPath == "" {
t.Skip("未找到中文字体")
}
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(font)
c.SetClip(dst.Bounds())
c.SetDst(dst)
if err := dc.LoadFontFace(fontPath, 48); err != nil {
t.Fatalf("加载字体失败: %v", err)
}
fontSize := 36.0
c.SetFontSize(fontSize)
text := "这是一个水印"
x := bounds.Dx() - 280
y := bounds.Dy() - 60
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 180}))
pt := freetype.Pt(x, y)
if _, err := c.DrawString(text, pt); err != nil {
t.Fatalf("绘制水印失败: %v", err)
// 斜纹平铺水印
tileW, tileH := 500, 400
for y := -tileH; y < bounds.Dy()+tileH; y += tileH {
for x := -tileW; x < bounds.Dx()+tileW; x += tileW {
cx := float64(x) + 250
cy := float64(y) + 200
dc.Push()
dc.RotateAbout(-0.35, cx, cy)
dc.SetRGB(1, 1, 1)
dc.SetAlpha(0.25)
dc.DrawStringAnchored("这是一个水印", cx, cy, 0.5, 0.5)
dc.SetRGBA(1, 1, 1, 0.15)
dc.DrawStringAnchored("这是一个水印", float64(x)+250, float64(y)+200, 0.5, 0.5)
dc.Pop()
}
}
outPath := "d:\\source\\daShangDao_utils\\kbk_watermarked.png"
err = SavePNG(dst, outPath)
f, err := os.Create(outPath)
if err != nil {
t.Fatalf("创建文件失败: %v", err)
}
defer f.Close()
err = SavePNG(dc.Image(), outPath)
if err != nil {
t.Fatalf("保存图片失败: %v", err)
}