【问题标题】:How can I get full resolution images from an AVCaptureSession without using the jpeg encoder?如何在不使用 jpeg 编码器的情况下从 AVCaptureSession 获取全分辨率图像?
【发布时间】:2023-03-21 00:02:01
【问题描述】:

我正在尝试保存相机中的全分辨率 tiff 文件。我在这里看到了一堆非常有用的指南,用于以直接像素数据或使用 jpeg 硬件压缩器捕获静止图像。到目前为止,我能够以 BGRA 格式捕获直接像素数据,但我似乎无法在 5s 上获得大于 1920x1080 的样本缓冲区。当我切换到 jpeg 压缩器路径时,我得到了完整的 5MP 图像.. 只是 jpeg 格式。

这是我的设置:

// Create the AVCaptureSession
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[self.session setSessionPreset:AVCaptureSessionPresetPhoto];

及以后的输出设置:

NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,nil];

或者:

[stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];

在我要求我设置的样本缓冲区之前:

setHighResolutionStillImageOutputEnabled:YES

然后我正在使用: -captureStillImageAsynchronouslyFromConnection 获取样本缓冲区。

只是为了完成...... 在-captureStillImageAsynchronouslyFromConnection:的完成块内 对于我使用的 Jpeg:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

对于 BGRA,我使用:

CFDictionaryRef metadata = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
// >>>>>>>>>> lock buffer address
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);

// create suitable color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

//Create suitable context (suitable for camera output setting kCVPixelFormatType_32BGRA)
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

// <<<<<<<<<< unlock buffer address
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

// release color space
CGColorSpaceRelease(colorSpace);

//Create a CGImageRef from the CVImageBufferRef
CGImageRef newImage = CGBitmapContextCreateImage(newContext);

我是否忽略了什么?管道中的一切似乎都有效,我在 Apple 文档中找不到任何额外的设置。有没有更好/不同的方法来做到这一点?

TDLR:我似乎无法从使用 BGRA 像素格式输出设置的静态图像捕获会话中获得超过 1920x1080 的分辨率。希望有人能指出我正确的方向。

【问题讨论】:

    标签: ios image avcapturesession


    【解决方案1】:

    哎呀!我设置了类 var 而不是本地类,并在稍后覆盖了类 var,因此不使用会话预设。

    代码应该是:

        // Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [session setSessionPreset:AVCaptureSessionPresetPhoto];
    

    超级简单的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      相关资源
      最近更新 更多