ios 获取AvCaptureVideoDataOutput可用的实际NS字符串VideoCV像素格式类型

4c8rllxm  于 2023-01-22  发布在  iOS
关注(0)|答案(4)|浏览(101)

我正在尝试查找AVFoundation输出上可接受的格式:

self.theOutput=[[AVCaptureVideoDataOutput alloc]init];
    if ([self.theSession canAddOutput:self.theOutput])
        [self.theSession addOutput:self.theOutput];

然后,我在和之后插入一个断点:

po [self.theOutput availableVideoCVPixelFormatTypes]

然后我得到了这个

(NSArray *) $5 = 0x2087ad00 <__NSArrayM 0x2087ad00>(
875704438,
875704422,
1111970369
)

如何获得这些格式类型的字符串值?
谢谢

fkvaft9z

fkvaft9z1#

在运行iOS6的iPhone5上,以下是可用的AV捕获视频数据输出视频CV像素格式类型:
kCV像素格式类型_420YpCbCr8双平面视频范围
kCV像素格式类型_420YpCbCr8双平面全范围
kCV像素格式类型_32BGRA
值得表扬的地方,我找到了一种方法来获得这里支持的值。https://gist.github.com/2327666

kx1ctssn

kx1ctssn2#

用于调试的类别版本

作为NSNumber上的类别

#import <CoreVideo/CoreVideo.h> 

@implementation NSNumber (CVPixelFormatType)

- (NSString *)descriptivePixelFormat
{
    return @{
             @(kCVPixelFormatType_1Monochrome): @"kCVPixelFormatType_1Monochrome",
             @(kCVPixelFormatType_2Indexed): @"kCVPixelFormatType_2Indexed",
             @(kCVPixelFormatType_4Indexed): @"kCVPixelFormatType_4Indexed",
             @(kCVPixelFormatType_8Indexed): @"kCVPixelFormatType_8Indexed",
             @(kCVPixelFormatType_1IndexedGray_WhiteIsZero): @"kCVPixelFormatType_1IndexedGray_WhiteIsZero",
             @(kCVPixelFormatType_2IndexedGray_WhiteIsZero): @"kCVPixelFormatType_2IndexedGray_WhiteIsZero",
             @(kCVPixelFormatType_4IndexedGray_WhiteIsZero): @"kCVPixelFormatType_4IndexedGray_WhiteIsZero",
             @(kCVPixelFormatType_8IndexedGray_WhiteIsZero): @"kCVPixelFormatType_8IndexedGray_WhiteIsZero",
             @(kCVPixelFormatType_16BE555): @"kCVPixelFormatType_16BE555",
             @(kCVPixelFormatType_16LE555): @"kCVPixelFormatType_16LE555",
             @(kCVPixelFormatType_16LE5551): @"kCVPixelFormatType_16LE5551",
             @(kCVPixelFormatType_16BE565): @"kCVPixelFormatType_16BE565",
             @(kCVPixelFormatType_16LE565): @"kCVPixelFormatType_16LE565",
             @(kCVPixelFormatType_24RGB): @"kCVPixelFormatType_24RGB",
             @(kCVPixelFormatType_24BGR): @"kCVPixelFormatType_24BGR",
             @(kCVPixelFormatType_32ARGB): @"kCVPixelFormatType_32ARGB",
             @(kCVPixelFormatType_32BGRA): @"kCVPixelFormatType_32BGRA",
             @(kCVPixelFormatType_32ABGR): @"kCVPixelFormatType_32ABGR",
             @(kCVPixelFormatType_32RGBA): @"kCVPixelFormatType_32RGBA",
             @(kCVPixelFormatType_64ARGB): @"kCVPixelFormatType_64ARGB",
             @(kCVPixelFormatType_48RGB): @"kCVPixelFormatType_48RGB",
             @(kCVPixelFormatType_32AlphaGray): @"kCVPixelFormatType_32AlphaGray",
             @(kCVPixelFormatType_16Gray): @"kCVPixelFormatType_16Gray",
             @(kCVPixelFormatType_422YpCbCr8): @"kCVPixelFormatType_422YpCbCr8",
             @(kCVPixelFormatType_4444YpCbCrA8): @"kCVPixelFormatType_4444YpCbCrA8",
             @(kCVPixelFormatType_4444YpCbCrA8R): @"kCVPixelFormatType_4444YpCbCrA8R",
             @(kCVPixelFormatType_444YpCbCr8): @"kCVPixelFormatType_444YpCbCr8",
             @(kCVPixelFormatType_422YpCbCr16): @"kCVPixelFormatType_422YpCbCr16",
             @(kCVPixelFormatType_422YpCbCr10): @"kCVPixelFormatType_422YpCbCr10",
             @(kCVPixelFormatType_444YpCbCr10): @"kCVPixelFormatType_444YpCbCr10",
             @(kCVPixelFormatType_420YpCbCr8Planar): @"kCVPixelFormatType_420YpCbCr8Planar",
             @(kCVPixelFormatType_420YpCbCr8PlanarFullRange): @"kCVPixelFormatType_420YpCbCr8PlanarFullRange",
             @(kCVPixelFormatType_422YpCbCr_4A_8BiPlanar): @"kCVPixelFormatType_422YpCbCr_4A_8BiPlanar",
             @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange): @"kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange",
             @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange): @"kCVPixelFormatType_420YpCbCr8BiPlanarFullRange",
             @(kCVPixelFormatType_422YpCbCr8_yuvs): @"kCVPixelFormatType_422YpCbCr8_yuvs",
             @(kCVPixelFormatType_422YpCbCr8FullRange): @"kCVPixelFormatType_422YpCbCr8FullRange"
             }[self];
}

@end

诊断输出示例

NSMutableArray *mutablePixelFormatTypes = [NSMutableArray array];
[captureOutput.availableVideoCVPixelFormatTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    [mutablePixelFormatTypes addObject:[obj descriptivePixelFormat]];
}];
NSString *pixelFormats = [mutablePixelFormatTypes componentsJoinedByString:@",\n"];
NSLog(@"Available pixel formats:\n%@\n", pixelFormats);
vngu2lb8

vngu2lb83#

当你调用availableVideoCVPixelFormatTypes时,你会得到所有像素格式类型标签的十进制表示。如果你把它们转换成十六进制,你可以把其中一些匹配到苹果文档中列出的标签。对于其余的,你必须把你得到的十六进制值转换成ASCII字符,以最终匹配标签。

    • 例如:**

(十进制)------〉(十六进制)---〉(ASCII)
875704438-〉34323076-〉420伏
875704422-〉34323066-〉420女性
1111970369-〉42475241-〉英国政府监管局
我发现这个网站"ASCII to Hex"是有用的。

ikfrs5lh

ikfrs5lh4#

您可以像这样转换availableVideoCVPixelFormatTypes返回的值:

NSArray *supportedPixelFormats = [videoDeviceOutput availableVideoCVPixelFormatTypes];
char buffer[5] = {0};
for (NSNumber *pixelFormat in supportedPixelFormats){
    *(int *)&buffer[0] = CFSwapInt32HostToBig([pixelFormat intValue]);
    NSLog(@"FORMAT: %s", buffer);
}

并且输出将是

FORMAT: 420v
FORMAT: 420f
FORMAT: BGRA

这些值对应于CoreVideoCVPixelBuffer.h中描述的枚举值:

kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange = '420v'
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange  = '420f',
kCVPixelFormatType_32BGRA         = 'BGRA',

代码解释:常量值'420v'在内存中存储为4个字节,同时int也是4个字节,这允许我们将'420v'转换为字节,然后再转换回int。
下面的代码行将一个值作为int写入字符缓冲区,以便以后作为字符串读取。

*(int *)&buffer[0] = CFSwapInt32HostToBig([pixelFormat intValue]);

唯一的问题是Objective-C通常会将一个int值保存为little endian,这将产生'v024'而不是'420v',因此,我们需要使用CFSwapInt32HostToBig将一个枚举值转换为big endian,然后再将其保存到缓冲区。
希望这个有用。

相关问题