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); }