feat(Quick): 优化RegionCameraView裁剪算法

重构RegionImageCropper裁剪逻辑,改用纯几何aspect-fill反算方法,
将屏上小框坐标直接映射到照片像素rect,避免使用
metadataOutputRectConverted导致的坐标轴对调问题。

主要变更:
- 移除基于归一化rect的裁剪方式
- 新增cropRect函数进行几何反算
- 修复传感器横向坐标与竖屏照片方向不一致的问题
- 保持裁剪精度的同时提升算法稳定性
```
This commit is contained in:
link2026
2026-05-31 23:51:53 +08:00
parent d72a1fec17
commit 32e7c25ed7
2 changed files with 100 additions and 16 deletions

View File

@@ -0,0 +1,61 @@
import XCTest
import CoreGraphics
@testable import
///
/// :, > rect
/// `metadataOutputRectConverted`() x/y ,
/// (2026-05-31 bug)
final class RegionImageCropperTests: XCTestCase {
/// + + rect ( > )
func testWideBoxYieldsLandscapeCropRect() {
let photo = CGSize(width: 3024, height: 4032) // ()
let view = CGSize(width: 393, height: 852) // ()
let box = RegionFraming.box(in: view) // 84% / 160
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
XCTAssertGreaterThan(rect.width, rect.height,
"宽框应裁出宽 rect;若 width<height 说明轴对调 bug 复现")
XCTAssertGreaterThan(rect.width, 0)
XCTAssertGreaterThan(rect.height, 0)
}
/// rect ()
func testCropRectStaysInsidePhotoBounds() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let box = RegionFraming.box(in: view)
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
XCTAssertGreaterThanOrEqual(rect.minX, 0)
XCTAssertGreaterThanOrEqual(rect.minY, 0)
XCTAssertLessThanOrEqual(rect.maxX, photo.width)
XCTAssertLessThanOrEqual(rect.maxY, photo.height)
}
/// rect (aspect-fill ,)
func testCropRectPreservesBoxAspectRatio() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let box = RegionFraming.box(in: view)
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
let boxAspect = box.width / box.height
let rectAspect = rect.width / rect.height
XCTAssertEqual(rectAspect, boxAspect, accuracy: 0.05,
"等比映射下,裁剪 rect 宽高比应与屏上小框一致")
}
/// 退:, .zero
func testZeroInputsReturnZero() {
XCTAssertEqual(
RegionImageCropper.cropRect(photoPixelSize: .zero,
box: CGRect(x: 0, y: 0, width: 10, height: 10),
in: CGSize(width: 100, height: 200)),
.zero)
}
}