From 9fbcd9049af51cbf7870dd817bacbedab5a7d25f Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 2 Jul 2026 13:17:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8D=B0=E7=AB=A0=E5=9B=BE=E5=83=8F):=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8D=B0=E7=AB=A0=E5=9B=BE=E7=89=87=E7=99=BD?= =?UTF-8?q?=E8=89=B2=E5=BA=95=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_renderer.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ofdgo_renderer.go b/ofdgo_renderer.go index 0c33e63..2665a65 100644 --- a/ofdgo_renderer.go +++ b/ofdgo_renderer.go @@ -1380,6 +1380,7 @@ func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) { if len(s.Data) > 0 { img, _, err := image.Decode(bytes.NewReader(s.Data)) if err == nil { + img = stampImageWithTransparentWhite(img) ctx.Push() ctx.Translate(x, screenY) 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 // 入参: obj 路径对象, pageH 页面高度, ctm 变换矩阵, boundaryInCTM 边界是否参与CTM变换 // 返回: *canvas.Path 路径对象