菜鸟教程小白 发表于 2022-12-13 12:04:58

ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调


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

<p> <a href="/image/ibnma.png" rel="noreferrer noopener nofollow"><img src="/image/ibnma.png" alt="enter image description here"/></a> </p>

<p>这是 CMSampleBuffer 到 UIImage 的代码</p>

<pre><code>extension CMSampleBuffer {
func imageRepresentation() -&gt; 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
}
}
</code></pre>

<p>AVCaptureVideoDataOutput 设置:</p>

<pre><code>class MovieRecorder: NSObject {
// vars
private let captureVideoDataOutput = AVCaptureVideoDataOutput()

// capture session boilerplate setup...

captureVideoDataOutput.videoSettings =
captureVideoDataOutput.alwaysDiscardsLateVideoFrames = true
captureVideoDataOutput.setSampleBufferDelegate(self, queue: captureDataOutputQueue)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题出在 bitmapInfo 上。这个位图信息修复了它。</p>

<pre><code>let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33900810/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33900810/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 来自 CMSampleBuffer 的 UIImage 有蓝色调