微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何从vImage_Buffer转到CVPixelBufferRef

我正在iOS应用中录制实时视频.在另一个StackOverflow页面( Link)上,我发现你可以使用vImage_Buffer来处理我的帧.

问题是我不知道如何从输出的vImage_buffer返回CVPixelBufferRef.

以下是另一篇文章中给出的代码

NSInteger cropX0 = 100,cropY0 = 100,cropHeight = 100,cropWidth = 100,outWidth = 480,outHeight = 480;

CVImageBufferRef imageBuffer = CMSampleBufferGetimageBuffer(sampleBuffer);                   
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

vImage_Buffer inBuff;                       
inBuff.height = cropHeight;
inBuff.width = cropWidth;
inBuff.rowBytes = bytesPerRow;

int startpos = cropY0*bytesPerRow+4*cropX0;
inBuff.data = baseAddress+startpos;

unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight);
vImage_Buffer outBuff = {outImg,outHeight,outWidth,4*outWidth};

vImage_Error err = vImageScale_ARGB8888(&inBuff,&outBuff,NULL,0);
if (err != kvImageNoError) NSLog(@" error %ld",err);

现在我需要将outBuff转换为CVPixelBufferRef.

我假设我需要使用vImageBuffer_copyToCVPixelBuffer,但我不确定如何.

我的第一次尝试失败了EXC_BAD_ACCESS:

CVPixelBufferUnlockBaseAddress(ImageBuffer的,0);

CVPixelBufferRef pixelBuffer;
    CVPixelBufferCreate(kcfAllocatorSystemDefault,480,kCVPixelFormatType_32BGRA,&pixelBuffer);

    CVPixelBufferLockBaseAddress( pixelBuffer,0 );

    vImage_CGImageFormat format = {
        .bitsPerComponent = 8,.bitsPerPixel = 32,.bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneskipFirst,//BGRX8888
        .colorSpace = NULL,//sRGB
    };

    vImageBuffer_copyToCVPixelBuffer(&outBuff,&format,pixelBuffer,kvImageNoFlags); // Here is the crash!


    CVPixelBufferUnlockBaseAddress( pixelBuffer,0 );

任何的想法?

解决方法

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool : YES],kCVPixelBufferCGImageCompatibilityKey,[NSNumber numberWithBool : YES],kCVPixelBufferCGBitmapContextCompatibilityKey,[NSNumber numberWithInt  : 480],kCVPixelBufferWidthKey,kCVPixelBufferHeightKey,nil];

            status = CVPixelBufferCreateWithBytes(kcfAllocatorDefault,outImg,bytesPerRow,(__bridge CFDictionaryRef)options,&pixbuffer);

你应该生成一个像上面这样的新pixelBuffer.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐