【问题标题】:Swift - Camera with device rotation in Object DetectionSwift - 物体检测中带有设备旋转的相机
【发布时间】:2021-01-30 09:20:40
【问题描述】:

我正在查看本教程 (https://developer.apple.com/documentation/vision/recognizing_objects_in_live_capture),问题是当您旋转设备时,相机的视图不会改变帧大小,因此您会有一个黑色空间。

有解决这个问题的想法吗?

另外,我在 SwiftUI 中制作了我的应用程序版本(但问题仍然存在),因此也感谢使用 SwiftUI 的解决方案!

提前致谢

【问题讨论】:

    标签: swift swiftui uikit avfoundation coreml


    【解决方案1】:

    我不确定在 SwiftUI 中如何,但在 UIKit 中是这样的:

    尝试设置videoGravity:

    cameraView.videoPreviewLayer.videoGravity = .resizeAspectFill
    

    然后,这应该注意方向。这是基于answer

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        let cameraPreviewTransform = self.cameraView.transform
        
        coordinator.animate { (context) in
            
            let deltaTransform = coordinator.targetTransform
            let deltaAngle: CGFloat = atan2(deltaTransform.b, deltaTransform.a)
            
            var currentRotation = atan2(cameraPreviewTransform.b, cameraPreviewTransform.a)
            
            // Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft.
            currentRotation += -1 * deltaAngle + 0.0001;
            self.cameraView.layer.setValue(currentRotation, forKeyPath: "transform.rotation.z")
            self.cameraView.layer.frame = self.view.bounds
        } completion: { (context) in
            let currentTransform : CGAffineTransform = self.cameraView.transform
            self.cameraView.transform = currentTransform
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 2012-10-11
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多