我尝试从从相机接收到的像素缓冲区中提取 yuv 数据,然后重新创建一个像素缓冲区,但我在 CVOpenGLESTextureCacheCreateTextureFromImage 处得到 -6683,文档只是说明
由于不支持的缓冲区大小、像素格式或属性,像素缓冲区与 OpenGL 不兼容。
这对我没有多大帮助。我该如何解决?代码如下:
- (void)Init
{
*****;
OSStatus err = CMBufferQueueCreate(kCFAllocatorDefault, 1, CMBufferQueueGetCallbacksForUnsortedSampleBuffers(), &previewBufferQueue);
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
OSStatus err = CMBufferQueueEnqueue(previewBufferQueue, sampleBuffer);
if ( !err ) {
dispatch_async(dispatch_get_main_queue(), ^{
CMSampleBufferRef sbuf = (CMSampleBufferRef)CMBufferQueueDequeueAndRetain(previewBufferQueue);
if (sbuf) {
CVImageBufferRef pixBuf = CMSampleBufferGetImageBuffer(sbuf);
[self.delegate displayPixelBuffer:pixBuf];
CFRelease(sbuf);
}
});
}
}
displayPixelBuffer 方法:
-(void)displayPixelBuffer:(CVImageBufferRef)pixelBuffer
{
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
size_t planeWidth[2] = {CVPixelBufferGetWidthOfPlane(pixelBuffer, 0), width};
size_t planeHeight[2] = {CVPixelBufferGetHeightOfPlane(pixelBuffer, 0),
CVPixelBufferGetHeightOfPlane(pixelBuffer, 1)};
size_t planeBytesPerRow[2] = {CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0), width/2};
unsigned char *YUV2[2] = {0};
YUV2[0] = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
YUV2[1] = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
CVReturn renturn = CVPixelBufferCreateWithPlanarBytes(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
0,
0,
2,
(void *)YUV2,
planeWidth,
planeHeight,
planeBytesPerRow,
nil,
nil, nil, &imageBuffer);
glActiveTexture(GL_TEXTURE0);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
videoTextureCache,
imageBuffer,
NULL,
GL_TEXTURE_2D,
GL_LUMINANCE,
width,
height,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
0,
&texture);
if (!texture || err) {
NSLog(@"CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
return;
}
glBindTexture(CVOpenGLESTextureGetTarget(texture), 0);
// Flush the CVOpenGLESTexture cache and release the texture
CVOpenGLESTextureCacheFlush(videoTextureCache, 0);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CFRelease(texture);
}
结果是 CVOpenGLESTextureCacheCreateTextureFromImage failed(error:-6683),为什么?