Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get iPhone's camera resolution?

Is there any way to get the resolution of the iPhone's camera? Apparently the 3G has 1200x1600, and 3GS has 1500x2000, but how do I obtain these values from inside my code (without taking the picture). I need them to make some affine transform to the camera preview.

I could detect if it's 3G or 3GS to hardcode these values, but it's just the last resort.

like image 512
iamj4de Avatar asked May 30 '26 12:05

iamj4de


2 Answers

I think your "last resort" is a good solution.

What's wrong with detecting the model? The hardware isn't going to change for either of those models.. The only concern you might have is if the 3GSX-R or something comes out with a 16mpx camera. At which point you'd probably have to update your app anyways and could just add another value to the list.

I vote for model detection.

like image 99
snicker Avatar answered Jun 01 '26 02:06

snicker


The post is old, but it is almost first in google, and it has got no valid answer, so here's one more option:

Solution for iOS from 4.x to 7.x

It's all in terms of AV Foundation framework

After AVCaptureSession is configured and started you can find video dimensions inside [[[session.inputs.lastObject] ports].lastObject formatDescription] variable

Here's approximate code:

AVCaptureSession* session = ...;
AVCaptureDevice *videoCaptureDevice = ...;
AVCaptureDeviceInput *videoInput = ...;

[session beginConfiguration];
if ([session canAddInput:videoInput]) {[session addInput:videoInput];}
[session commitConfiguration];
[session startRunning];

//this is the clue
AVCaptureInputPort *port = videoInput.ports.lastObject;
if ([port mediaType] == AVMediaTypeVideo)
{
    videoDimensions = CMVideoFormatDescriptionGetDimensions([port formatDescription]);
}

Solution for iOS8

Apple did change everything again: now you must subscribe for AVCaptureInputPortFormatDescriptionDidChangeNotification

Here is the sample:

-(void)initSession
{
    AVCaptureSession* session = ...;
    AVCaptureDevice *videoCaptureDevice = ...;
    AVCaptureDeviceInput *videoInput = ...;

    [session beginConfiguration];
    if ([session canAddInput:videoInput]) {[session addInput:videoInput];}
    [session commitConfiguration];
    [session startRunning];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(avCaptureInputPortFormatDescriptionDidChangeNotification:)
            name:@"AVCaptureInputPortFormatDescriptionDidChangeNotification"
            object:nil];
}

-(void)avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification
{
    AVCaptureInputPort *port = [videoInput.ports objectAtIndex:0];
    CMFormatDescriptionRef formatDescription = port.formatDescription;
    if (formatDescription) {
        videoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
    }

}
like image 23
Rosik Avatar answered Jun 01 '26 01:06

Rosik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!