我玩过AVFoundation
尝试将过滤器应用于实时视频。我尝试将过滤器应用于 AVCaptureVideoDataOutput
,但输出只占 View 的 1/4。
这是我的一些相关代码
捕获
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)
drawableWidth
和 drawableHeight
返回以像素为单位的尺寸,考虑 contentScaleFactor。看:https://developer.apple.com/reference/glkit/glkview/1615591-drawablewidth
另外,glView 的
contentScaleFactor
也不需要设置。
关于ios - AVFoundation 输出的图像仅占屏幕的 1/4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522120/