Get iPhone's camera resolution?
Asked Answered
M

6

3

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.

Mcclelland answered 28/10, 2009 at 15:51 Comment(0)
H
7

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.

Howbeit answered 28/10, 2009 at 16:9 Comment(2)
I was put off exactly by that reason: if Apple makes another iPhone with different camera resolution, I would be forced to make update of my application. Oh well, guess I will need to stick to this then. Thanks for the response.Mcclelland
The reason why I put it that way is because if Apple makes another iPhone, there are quite likely OTHER reasons that you'd need to update your app.Howbeit
C
3

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);
    }

}
Compliance answered 15/8, 2012 at 16:26 Comment(2)
Last object returns dimensions as 0x0. 1st object instead is giving the dimensions of the video (or rather video frames)..Defrayal
@Defrayal in normal way you should probe all port and check if [[port mediaType] isEqualToString:AVMediaTypeVideo]Compliance
B
2

Do you really need to know the resolution of the image to set up the affine transform? Since the view is pre-set-up to cover the width of the screen, you can alter the transform based on what fraction of the screen you want to take up, i.e. if you need the preview to be 160 pixels across, just shrink it 50% from the default. I'm doing this in an app now, and it works on new and old iPhones...

Brawley answered 19/11, 2009 at 17:32 Comment(4)
This is probably the right way to do it. I assume you're referring to the cameraViewTransform property on UIImagePickerController?Frazil
yes, I use code like self.camera.cameraViewTransform = CGAffineTransformTranslate(CGAffineTransformMakeScale(0.5f, 0.5f), -160, -211); to get things to line up...Brawley
Because there are cases when I need to make complicated transformation, made up from translation + rotation + scale. And without the camera resolution, it's not possible.Mcclelland
you just need to figure the transform from the original screen preview rectangle to wherever you'd like the preview to appear on the screen: the actual camera resolution doesn't figure into the math.Brawley
P
0

Since Apple doesn't let you talk to the hardware directly, there's not really much choice for a legitimate App Store product.

Patagonia answered 28/10, 2009 at 16:23 Comment(0)
D
0

While I think snicker's answer is definitely the correct one. I thought I'd post this for fun.

You could look through the users stored photo album on the iPhone (I am making a bold assumption that this can be done programatically on the iPhone without restrictions!!) making the assumption that the photos on the phone were mostly taken with the phone it's self, you could then work out what the average photo resolution is and roll with that.

Discalced answered 28/10, 2009 at 17:22 Comment(4)
People frown upon "fun" answers. I had to learn to start making jokes in comments =]Howbeit
Unfortunately, your bold assumption is not correct. There is no documented way to browse through the photo library programmatically.Sparrow
Ah =] The dangers of bold assumptions! Oh well, lesson learned!Discalced
Also, people can synchronize their own photos into the phone, which guarantees that you will not have a certain value.Howbeit
I
0

Why don't you want to take a picture? Use UIImagePickerController's takePicture method, instructing the user that this is a necessary calibration step. After that, look at the picture's resolution, persistently save the value, and delete the picture that you took. After that, you'll have your resolution and will be able to apply the transform thereon.

Inadmissible answered 29/10, 2009 at 9:2 Comment(1)
Because I don't want to make transformation to the taken picture, but to the preview camera. I can't call takePicture to do that.Mcclelland

© 2022 - 2024 — McMap. All rights reserved.