CMSampleBufferGetImageBuffer对于捕获的JPEG stillImage返回nil

时间:2019-01-18 07:13:55

标签: macos jpeg cvpixelbuffer cmsamplebuffer

我在使用JPEG格式的Mac捕获屏幕上工作,然后获取捕获的JPEG样本缓冲区的pixelBuffer和imageBuffer。 但是,pixelBuffer始终为零,而当我将JPEG缓冲区转换为NSImage时,可以成功获取并显示图像。

-(void)createSession
{
if(self.session == nil)
        {
            self.session = [[AVCaptureSession alloc] init];
            self.session.sessionPreset = AVCaptureSessionPresetPhoto;
            CGDirectDisplayID displayId = [self getDisplayID];

            self.input = [[AVCaptureScreenInput alloc] initWithDisplayID:displayId];

            [self.session addInput:self.input];

            self.imageOutput = [[AVCaptureStillImageOutput alloc] init];

            NSDictionary *outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG};

            [self.imageOutput setOutputSettings:outputSettings];

            [self.session addOutput:self.imageOutput];

            [self.session startRunning];
        }
}



-(void)processSampleBuffer
{
AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in self.imageOutput.connections) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    [self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *  error) {
        if(imageDataSampleBuffer != nil)
        {
            NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            self.image = [[NSImage alloc] initWithData:imageData];

            CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
            CVPixelBufferLockBaseAddress(imageBuffer,0);

            size_t width = CVPixelBufferGetWidth(imageBuffer);
            size_t height = CVPixelBufferGetHeight(imageBuffer);

            NSLog(@"width %zu height %zu", width,height);

             CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
            float width1 = CVPixelBufferGetWidth(pixelBuffer);
            float height1 = CVPixelBufferGetHeight(pixelBuffer);

             NSLog(@"Pixelbuffer width %f height %f", width1,height1);

        }
        else
        {
            NSLog(@"error");
        }

    }];
}

in processSampleBuffer self.image可以获取NSImage并成功显示在NSImageView中。 但是imageBuffer和pixelBuffer都为零。

这让我很困惑,有人可以帮忙看看吗?

1 个答案:

答案 0 :(得分:0)

好的,最后,我在这里找到了答案,希望它能对其他人有所帮助。

https://developer.apple.com/documentation/avfoundation/avcapturephoto/2873914-pixelbuffer?language=objc

讨论

如果您请求以RAW格式或未压缩的经过处理的格式(例如TIFF)捕获照片,则可以使用此属性访问基础的样本缓冲区。

如果您请求使用JPEG或HEVC / HEIF等压缩格式的捕获,则此属性的值为nil。使用fileDataRepresentation或CGImageRepresentation方法获取压缩的图像数据。