我试图从相机捕获视频.我已经得到了captureOutput:didOutputSampleBuffer:回调触发器,它给了我一个样本缓冲区,然后我转换为CVImageBufferRef.我然后尝试将该图像转换为UIImage我可以在我的应用程序中查看的图像.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
/*Lock the image buffer*/
CVPixelBufferLockBaseAddress(imageBuffer,0);
/*Get information about the image*/
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
/*We unlock the image buffer*/
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
/*Create a CGImageRef from the CVImageBufferRef*/
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
/*We release some components*/
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
/*We display the result on the custom layer*/
/*self.customLayer.contents = (id) newImage;*/
/*We display the result on the image view (We need to change the orientation of the image so that the video is displayed correctly)*/
UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
self.capturedView.image = image;
/*We relase the CGImageRef*/
CGImageRelease(newImage);
}
Run Code Online (Sandbox Code Playgroud)
在调用之前,代码似乎工作正常CGBitmapContextCreate.它总是返回一个NULL指针.因此,该函数的其余部分都不起作用.无论我似乎传递它,该函数返回null.我不知道为什么.
Chr*_*nak 20
传递baseAddress的方式假定图像数据在表单中
ACCC
(其中C是一些颜色成分,R || G || B).
如果您已将AVCaptureSession设置为以原始格式捕获视频帧,则很可能您将视频数据恢复为平面YUV420格式.(请参阅:链接文本)为了完成您在此处尝试执行的操作,可能最简单的方法是指定您希望在kCVPixelFormatType_32RGBA中捕获的视频帧.如果您以非平面格式捕获视频帧,Apple建议您以kCVPixelFormatType_32BGRA捕获视频帧,其原因未说明,但我可以合理地假设是出于性能考虑.
警告:我没有这样做,并且假设访问这样的CVPixelBufferRef内容是构建图像的合理方法.我无法保证这实际上有效,但我/可以/告诉你,由于您(可能)捕获视频帧的像素格式,您现在正确处理的方式将无法正常工作.
Oli*_*ork 14
如果你只需要将CVImageBufferRef转换为UIImage,那么它似乎要比应该的要困难得多.基本上你需要转换为CIImage,然后转换为CGImage,然后转换为UIImage.我希望我能告诉你为什么.谁知道.
-(void) screenshotOfVideoStream:(CVImageBufferRef)imageBuffer
{
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
CIContext *temporaryContext = [CIContext contextWithOptions:nil];
CGImageRef videoImage = [temporaryContext
createCGImage:ciImage
fromRect:CGRectMake(0, 0,
CVPixelBufferGetWidth(imageBuffer),
CVPixelBufferGetHeight(imageBuffer))];
UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
[self doSomethingWithOurUIImage:image];
CGImageRelease(videoImage);
}
Run Code Online (Sandbox Code Playgroud)
当我使用VTDecompressionSession回调转换H.264视频以获取CVImageBufferRef(但它适用于任何CVImageBufferRef)时,这种特殊方法对我有用.我使用的是iOS 8.1,XCode 6.2.
您可以直接致电:
Run Code Online (Sandbox Code Playgroud)self.yourImageView.image=[[UIImage alloc] initWithCIImage:[CIImage imageWithCVPixelBuffer:imageBuffer]];
| 归档时间: |
|
| 查看次数: |
27520 次 |
| 最近记录: |