gpt4 book ai didi

ios - 来自 CMSampleBuffer 的 UIImage 具有蓝色色调

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:39:35 27 4
gpt4 key购买 nike

我在 UIImageView 中显示转换为 UIImage 的 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)
}

最佳答案

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com