【问题标题】:Method to find device's camera resolution iOS查找设备相机分辨率iOS的方法
【发布时间】:2013-10-25 16:24:10
【问题描述】:

使用设置AVCaptureSessionPresetPhoto找到要捕获的图像分辨率的最佳方法是什么。
我正在尝试在捕获图像之前找到分辨率。

【问题讨论】:

    标签: iphone ios avfoundation avcapturesession avcapture


    【解决方案1】:

    这是在支持 iOS 9 的设备上测试的所有相机 sessionPreset 的表格

    +-----------------------+--------------+---------------------------------+---------------+
    |        Device         |    Camera    |     AVCaptureSessionPreset      |  Resolution   |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPhone 4S             | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 640x480       |
    |                       |              | AVCaptureSessionPresetHigh      | 640x480       |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | not supported |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 3264x2448     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPhone 5/5C/5S/6/6+   |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 3264x2448     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPhone 6S/6S          |              |                                 |               |
    |                       | FRONT camera |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK camera  |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 4032x3024     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPad 2                |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 640x480       |
    |                       |              | AVCaptureSessionPresetHigh      | 640x480       |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | not supported |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 960x720       |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPad 3                |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 640x480       |
    |                       |              | AVCaptureSessionPresetHigh      | 640x480       |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | not supported |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 2592x1936     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPad 4/Air            |              |                                 |               |
    | iPad Mini 1/2/3       |              |                                 |               |
    | iPod 5G               |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 2592x1936     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPad Air 2            |              |                                 |               |
    | iPad Mini 4           |              |                                 |               |
    | iPad Pro              |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 3264x2448     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPod Touch 5          |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 2592x1936     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    | iPod Touch 6          |              |                                 |               |
    |                       | FRONT        |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 1280x960      |
    |                       |              | AVCaptureSessionPresetHigh      | 1280x720      |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | not supported |
    |                       | BACK         |                                 |               |
    |                       |              | AVCaptureSessionPresetPhoto     | 3264x2448     |
    |                       |              | AVCaptureSessionPresetHigh      | 1920x1080     |
    |                       |              | AVCaptureSessionPresetMedium    | 480x360       |
    |                       |              | AVCaptureSessionPresetLow       | 192x144       |
    |                       |              | AVCaptureSessionPreset640x480   | 640x480       |
    |                       |              | AVCaptureSessionPreset1280x720  | 1280x720      |
    |                       |              | AVCaptureSessionPreset1920x1080 | 1920x1080     |
    +-----------------------+--------------+---------------------------------+---------------+
    

    代码

    + (NSDictionary*) cameraResolutions {
        NSString* deviceModel = [self deviceModel];
    
        // iPhone 4S
        if ([deviceModel isEqualToString:@"iPhone4,1"]) {
    
            NSString* lFrontCam = @"640x480,480x360,192x144";
            NSString* lBackCam = @"3264x2448,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        // iPhone 5/5C/5S/6/6+/iPod 6
        else if ([deviceModel isEqualToString:@"iPhone5,1"]
                 || [deviceModel isEqualToString:@"iPhone5,2"]
                 || [deviceModel isEqualToString:@"iPhone5,3"]
                 || [deviceModel isEqualToString:@"iPhone5,4"]
                 || [deviceModel isEqualToString:@"iPhone6,1"]
                 || [deviceModel isEqualToString:@"iPhone6,2"]
                 || [deviceModel isEqualToString:@"iPhone7,1"]
                 || [deviceModel isEqualToString:@"iPhone7,2"]
                 || [deviceModel isEqualToString:@"iPod7,1"]) {
    
            NSString* lFrontCam = @"1280x960,1280x720,640x480,480x360,192x144";
            NSString* lBackCam = @"3264x2448,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        // iPhone 6S/6S+
        else if ([deviceModel isEqualToString:@"iPhone8,1"]
                 || [deviceModel isEqualToString:@"iPhone8,2"]) {
            NSString* lFrontCam = @"1280x960,1280x720,640x480,480x360,192x144";
            NSString* lBackCam = @"4032x3024,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        // iPad 2
        else if ([deviceModel isEqualToString:@"iPad2,1"]
                 || [deviceModel isEqualToString:@"iPad2,2"]
                 || [deviceModel isEqualToString:@"iPad2,3"]
                 || [deviceModel isEqualToString:@"iPad2,4"]) {
    
    
            NSString* lFrontCam = @"640x480,480x360,192x144";
            NSString* lBackCam = @"1280x720,960x720,480x360,192x144,640x480";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        // iPad 3
        else if ([deviceModel isEqualToString:@"iPad3,1"]
                 || [deviceModel isEqualToString:@"iPad3,2"]
                 || [deviceModel isEqualToString:@"iPad3,3"]) {
    
    
            NSString* lFrontCam = @"640x480,480x360,192x144";
            NSString* lBackCam = @"2592x1936,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        // iPad 4/Air/Mini/Mini 2/Mini 3/iPod 5G
        else if ([deviceModel isEqualToString:@"iPad3,4"]
                 || [deviceModel isEqualToString:@"iPad3,5"]
                 || [deviceModel isEqualToString:@"iPad3,6"]
                 || [deviceModel isEqualToString:@"iPad4,1"]
                 || [deviceModel isEqualToString:@"iPad4,2"]
                 || [deviceModel isEqualToString:@"iPad4,3"]
                 || [deviceModel isEqualToString:@"iPad4,4"]
                 || [deviceModel isEqualToString:@"iPad4,5"]
                 || [deviceModel isEqualToString:@"iPad4,6"]
                 || [deviceModel isEqualToString:@"iPad4,7"]
                 || [deviceModel isEqualToString:@"iPad4,8"]
                 || [deviceModel isEqualToString:@"iPod5,1"]) {
    
            NSString* lFrontCam = @"1280x960,1280x720,640x480,480x360,192x144";
            NSString* lBackCam = @"2592x1936,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                               @"back":lBackCam};
            return lCamResolutions;
        }
        // iPad Air 2/Mini 4/Pro
        else if ([deviceModel isEqualToString:@"iPad5,3"]
                 || [deviceModel isEqualToString:@"iPad5,4"]) {
            NSString* lFrontCam = @"1280x960,1280x720,640x480,480x360,192x144";
            NSString* lBackCam = @"3264x2448,1920x1080,1280x720,640x480,480x360,192x144";
            NSDictionary* lCamResolutions = @{@"front":lFrontCam,
                                              @"back":lBackCam};
            return lCamResolutions;
        }
        return nil;
    }
    

    【讨论】:

    • 感谢您的努力 ;)
    • 嗨罗曼,出色的工作。你能告诉我你是怎么得到这些数据的吗?实际上,我需要新 iPhone(SE、7、7+、8、8+、X)的数据。如果您能为新 iPhone 更新此列表,我将不胜感激。谢谢
    • 您好 Roman,您能否提供有关您的来源的链接或更多信息?
    【解决方案2】:

    使用下面的函数,您可以在捕获开始之前以编程方式从activeFormat 获取分辨率,但在添加输入和输出之前不能: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/activeFormat

    private func getCaptureResolution() -> CGSize {
        // Define default resolution
        var resolution = CGSize(width: 0, height: 0)
    
        // Get cur video device
        let curVideoDevice = useBackCamera ? backCameraDevice : frontCameraDevice
    
        // Set if video portrait orientation
        let portraitOrientation = orientation == .Portrait || orientation == .PortraitUpsideDown
    
        // Get video dimensions
        if let formatDescription = curVideoDevice?.activeFormat.formatDescription {
            let dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
            resolution = CGSize(width: CGFloat(dimensions.width), height: CGFloat(dimensions.height))
            if portraitOrientation {
                resolution = CGSize(width: resolution.height, height: resolution.width)
            }
        }
    
        // Return resolution
        return resolution
    }
    

    【讨论】:

    【解决方案3】:

    可能无法直接在捕获图像之前以编程方式获取相机分辨率。

    Apple 为各种设备和预设提供了一个表格:

    Capturing Still Images

    【讨论】:

    • 这太可悲了!好的,iOS8 仅支持 8MP 设备。但这仍然意味着硬编码值。如果下一个设备具有更高的相机分辨率,则应用需要使用新的硬编码值进行即时更新:(
    • @ecth 您可以在捕获开始之前以编程方式从activeFormat 获取分辨率,但在添加输入和输出之前不能。完整答案如下。
    【解决方案4】:

    可以这样做:

    /* Callback that is called when you activate the capture session. */
    - (void) avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification {
            CMFormatDescriptionRef formatDescription = nil;
            NSArray *ports = [deviceInput ports];
            AVCaptureInputPort *usePort = nil;
    
            for (AVCaptureInputPort *port in ports) {
                    if (usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo]) {
                            usePort = port;
                            break;
                    }
            }
    
            if (usePort != nil)
                    formatDescription = usePort.formatDescription;
            if (formatDescription != nil) {
                    /*------------>>>>>>> THIS IS YOUR RESOLUTION */
                    CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
                    self.frameSize = CGSizeMake(dimensions.width, dimensions.height);
                    [[NSNotificationCenter defaultCenter] removeObserver:self];
                    NSLog(@"Capturing with %dx%d", (int)self.frameSize.width, (int)self.frameSize.height);
            } else {
                    NSLog(@"Failed to detect resolution, using default one.");
                    self.frameSize = CGSizeMake(640.0f, 480.0f);
            }
    }
    
    - (id) init {
            if ((self = [super init])) {
                    /* Default framesize. */
                    self.frameSize = CGSizeMake(640.0f, 480.0f);
    
                    AVCaptureSession *capSession = [[AVCaptureSession alloc] init];
                    /* -------->>>>>>> REQUESTED RESOLUTION, CAN BE SOMTHING ELSE */
                    [capSession setSessionPreset:AVCaptureSessionPresetHigh];
    
    //                if (([modelName rangeOfString:@"iPhone 5"].length != 0) || ([modelName rangeOfString:@"iPhone 6"].length != 0)) {
    //                        [capSession setSessionPreset:AVCaptureSessionPresetHigh];
    //                } else {
    //                        [capSession setSessionPreset:AVCaptureSessionPreset640x480];
    //                }
    //                
                    self.captureSession = capSession;
    
                    [[NSNotificationCenter defaultCenter] addObserver:self
                                                             selector:@selector(avCaptureInputPortFormatDescriptionDidChangeNotification:)
                                                                 name:AVCaptureInputPortFormatDescriptionDidChangeNotification object:nil];
            }
            return self;
    }
    
    - (void) dealloc
    {
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            if (self.captureSession.isRunning)
                    [self.captureSession stopRunning];
            self.captureSession = nil;
    }
    
    - (void) addRawViewOutput
    {
        /* We setup the output */
        AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
    
            /* While a frame is processes in -captureOutput:didOutputSampleBuffer:fromConnection: delegate methods
               no other frames are added in the queue.
           If you don't want this behaviour set the property to NO */
        captureOutput.alwaysDiscardsLateVideoFrames = YES;
    
            /*We specify a minimum duration for each frame (play with this settings to avoid having too many frames waiting
         in the queue because it can cause memory issues). It is similar to the inverse of the maximum framerate.
         In this example we set a min frame duration of 1/10 seconds so a maximum framerate of 10fps. We say that
         we are not able to process more than 10 frames per second.*/
        //captureOutput.minFrameDuration = CMTimeMake(1, 10);
    
        /*We create a serial queue to handle the processing of our frames*/
        dispatch_queue_t queue;
        queue = dispatch_queue_create("com.YourApp.cameraQueue", NULL);
        [captureOutput setSampleBufferDelegate:self queue:queue];
    //  dispatch_release(queue);
    
        // Set the video output to store frame in BGRA (It is supposed to be faster)
        NSString *key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
        NSNumber *value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
        NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
        [captureOutput setVideoSettings:videoSettings];
    
            // Register an output
        [self.captureSession addOutput:captureOutput];
    }
    
    - (bool) startWithDevicePosition:(AVCaptureDevicePosition)devicePosition
    {
            AVCaptureDevice *videoDevice = [self cameraWithPosition:devicePosition];
    
            if (!videoDevice)
                    return FALSE;
    
            NSError *error;
    
            AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
            self.deviceInput = videoIn;
    
            if (!error)
            {
                    if ([[self captureSession] canAddInput:videoIn])
                    {
                            [[self captureSession] addInput:videoIn];
                    }
                    else
                    {
                            NSLog(@"Couldn't add video input");
                            return FALSE;
                    }
            }
            else
            {
                    NSLog(@"Couldn't create video input");
                    return FALSE;
            }
    
            [self addRawViewOutput];
            [self resume];
            return TRUE;
    }
    
    -(void) pause
    {
            self.paused = YES;
            if (self.captureSession.isRunning)
                    [captureSession stopRunning];
    }
    
    -(void) resume
    {
            if (!self.captureSession.isRunning)
                    [captureSession startRunning];
            self.paused = NO;
    }
    
    #pragma mark -
    #pragma mark AVCaptureSession delegate
    - (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection
    {
            CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
            /* Lock the image buffer */
            CVPixelBufferLockBaseAddress(imageBuffer,0);
    
            /* Get information about the image */
            uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
            size_t width = CVPixelBufferGetWidth(imageBuffer);
            size_t height = CVPixelBufferGetHeight(imageBuffer);
            size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer);
            size_t size = CVPixelBufferGetDataSize(imageBuffer);
    
            BGRAVideoFrame frame = {(int)width, (int)height, (int)stride, (int)size, baseAddress};
            [delegate frameReady:frame];
    
            /* We unlock the image buffer */
            CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    }
    

    【讨论】:

      【解决方案5】:

      尝试此代码以获得设备相机的最大图像分辨率:

      - (CMVideoDimensions) getCameraMaxStillImageResolution:(AVCaptureDevicePosition) cameraPosition {
      
          CMVideoDimensions max_resolution;
          max_resolution.width = 0;
          max_resolution.height = 0;
      
          AVCaptureDevice *captureDevice = nil;
      
          NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
          for (AVCaptureDevice *device in devices) {
              if ([device position] == cameraPosition) {
                  captureDevice = device;
                  break;
              }
          }
          if (captureDevice == nil) {
              return max_resolution;
          }
      
          NSArray* availFormats=captureDevice.formats;
      
          for (AVCaptureDeviceFormat* format in availFormats) {
              CMVideoDimensions resolution = format.highResolutionStillImageDimensions;
              int w = resolution.width;
              int h = resolution.height;
              if ((w * h) > (max_resolution.width * max_resolution.height)) {
                  max_resolution.width = w;
                  max_resolution.height = h;
              }
          }
      
          return max_resolution;
      }
      
      - (void) printCamerasInfo {
          CMVideoDimensions res;
          res = [self getCameraMaxStillImageResolution:AVCaptureDevicePositionBack];
          NSLog(@" Back  Camera max Image resolution: %d x %d", res.width, res.height);
          res = [self getCameraMaxStillImageResolution:AVCaptureDevicePositionFront];
          NSLog(@" Front Camera max Image resolution: %d x %d", res.width, res.height);
      }
      

      【讨论】:

      • 请不要将identical answers 发布到multiple questions。发布一个好的答案,然后投票/标记以关闭其他问题作为重复问题。如果问题不是重复的,调整您对该问题的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多