iOS - 缩放和裁剪CMSampleBufferRef / CVImageBufferRef [英] iOS - Scale and crop CMSampleBufferRef/CVImageBufferRef

查看:4119
本文介绍了iOS - 缩放和裁剪CMSampleBufferRef / CVImageBufferRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVFoundation并从 AVCaptureVideoDataOutput 获取样本缓冲区,我可以使用以下命令将其直接写入videoWriter:

I am using AVFoundation and getting the sample buffer from AVCaptureVideoDataOutput, I can write it directly to videoWriter by using:

- (void)writeBufferFrame:(CMSampleBufferRef)sampleBuffer {
    CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);    
    if(self.videoWriter.status != AVAssetWriterStatusWriting)
    {
        [self.videoWriter startWriting];
        [self.videoWriter startSessionAtSourceTime:lastSampleTime];
    }

    [self.videoWriterInput appendSampleBuffer:sampleBuffer];

}

我现在要做的是裁剪和缩放CMSampleBufferRef中的图像,而不将其转换为UIImage或CGImageRef,因为这会降低性能。

What I want to do now is to crop and scale the image inside the CMSampleBufferRef without converting it into UIImage or CGImageRef because that slows down the performance.

推荐答案

如果您使用vimage,您可以工作直接在缓冲区数据上,而不将其转换为任何图像格式。

If you use vimage you can work directly on the buffer data without converting it to any image format.

outImg包含裁剪和缩放的图像数据。 outWidth和cropWidth之间的关系设置缩放。

outImg contains the cropped and scaled image data. The relation between outWidth and cropWidth sets the scaling.

int cropX0, cropY0, cropHeight, cropWidth, outWidth, outHeight;

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);

因此设置cropX0 = 0和cropY0 = 0并将cropWidth和cropHeight设置为原始大小意味着没有裁剪(使用整个原始图像)。设置outWidth = cropWidth和outHeight = cropHeight会导致无缩放。请注意,inBuff.rowBytes应始终是完整源缓冲区的长度,而不是裁剪长度。

So setting cropX0 = 0 and cropY0 = 0 and cropWidth and cropHeight to the original size means no cropping (using the whole original image). Setting outWidth = cropWidth and outHeight = cropHeight results in no scaling. Note that inBuff.rowBytes should always be the length of the full source buffer, not the cropped length.

这篇关于iOS - 缩放和裁剪CMSampleBufferRef / CVImageBufferRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆