From 00baf6574a85ae7b29c541593e6871d92960942c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=99=9E=E6=B8=A0=E6=88=90=E2=80=9D?= <“yuqucheng2006@qq.com”> Date: Thu, 23 Jul 2026 16:09:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(scanQRcode):=20=E4=BF=AE=E6=AD=A3=E6=89=AB?= =?UTF-8?q?=E7=A0=81=E9=A1=B5=E9=9D=A2=E8=A3=81=E5=88=87=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整了中心裁切区域的计算参数和缩放系数,保证画面不变形同时优化裁切逻辑注释 --- .../UI/Pages/ConnectDevice/ScanQRcode.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/UI/Pages/ConnectDevice/ScanQRcode.cs b/Assets/Scripts/UI/Pages/ConnectDevice/ScanQRcode.cs index d54be80..3560576 100644 --- a/Assets/Scripts/UI/Pages/ConnectDevice/ScanQRcode.cs +++ b/Assets/Scripts/UI/Pages/ConnectDevice/ScanQRcode.cs @@ -277,15 +277,16 @@ namespace Kill.UI.Pages // 预览框横向 1080x680 // 从旋转后的 1080x1920 中截取中间区域并放大 - // 计算中心裁切区域大小(基于 centerCropRatio) - int sourceCropH = Mathf.Max(1, Mathf.RoundToInt(camW * centerCropRatio)); - int sourceCropW = Mathf.Max(1, Mathf.RoundToInt(camH * centerCropRatio)); + // 计算中心裁切区域大小 + // centerCropRatio=1.0 时等于原始裁切(取 targetH 列 / 所有行),无缩放 + // centerCropRatio<1.0 时取更小区域放大,值越小放大倍数越大 + int sourceCropH = Mathf.Max(1, Mathf.RoundToInt(targetH * centerCropRatio)); + int sourceCropW = Mathf.Max(1, Mathf.RoundToInt(targetW * centerCropRatio)); int cropY = (camW - sourceCropH) / 2; int cropX = (camH - sourceCropW) / 2; - // 计算缩放比例 - float scaleY = (float)sourceCropH / targetH; - float scaleX = (float)sourceCropW / targetW; + // 两个方向缩放一致,保证不变形 + float scale = centerCropRatio; // 获取相机像素数据 Color32[] cameraPixels = webCamTexture.GetPixels32(); @@ -293,14 +294,14 @@ namespace Kill.UI.Pages // 顺时针旋转90度 + 中心裁切 + 缩放 // 目标像素 (x, y) 对应源坐标: - // srcX = cropY + (targetH-1-y) * scaleY (裁切区域内纵向缩放) - // srcY = cropX + x * scaleX (裁切区域内横向缩放) + // srcX = cropY + (targetH-1-y) * scale (裁切区域内纵向缩放) + // srcY = cropX + x * scale (裁切区域内横向缩放) for (int y = 0; y < targetH; y++) { for (int x = 0; x < targetW; x++) { - float srcX = cropY + (targetH - 1 - y) * scaleY; - float srcY = cropX + x * scaleX; + float srcX = cropY + (targetH - 1 - y) * scale; + float srcY = cropX + x * scale; // 双线性插值采样 int sx = (int)srcX;