ios - AVFoundation 输出的图像仅占屏幕的 1/4

标签 ios avfoundation core-image

我玩过AVFoundation尝试将过滤器应用于实时视频。我尝试将过滤器应用于 AVCaptureVideoDataOutput ,但输出只占 View 的 1/4。

enter image description here

这是我的一些相关代码

捕获

let availableCameraDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
    for device in availableCameraDevices as! [AVCaptureDevice] {
        if device.position == .Back {
            backCameraDevice = device
        } else if device.position == .Front {
            frontCameraDevice = device
        }
    }

配置输出
private func configureVideoOutput() {
    videoOutput = AVCaptureVideoDataOutput()
    videoOutput?.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))
    if session.canAddOutput(videoOutput) {
        session.addOutput(videoOutput)
    }
}

获取图像
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer:
    CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    // Grab the pixelbuffer
    let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

    // create a CIImage from it, rotate it, and zero the origin
    var image = CIImage(CVPixelBuffer: pixelBuffer)
    image = image.imageByApplyingTransform(CGAffineTransformMakeRotation(CGFloat(-M_PI_2)))
    let origin = image.extent.origin
    image = image.imageByApplyingTransform(CGAffineTransformMakeTranslation(-origin.x, -origin.y))


    self.manualDelegate?.cameraController(self, didOutputImage: image)
}

使成为
func cameraController(cameraController: CameraController, didOutputImage image: CIImage) {
    if glContext != EAGLContext.currentContext() {
        EAGLContext.setCurrentContext(glContext)
    }

    let filteredImage = image.imageByApplyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey: 0.0])

    var rect = view.bounds

    glView.bindDrawable()
    ciContext.drawImage(filteredImage, inRect: rect, fromRect: image.extent)
    glView.display()
}

我预计视网膜显示和比例因子会导致这种情况,但不知道我应该在哪里处理这个问题。我已经将内容比例因子设置为 GLKView,但没有运气。
private var glView: GLKView {
    // Set in storyboard
    return view as! GLKView
}

glView.contentScaleFactor = glView.bounds.size.width / UIScreen.mainScreen().bounds.size.width * UIScreen.mainScreen().scale

最佳答案

您的问题在于输出 rect用于drawImage功能:

ciContext.drawImage(filteredImage, inRect: rect, fromRect: image.extent)

图像的范围是实际像素,而 View 的边界是点,并且不会通过 contentScaleFactor 调整以获得像素。您的设备无疑具有 2.0 的 contentScaleFactor ,因此它是每个维度的 1/2。

相反,将矩形设置为:
var rect = CGRect(x: 0, y: 0, width: glView.drawableWidth, 
                             height: glView.drawableHeight)
drawableWidthdrawableHeight返回以像素为单位的尺寸,考虑 contentScaleFactor。看:
https://developer.apple.com/reference/glkit/glkview/1615591-drawablewidth

另外,glView 的 contentScaleFactor 也不需要设置。

关于ios - AVFoundation 输出的图像仅占屏幕的 1/4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522120/

相关文章:

ios - 自动布局在 iOS 10 中没有动画但在 iOS 9 中工作正常

ios - 如何将实时相机预览添加到 UIView

iphone - iOS 中的 Mac PhotoBooth 凹痕效果

ios - 在 Swift 中以编程方式更改 UIButton 的文本(填充)

ios - Path 和 Instagram 等应用程序如何判断联系人是否也在使用该应用程序?

iOS AVAudioSession,如何访问AVAudioRecorder 电平表

ios - 如何使用StreamingKit保存流音频?

ios - 如何创建有限的 CIImage?

ios - 使用CIFilter的链接器错误

ios - 检索数据并将 JSON 记录保存在数组中