2

我在 UIImageView 中显示转换为 UIImages 的 CMSampleBuffers 视频源。在下面的照片中,背景层是 AVCapturePreviewLayer,中心是缓冲区提要。我的目标是去除蓝色调。

在此处输入图像描述

这是 CMSampleBuffer 到 UIImage 代码

extension CMSampleBuffer {
  func imageRepresentation() -> UIImage? {

    let imageBuffer: CVImageBufferRef = CMSampleBufferGetImageBuffer(self)!

    CVPixelBufferLockBaseAddress(imageBuffer, 0)
    let address = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
    let width = CVPixelBufferGetWidth(imageBuffer)
    let height = CVPixelBufferGetHeight(imageBuffer)

    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.NoneSkipFirst.rawValue)
    let imageRef = CGBitmapContextCreateImage(context)

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0)
    let resultImage: UIImage = UIImage(CGImage: imageRef!)

    return resultImage
  }
}

AVCaptureVideoDataOutput 设置:

class MovieRecorder: NSObject {
  // vars
  private let captureVideoDataOutput = AVCaptureVideoDataOutput()

  // capture session boilerplate setup...

  captureVideoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)]
  captureVideoDataOutput.alwaysDiscardsLateVideoFrames = true
  captureVideoDataOutput.setSampleBufferDelegate(self, queue: captureDataOutputQueue)
}
4

1 回答 1

4

问题出在位图信息上。这个位图信息修复了它。

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
于 2015-11-24T18:53:36.717 回答