feat(印章图像): 处理印章图片白色底色

This commit is contained in:
2026-07-02 13:17:14 +08:00
parent 75c9c17d1d
commit 9fbcd9049a
+32
View File
@@ -1380,6 +1380,7 @@ func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) {
if len(s.Data) > 0 { if len(s.Data) > 0 {
img, _, err := image.Decode(bytes.NewReader(s.Data)) img, _, err := image.Decode(bytes.NewReader(s.Data))
if err == nil { if err == nil {
img = stampImageWithTransparentWhite(img)
ctx.Push() ctx.Push()
ctx.Translate(x, screenY) ctx.Translate(x, screenY)
ctx.Scale(w/float64(img.Bounds().Dx()), h/float64(img.Bounds().Dy())) ctx.Scale(w/float64(img.Bounds().Dx()), h/float64(img.Bounds().Dy()))
@@ -1390,6 +1391,37 @@ func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) {
} }
} }
// stampImageWithTransparentWhite 处理印章图片白色底色
// 入参: img 印章图片对象
// 返回: image.Image 处理后的印章图片对象
func stampImageWithTransparentWhite(img image.Image) image.Image {
bounds := img.Bounds()
out := image.NewNRGBA(bounds)
hasAlpha := false
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
if c.A < 255 {
hasAlpha = true
}
out.SetNRGBA(x, y, c)
}
}
if hasAlpha {
return img
}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := out.NRGBAAt(x, y)
if c.R >= 250 && c.G >= 250 && c.B >= 250 {
c.A = 0
out.SetNRGBA(x, y, c)
}
}
}
return out
}
// buildPath 解析路径并返回Canvas Path // buildPath 解析路径并返回Canvas Path
// 入参: obj 路径对象, pageH 页面高度, ctm 变换矩阵, boundaryInCTM 边界是否参与CTM变换 // 入参: obj 路径对象, pageH 页面高度, ctm 变换矩阵, boundaryInCTM 边界是否参与CTM变换
// 返回: *canvas.Path 路径对象 // 返回: *canvas.Path 路径对象