我正在尝试在 AVFoundation 输出上找到可接受的格式:
self.theOutput=[[AVCaptureVideoDataOutput alloc]init];
if ([self.theSession canAddOutput:self.theOutput])
[self.theSession addOutput:self.theOutput];
然后我在 and 之后插入一个断点:
po [self.theOutput availableVideoCVPixelFormatTypes]
我明白了:
(NSArray *) $5 = 0x2087ad00 <__NSArrayM 0x2087ad00>(
875704438,
875704422,
1111970369
)
如何获取这些格式类型的字符串值?
谢谢
在运行 iOS6 的 iPhone5 上,以下是可用的 AVCaptureVideoDataOuputVideoCVPixelFormatTypes:
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
kCVPixelFormatType_32BGRA
信用该信用的地方,我找到了一种方法来获得这里支持的价值。 https://gist.github.com/2327666
#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);
当您调用 availableVideoCVPixelFormatTypes 时,您将获得所有像素格式类型标签的十进制表示形式。如果将它们转换为十六进制,您可以将其中一些与Apple 文档中列出的标签相匹配。对于其余的,您必须将获得的十六进制值转换为 ASCII 字符以最终匹配标签。
例如:
(十进制)------>(十六进制)--->(ASCII)
875704438 -> 34323076 -> 420v
875704422 -> 34323066 -> 420f
1111970369 -> 42475241 -> BGRA
我发现这个网站“ASCII to Hex”很有用。
您可以像这样转换
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
这些值对应于
CVPixelBuffer.h
的 CoreVideo
中描述的枚举值:
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 值保存为小端,这会产生“v024”而不是“420v”。因此,我们需要使用
CFSwapInt32HostToBig
将枚举值转换为 big endian
,然后再将其保存到缓冲区。
希望这有帮助。
在 Swift 5.9 中,您可以使用此代码将小数转换为字符串
for pixel in videoDataOutput.availableVideoPixelFormatTypes {
let number: UInt32 = pixel
let ostype = number.bigEndian // Assuming big-endian byte order
let bytes = withUnsafeBytes(of: ostype) {
Array($0)
}
let ostypeString = String(bytes: bytes, encoding: .ascii)
print(ostypeString)
}
这应该在控制台中打印:
2016686642 = x422
2016686640 = x420
645428786 = &xv2
762869298 = -xv2
645428784 = &xv0
762869296 = -xv0
然后你可以检查苹果文档是否获得匹配的pixelFormat: https://developer.apple.com/documentation/technotes/tn3121-selecting-a-pixel-format-for-an-avcapturevideodataoutput