【发布时间】: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