【问题标题】:UIImage from CMSampleBuffer has a blue tint来自 CMSampleBuffer 的 UIImage 有蓝色调
【发布时间】:2015-11-24 18:08:12
【问题描述】:

我在 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)
}

【问题讨论】:

    标签: ios swift2 avfoundation avcapture


    【解决方案1】:

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

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
    let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
    

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2012-11-15
      • 2012-01-19
      相关资源
      最近更新 更多