OStack程序员社区-中国程序员成长平台

标题: ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:04
标题: ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调

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

enter image description here

这是 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)
}



Best Answer-推荐答案


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

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

关于ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900810/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4