Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get shutter speed, aperture, and ISO values on iPhone

Tags:

ios

iphone

camera

I have written code like this :

-(IBAction)startCapture
{
    //session object
    captureSession = [[AVCaptureSession alloc]init];
    captureSession.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    previewLayer.frame = CGRectMake(0, 10, 320, 200); ////self.view.frame; //
    [self.view.layer addSublayer:previewLayer];

    NSError *error = nil;
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //input object
    AVCaptureDeviceInput *inputDevice = [[AVCaptureDeviceInput alloc]initWithDevice:device error:&error];
    [captureSession addInput:inputDevice];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [captureSession addOutput:stillImageOutput];
    [captureSession startRunning];
}

-(IBAction) captureNow
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);

         NSLog(@"exif Attachments:%@",exifAttachments);
         if (exifAttachments)
         {
             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

             UIImage *image = [[UIImage alloc] initWithData:imageData];
             self.vImage.image = image;
             // Do something with the attachments.

         }
         else
             NSLog(@"no attachments");   
     }];
}

to capture the images. But I want to know the shutter speed, ISO value and aperture while capturing. How can I find out these values? I tried the following:

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
    CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
    NSDictionary *exifDict = (NSDictionary *)exifAttachments;
    NSLog(@"\n exif data = %@",exifDict);

    CFNumberRef aperaturevalue = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifApertureValue, NULL);
    NSNumber *num = (NSNumber *)aperaturevalue;
    NSLog(@"\n AperatureValue : %@",num);

    CFNumberRef shutter = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifShutterSpeedValue, NULL);
    NSNumber *shunum = (NSNumber *)shutter;
    NSLog(@"\n shuttervalue : %@",shunum);

    CFArrayRef isoRef = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifISOSpeedRatings, NULL);
    NSArray *iso = (NSArray *)isoRef;
    NSLog(@"Iso value : %@",iso);
}

but it is giving output like this:

exif data = {
    ApertureValue = "2.970853605202583";
    ExposureMode = 0;
    ExposureProgram = 2;
    FNumber = "2.8";
    Flash = 32;
    MeteringMode = 1;
    SceneType = 1;
    SensingMethod = 2;
    WhiteBalance = 0;
}
2011-06-23 14:35:14.955 CameraExample[1464:307] 
 AperatureValue : (null)
2011-06-23 14:35:14.981 CameraExample[1464:307] 
 shuttervalue : (null)
2011-06-23 14:35:14.999 CameraExample[1464:307] Iso value : (null)
like image 438
Sandy_ios Avatar asked Nov 05 '22 19:11

Sandy_ios


1 Answers

Try to add this code in your block :

CFDictionaryRef exifDictRef = CMGetAttachment(imageSampleBuffer,kCGImagePropertyExifDictionary, NULL);
NSDictionary *exifDict = (NSDictionary *)exifDictRef;
for (id key in exifDict) {
    NSLog(@"key = %@, value = %@",key,[exifDict objectForKey:key]);
}

You should find the values you are looking for in these keys :

  • kCGImagePropertyExifShutterSpeedValue (result is a NSNumber)
  • kCGImagePropertyExifApertureValue (result is a NSNumber)
  • kCGImagePropertyExifISOSpeedRatings (result is a NSArray)
like image 174
Sylverb Avatar answered Nov 11 '22 04:11

Sylverb