我试图在原始帖子中回答这个问题,但是不会让我这么做.希望有更多权限的人可以将其合并到原始问题中.
好的,这是一个更完整的答案.首先,设置捕获:
// Create capture session
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
// Setup capture input
self.inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:self.inputDevice
error:nil];
[self.captureSession addInput:captureInput];
// Setup video processing (capture output)
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
// Don't add frames to the queue if frames are already processing
captureOutput.alwaysDiscardsLateVideoFrames = YES;
// Create a serial queue to handle processing of frames
_videoQueue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:_videoQueue];
// Set the video output to store …Run Code Online (Sandbox Code Playgroud) 我正在与之合作AVCaptureVideoDataOutput并希望转换CMSampleBufferRef为UIImage.许多答案都是一样的,比如从CMSampleBufferRef创建的UIImage没有在UIImageView中显示?和AVCaptureSession有多个预览
如果我将VideoDataOutput颜色空间设置为BGRA,则可以正常工作(记入此答案CGBitmapContextCreateImage错误)
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[dataOutput setVideoSettings:videoSettings];
Run Code Online (Sandbox Code Playgroud)
如果没有上述视频设置,我将收到以下错误
CGBitmapContextCreate: invalid data bytes/row: should be at least 2560 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst.
<Error>: CGBitmapContextCreateImage: invalid context 0x0
Run Code Online (Sandbox Code Playgroud)
使用BGRA不是一个好的选择,因为从YUV(默认的AVCaptureSession颜色空间)到BGRA有转换开销,如Brad和Codo在如何从AVCaptureSession中获取CMSampleBuffer中的Y组件所述?
那么,有没有办法转换CMSampleBufferRef到UIImage与YUV色彩空间的工作?
我正在使用这个问题的公式:
uint8_t *rgbBuffer = malloc(imageWidth * imageHeight * 3);
// .. iterate over height and width
// from ITU-R BT.601, rounded to integers
rgbOutput[0] = (298 * (y - 16) + 409 * cr - 223) >> 8;
rgbOutput[1] = (298 * (y - 16) + 100 * cb + 208 * cr + 136) >> 8;
rgbOutput[2] = (298 * (y - 16) + 516 * cb - 277) >> 8;
Run Code Online (Sandbox Code Playgroud)
我假设它基于维基文章中的ITU-R_BT.601公式.

但是我觉得公式不太对,因为输出图像看起来像这样: …