小编fff*_*fff的帖子

如何从 CVPixelBuffer 中删除 Alpha 通道并在 Swift 中获取其数据

我的 CVPixelBuffer 为kCVPixelFormatType_32BGRA,我试图获取没有 Alpha 通道的 BGR 格式的帧数据。这是我尝试做的(作为extension CVPixelBuffer

func bgrData(byteCount: Int) -> Data? {
    CVPixelBufferLockBaseAddress(self, .readOnly)
    defer { CVPixelBufferUnlockBaseAddress(self, .readOnly) }
    guard let sourceData = CVPixelBufferGetBaseAddress(self) else {
        return nil
    }
    
    let width = CVPixelBufferGetWidth(self)
    let height = CVPixelBufferGetHeight(self)
    let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(self)
    let destinationBytesPerRow = 3 * width
    
    // Assign input image to `sourceBuffer` to convert it.
    var sourceBuffer = vImage_Buffer(
        data: sourceData,
        height: vImagePixelCount(height),
        width: vImagePixelCount(width),
        rowBytes: sourceBytesPerRow
    )
    
    // Make `destinationBuffer` and `destinationData` …
Run Code Online (Sandbox Code Playgroud)

alpha image-processing ios swift

3
推荐指数
1
解决办法
1379
查看次数

标签 统计

alpha ×1

image-processing ×1

ios ×1

swift ×1