标签: avcapturephotooutput

从 AVCapturePhotoOutput 裁剪 CGRect (resizeAspectFill)

我发现了以下问题,不幸的是其他帖子没有帮助我找到可行的解决方案。

我有一个简单的应用程序,显示相机预览 (AVCaptureVideoPreviewLayer),其中视频重力已设置为 resizeAspectFill ( videoGravity = .resizeAspectFill)。

根据我的理解,这只会拉伸图像的宽度以填充屏幕。

在我的预览图层上,我还应用了 CGRect 作为具有固定 x、y、宽度和高度的蒙版。

现在,一旦我拍照,我就会尝试从图像中裁剪出确切的矩形。根据我的理解,我应该使用某种数学将 CGRect 转换为与我从 AVCapturePhotoOutput 方法获得的图像相同的宽高比,但它似乎从未在宽度上正确裁剪。

    private func cropImage(image: UIImage) {
        let rect = CGRect(x: 25, y: 150, width: 325, height: 230)
        
        
        let scale = CGAffineTransform(scaleX: 1/self.view.frame.width, y: 1/self.view.frame.height)
        let flip = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -1)
        
        let bounds = rect.applying(scale).applying(flip)
        
        
        let topLeft = bounds.topLeft.scaled(to: image.size)
        let topRight = bounds.topRight.scaled(to: image.size)
        let bottomLeft = bounds.bottomLeft.scaled(to: image.size)
        let bottomRight = bounds.bottomRight.scaled(to: image.size)
        
        var ciImage = CIImage(image: …
Run Code Online (Sandbox Code Playgroud)

crop cgrect swift avcapturephotooutput

5
推荐指数
1
解决办法
609
查看次数

裁剪 AVCapturePhoto 以覆盖屏幕上显示的矩形

我正在尝试拍摄一块薄金属的照片,并将其裁剪为屏幕上显示的轮廓。我几乎看过这里的所有其他帖子,但还没有任何东西适合我。然后该图像将被图书馆用于分析。我可以进行一些裁剪,但不能对显示的矩形进行裁剪。我尝试在裁剪之前旋转图像,并根据屏幕上的矩形计算矩形。

视频层

这是我的捕获代码。PreviewView是容器,videoLayer是AVCapture视频的容器。

    // Photo capture delegate
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        guard let imgData = photo.fileDataRepresentation(), let uiImg = UIImage(data: imgData), let cgImg = uiImg.cgImage else {
            return
        }
        print("Original image size: ", uiImg.size, "\nCGHeight: ", cgImg.height, " width: ", cgImg.width)
        print("Orientation: ", uiImg.imageOrientation.rawValue)
        
        guard let img = cropImage(image: uiImg) else {
            return
        }
        
        showImage(image: img)
    }

    
    func cropImage(image: UIImage) -> UIImage? {
        print("Image size before crop: ", image.size)
        //Get the croppedRect from function below …
Run Code Online (Sandbox Code Playgroud)

camera ios avcapturesession swift avcapturephotooutput

0
推荐指数
1
解决办法
1314
查看次数