gpt4 book ai didi

ios - 从 iPhone 相机 (AVCaptureSession) 捕获 24 bpp 位图中的图像

转载 作者:行者123 更新时间:2023-11-29 03:05:34 24 4
gpt4 key购买 nike

我正在使用 AVCaptureSession 从 iPhone 的前置摄像头捕获帧。我正在尝试更改 AVCaptureVideoDataOutput 的格式,以便它可以捕获 24 bpp 位图。此代码为我提供了一个没有任何问题的 32 bpp 位图:

AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];
outputDevice.videoSettings = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey: (id)kCVPixelBufferPixelFormatTypeKey];
[outputDevice setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

但是,当我将它更改为 24 时,它在那条线上崩溃了。

outputDevice.videoSettings = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:kCVPixelFormatType_24RGB] forKey: (id)kCVPixelBufferPixelFormatTypeKey];

如何以 24 bpp 捕获图像?为什么 *kCVPixelFormatType_24RGB* 会失败?解决方法是将 32 bmp 转换为 24,但我还没有找到如何做到这一点。

最佳答案

它崩溃是因为 iPhone 不支持 kCVPixelFormatType_24RGB。现代 iPhone 唯一支持的像素格式是:

  • kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
  • kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  • kCVPixelFormatType_32BGRA

尽管 BGRA 缓冲区更易于转换,但您可以将其中任何一种转换为 RGB。有多种方法可以做到这一点(在此处和 Google 上搜索示例),但这里有一个非常简单的方法:

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
@autoreleasepool {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
uint8_t *sourceBuffer = (uint8_t*)CVPixelBufferGetBaseAddress(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
int bufferSize = bytesPerRow * height;
uint8_t *bgraData = malloc(bufferSize);
memcpy(bgraData, sourceBuffer, bufferSize);
uint8_t *rgbData = malloc(width * height * 3);
int rgbCount = 0;
for (int i = 0; i < height; i++) {
for (int ii = 0; ii < width; ii+=4) {
int current = (i * height)+ii;
rgbData[rgbCount] = bgraData[current + 2];
rgbData[rgbCount + 1] = bgraData[current + 1];
rgbData[rgbCount + 2] = bgraData[current];
rgbCount+=3;
}
}
//
// Process rgbData
//
free (rgbData);
}
}

顺便说一句——它是 8bpp(不是 24bpp);构成 24 位图像的三个八位平面,或构成 32 位图像的四个平面。还值得指出的是,在大多数情况下,只使用 32 位数据并忽略 alpha channel ,而不是转换为 24 位数据可能更容易、更快捷。

关于ios - 从 iPhone 相机 (AVCaptureSession) 捕获 24 bpp 位图中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22819810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com