captureOutput:didOutputSampleBuffer:from 连接中的方向问题:

问题描述 投票:0回答:2

我正在尝试处理 AVCaptureSession 的每一帧并在 UIImageView 中预览过滤后的图像。它可以工作,但 UIImageView 中的图像出现旋转(并且扭曲)。我一直试图在这里和谷歌中寻找答案,但我找不到任何有效的解决方案...有人知道要尝试什么吗?

顺便说一句,我正在使用 Swift 3.0

这是我正在使用的代码:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

    let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    let attachments = CMCopyDictionaryOfAttachments(kCFAllocatorMalloc, sampleBuffer!, kCMAttachmentMode_ShouldPropagate)
    let coreImage = CIImage(cvImageBuffer: pixelBuffer!, options: attachments as! [String : Any]?)
    var filteredImage = UIImage(ciImage: coreImage, scale: 1.0, orientation: UIImageOrientation.right)

    if filter {
        NSLog("FILTER ACTIVATED")
        let filterType = CIFilter(name: "CISepiaTone")
        filterType?.setValue(coreImage, forKey: kCIInputImageKey)
        filterType?.setValue(0.5, forKey: kCIInputIntensityKey)
        filteredImage = UIImage(ciImage: filterType!.value(forKey: kCIOutputImageKey) as! CIImage!, scale: filteredImage.scale, orientation: UIImageOrientation.right)
    }

    DispatchQueue.main.async() {
        self.imageViewPreview.image = filteredImage // UIImageView
    }
}

这就是我在预览中看到的:

UIImageView预览

提前非常感谢

ios swift uiimageview uiimage avcapturesession
2个回答
1
投票

我找到了!事实证明我必须在该方法中添加行

connection.videoOrientation = AVCaptureVideoOrientation.portrait
...


0
投票

对于 iOS 17+:

创建一个rotationCoordinator变量:

private var rotationCoordinator: AVCaptureDevice.RotationCoordinator?

configurePreviewLayer()
中分配旋转协调器:

func configurePreviewLayer(_ view: UIView) {
    guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
                                                    for: .video,
                                                    position: .back) else { return }
        
    previewLayer = AVCaptureVideoPreviewLayer(session: session)
    previewLayer?.videoGravity = .resizeAspectFill
        
    guard let previewLayer = previewLayer else { return }
    view.layer.addSublayer(previewLayer)
        
    rotationCoordinator = AVCaptureDevice.RotationCoordinator(device: videoDevice, previewLayer: previewLayer)
}

然后用它来将

videoRotationAngle
分配给
connection
中的
captureOutput()

func captureOutput(_ output: AVCaptureOutput,
                      didOutput sampleBuffer: CMSampleBuffer,
                      from connection: AVCaptureConnection) {
    guard let coordinator = rotationCoordinator else { return }
    connection.videoRotationAngle = coordinator.videoRotationAngleForHorizonLevelCapture
        
    guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
        
    let ciImage = CIImage(cvImageBuffer: imageBuffer)
    let context = CIContext()
    guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return }
        
    currentFrame = UIImage(cgImage: cgImage)
}

注意使用

videoRotationAngleForHorizonLevelCapture
而不是
videoRotationAngleForHorizonLevelPreview

© www.soinside.com 2019 - 2024. All rights reserved.