From 6bd0fcaf9b30a238c269ec314946d04dda998721 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Sun, 9 Aug 2026 04:58:41 +0000 Subject: [PATCH] =?UTF-8?q?fix(captcha2):=20=E4=BF=AE=E5=A4=8D=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E5=AD=97=E7=AC=A6=E5=9E=82=E7=9B=B4=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E9=94=99=E8=AF=AF=E5=AF=BC=E8=87=B4=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=B8=8D=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit generate_image() 中 y_dist(2,12) 在 Python 原版是顶部留白语义, C++ 重写时误用为字符中心 y 坐标(cy = y - rh/2), 旋转后位图高度 rh 约 25~46px,cy 为负值导致字符上半部被裁掉。 修复:恢复 y 为顶部留白语义(cy = y),加边界钳制确保字符完整 落在 160×50 画布内。同步修复 test_gen.cpp 中的同款代码。 --- captcha2/src/main.cpp | 7 +++++-- captcha2/test_gen.cpp | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/captcha2/src/main.cpp b/captcha2/src/main.cpp index a40e645..3c8737b 100644 --- a/captcha2/src/main.cpp +++ b/captcha2/src/main.cpp @@ -451,9 +451,12 @@ static std::vector generate_image(const std::string& text, const std::s int rw, rh; auto rotated = rotate_bitmap(bmp, angle, rw, rh); - // 合成位置:目标中心对准期望位置 + // 合成位置:水平居中于 x,y 为顶部留白(与 Python 版语义一致) + // 底部越界时上移钳制,保证字符完整落在画布内 int cx = x - rw / 2; - int cy = y - rh / 2; + int cy = y; + if (cy + rh > kImgHeight) cy = kImgHeight - rh; + if (cy < 0) cy = 0; composite_char(pixels, rotated, rw, rh, cx, cy, fg); } diff --git a/captcha2/test_gen.cpp b/captcha2/test_gen.cpp index dae8298..995f7d0 100644 --- a/captcha2/test_gen.cpp +++ b/captcha2/test_gen.cpp @@ -169,7 +169,10 @@ static std::vector generate_image(const std::string& text, const std::s int rw, rh, ox, oy; auto rotated = rotate_bitmap(bmp, angle, rw, rh, ox, oy); - int cx = x - rw / 2, cy = y - rh / 2; + int cx = x - rw / 2; + int cy = y; + if (cy + rh > kImgHeight) cy = kImgHeight - rh; + if (cy < 0) cy = 0; composite_char(pixels, rotated, rw, rh, cx, cy, fg); }