我尝试在视频捕获管道中将 4:3 帧转换为 16:9 帧。转换后的帧需要进一步处理。因此我需要将覆盖框架保持为CVImageBufferRef
. 我查看了这个堆栈溢出线程,并从中借鉴了一些想法
iOS - Scale and crop CMSampleBufferRef/CVImageBufferRef
这是我所做的:
int cropX0 = 0, cropY0 = 60, cropHeight = 360, cropWidth = 640, outWidth = 640, outHeight = 360;
//get CVPixel buffer from CMSampleBuffer
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(cmSampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t startpos = cropY0 * bytesPerRow;
void* cropStartAddr = ((char*)baseAddress) + startpos;
CVPixelBufferRef cropPixelBuffer = NULL;
int status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
outWidth,
outHeight,
CVPixelBufferGetPixelFormatType(imageBuffer),
cropStartAddr,
bytesPerRow,
NULL,
0,
NULL,
&cropPixelBuffer);
if(status == 0){
OSStatus result = 0;
}
但是经过这个方法。如果我们从 Xcode 检查cropPixelBuffer。它看起来像一个损坏的图像。
我想这可能是因为颜色格式是 NV12 yuv 像素格式 谢谢你的帮助