Files
kangkang/康康/Features/Quick/RegionCameraView.swift
link2026 32e7c25ed7 ```
feat(Quick): 优化RegionCameraView裁剪算法

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

主要变更:
- 移除基于归一化rect的裁剪方式
- 新增cropRect函数进行几何反算
- 修复传感器横向坐标与竖屏照片方向不一致的问题
- 保持裁剪精度的同时提升算法稳定性
```
2026-05-31 23:51:53 +08:00

373 lines
14 KiB
Swift

import SwiftUI
import AVFoundation
import UIKit
import Combine
/// ·
/// + + **** UIImage
/// (,QuickRegionCaptureFlow 退 PhotoPicker)
///
/// : bake `.up`(), aspect-fill
/// (view ) rect( `RegionImageCropper`)
/// `metadataOutputRectConverted` ,
struct RegionCameraView: View {
let onCapture: (UIImage) -> Void
let onCancel: () -> Void
@StateObject private var controller = RegionCameraController()
@State private var authState: AuthState = .checking
@State private var isCapturing = false
@State private var flash = false
enum AuthState { case checking, authorized, denied }
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
switch authState {
case .checking:
ProgressView().tint(.white)
case .denied:
deniedView
case .authorized:
cameraStack
}
if flash {
Color.white.ignoresSafeArea().transition(.opacity)
}
}
.task { await resolveAuth() }
}
// MARK: - + +
private var cameraStack: some View {
GeometryReader { proxy in
let box = RegionFraming.box(in: proxy.size)
ZStack {
RegionCameraPreview(controller: controller)
.ignoresSafeArea()
// (even-odd ),
Canvas { ctx, size in
var path = Path(CGRect(origin: .zero, size: size))
path.addPath(Path(roundedRect: box, cornerRadius: Tj.Radius.md))
ctx.fill(path, with: .color(.black.opacity(0.5)), style: FillStyle(eoFill: true))
}
.ignoresSafeArea()
.allowsHitTesting(false)
//
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
.strokeBorder(Color.white.opacity(0.95),
style: StrokeStyle(lineWidth: 2, dash: [8, 6]))
.frame(width: box.width, height: box.height)
.position(x: box.midX, y: box.midY)
.allowsHitTesting(false)
//
Text("把异常项放进框里 · 对准一两行")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.background(Capsule().fill(.black.opacity(0.4)))
.position(x: box.midX, y: box.minY - 22)
.allowsHitTesting(false)
controlsOverlay
}
}
}
private var controlsOverlay: some View {
VStack {
HStack {
Button {
onCancel()
} label: {
Text("取消")
.font(.system(size: 16, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.background(Capsule().fill(.black.opacity(0.35)))
}
Spacer()
}
.padding(.horizontal, 18)
.padding(.top, 8)
Spacer()
shutterButton
.padding(.bottom, 36)
}
}
private var shutterButton: some View {
Button {
capture()
} label: {
ZStack {
Circle().fill(.white).frame(width: 72, height: 72)
Circle().strokeBorder(.white.opacity(0.6), lineWidth: 3).frame(width: 84, height: 84)
if isCapturing {
ProgressView().tint(.black)
}
}
}
.disabled(isCapturing)
.accessibilityLabel("拍摄异常项")
}
private var deniedView: some View {
VStack(spacing: 16) {
Image(systemName: "camera.fill")
.font(.system(size: 40))
.foregroundStyle(.white.opacity(0.8))
Text("相机权限未开启")
.font(.tjH2())
.foregroundStyle(.white)
Text("异常项快拍需要相机。去「设置 → 康康 → 相机」打开后再回来。")
.font(.system(size: 13))
.foregroundStyle(.white.opacity(0.7))
.multilineTextAlignment(.center)
.padding(.horizontal, 36)
HStack(spacing: 12) {
Button("取消") { onCancel() }
.font(.system(size: 15))
.foregroundStyle(.white)
.padding(.horizontal, 18).padding(.vertical, 10)
.background(Capsule().strokeBorder(.white.opacity(0.5), lineWidth: 1))
Button("去设置") {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(.black)
.padding(.horizontal, 18).padding(.vertical, 10)
.background(Capsule().fill(.white))
}
}
}
// MARK: -
private func capture() {
guard !isCapturing else { return }
isCapturing = true
withAnimation(.easeOut(duration: 0.08)) { flash = true }
controller.capture { image in
withAnimation(.easeIn(duration: 0.15)) { flash = false }
isCapturing = false
guard let image else { return }
onCapture(image)
}
}
private func resolveAuth() async {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
authState = .authorized
case .notDetermined:
let granted = await AVCaptureDevice.requestAccess(for: .video)
authState = granted ? .authorized : .denied
default:
authState = .denied
}
}
}
// MARK: - (UIView SwiftUI ,)
enum RegionFraming {
/// 84% , 160 28%
static func box(in size: CGSize) -> CGRect {
guard size.width > 0, size.height > 0 else { return .zero }
let w = size.width * 0.84
let h = min(160, size.height * 0.28)
let x = (size.width - w) / 2
let y = (size.height - h) / 2 - size.height * 0.06
return CGRect(x: x, y: y, width: w, height: h)
}
}
// MARK: -
enum RegionImageCropper {
/// (view ) `.resizeAspectFill` `.up` rect
/// : aspect-fill viewSize,
/// `metadataOutputRectConverted`(****,
/// x/y ,, RegionImageCropperTests)
static func cropRect(photoPixelSize p: CGSize, box: CGRect, in viewSize: CGSize) -> CGRect {
guard p.width > 0, p.height > 0, viewSize.width > 0, viewSize.height > 0 else { return .zero }
// aspect-fill:,
let scale = max(viewSize.width / p.width, viewSize.height / p.height)
let scaledW = p.width * scale
let scaledH = p.height * scale
// ,
let ox = (viewSize.width - scaledW) / 2
let oy = (viewSize.height - scaledH) / 2
// :,
var x = (box.minX - ox) / scale
var y = (box.minY - oy) / scale
var w = box.width / scale
var h = box.height / scale
//
x = max(0, min(p.width, x))
y = max(0, min(p.height, y))
w = max(0, min(p.width - x, w))
h = max(0, min(p.height - y, h))
return CGRect(x: x, y: y, width: w, height: h).integral
}
/// `.up` (`box` / `viewSize` view );退
static func crop(_ image: UIImage, box: CGRect, viewSize: CGSize) -> UIImage {
guard let cg = image.cgImage else { return image }
let rect = cropRect(photoPixelSize: CGSize(width: cg.width, height: cg.height),
box: box, in: viewSize)
guard rect.width >= 1, rect.height >= 1, let cropped = cg.cropping(to: rect) else { return image }
return UIImage(cgImage: cropped, scale: image.scale, orientation: .up)
}
}
extension UIImage {
/// EXIF bake , `.up` ,便 rect CGImage
func normalizedUp() -> UIImage {
if imageOrientation == .up { return self }
let format = UIGraphicsImageRendererFormat.default()
format.scale = scale
let renderer = UIGraphicsImageRenderer(size: size, format: format)
return renderer.image { _ in draw(in: CGRect(origin: .zero, size: size)) }
}
}
// MARK: - AVFoundation
/// SwiftUI ,(weak UIView)
final class RegionCameraController: ObservableObject {
weak var view: RegionPreviewUIView?
func capture(_ completion: @escaping (UIImage?) -> Void) {
guard let view else { completion(nil); return }
view.capture(completion: completion)
}
}
struct RegionCameraPreview: UIViewRepresentable {
let controller: RegionCameraController
func makeUIView(context: Context) -> RegionPreviewUIView {
let v = RegionPreviewUIView()
controller.view = v
return v
}
func updateUIView(_ uiView: RegionPreviewUIView, context: Context) {}
static func dismantleUIView(_ uiView: RegionPreviewUIView, coordinator: ()) {
uiView.stop()
}
}
/// + ,
final class RegionPreviewUIView: UIView, AVCapturePhotoCaptureDelegate {
private let session = AVCaptureSession()
private let output = AVCapturePhotoOutput()
private var previewLayer: AVCaptureVideoPreviewLayer?
private var setupDone = false
private var captureCompletion: ((UIImage?) -> Void)?
override func didMoveToWindow() {
super.didMoveToWindow()
guard !setupDone, window != nil else { return }
setupDone = true
configure()
}
private func configure() {
session.beginConfiguration()
session.sessionPreset = .photo
guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back),
let input = try? AVCaptureDeviceInput(device: device),
session.canAddInput(input) else {
session.commitConfiguration()
return
}
session.addInput(input)
if session.canAddOutput(output) { session.addOutput(output) }
session.commitConfiguration()
let preview = AVCaptureVideoPreviewLayer(session: session)
preview.videoGravity = .resizeAspectFill
preview.frame = bounds
layer.addSublayer(preview)
self.previewLayer = preview
applyPortrait(preview.connection)
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.session.startRunning()
}
}
/// (iOS 17+ videoRotationAngle, videoOrientation )
private func applyPortrait(_ connection: AVCaptureConnection?) {
guard let connection else { return }
if connection.isVideoRotationAngleSupported(90) {
connection.videoRotationAngle = 90
}
}
override func layoutSubviews() {
super.layoutSubviews()
previewLayer?.frame = bounds
}
func capture(completion: @escaping (UIImage?) -> Void) {
guard session.isRunning else { completion(nil); return }
captureCompletion = completion
applyPortrait(output.connection(with: .video))
output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
}
func stop() {
guard session.isRunning else { return }
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.session.stopRunning()
}
}
func photoOutput(_ output: AVCapturePhotoOutput,
didFinishProcessingPhoto photo: AVCapturePhoto,
error: Error?) {
let completion = captureCompletion
captureCompletion = nil
// AVFoundation ,SwiftUI 线
let deliver: (UIImage?) -> Void = { result in
DispatchQueue.main.async { completion?(result) }
}
guard error == nil,
let data = photo.fileDataRepresentation(),
let image = UIImage(data: data) else {
deliver(nil)
return
}
let upright = image.normalizedUp()
guard previewLayer != nil else {
deliver(upright)
return
}
// : .resizeAspectFill bounds,,
// aspect-fill rect bounds 线
DispatchQueue.main.async {
let viewSize = self.bounds.size
let box = RegionFraming.box(in: viewSize)
let cropped = RegionImageCropper.crop(upright, box: box, viewSize: viewSize)
completion?(cropped)
}
}
}