2016-05-24 63 views
1

我需要創建CVPixelBufferRef的副本,以便能夠使用副本中的值以按位方式處理原始像素緩衝區。我似乎無法通過CVPixelBufferCreateCVPixelBufferCreateWithBytes實現此目的。使用CVPixelBufferCreate複製/複製CVPixelBufferRef

根據this question,它也可以用memcpy()來完成。但是,沒有關於如何實現這一點的解釋,以及無論需要哪個核心視頻庫調用。

+0

你就不能'memcpy'數據別的地方? – jtbandes

+0

這實際上正是我想到的'更簡單的方法',但是我沒有找到如何做到這一點的任何示例,而且我對C編程頗爲不屑。我只需定義一個新的像素緩衝區「CVPixelBufferRef newPixelBuffer = NULL」,然後使用memcpy()? –

+0

不,您只需要複製像素數據,而不是複製CVPixelBufferRef所具有的任何內部結構。請參閱 CVPixelBufferGetBaseAddress文檔。 – jtbandes

回答

3

這似乎工作:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // Get pixel buffer info 
    const int kBytesPerPixel = 4; 
    CVPixelBufferLockBaseAddress(pixelBuffer, 0); 
    int bufferWidth = (int)CVPixelBufferGetWidth(pixelBuffer); 
    int bufferHeight = (int)CVPixelBufferGetHeight(pixelBuffer); 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); 
    uint8_t *baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer); 

    // Copy the pixel buffer 
    CVPixelBufferRef pixelBufferCopy = NULL; 
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, bufferWidth, bufferHeight, kCVPixelFormatType_32BGRA, NULL, &pixelBufferCopy); 
    CVPixelBufferLockBaseAddress(pixelBufferCopy, 0); 
    uint8_t *copyBaseAddress = CVPixelBufferGetBaseAddress(pixelBufferCopy); 
    memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow); 

    // Do what needs to be done with the 2 pixel buffers 

} 
+0

kBytesPerPixel未使用且不必要 – tmm1

+0

這不起作用?輸出圖像被破壞。 –