2011-06-22 44 views
4

在此先感謝。如何獲得快門速度,光圈和ISO值在iphone

我寫這樣的代碼:

-(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"); 
    }]; 
} 

捕捉圖像。但是我想知道拍攝時的快門速度,ISO值和光圈。我怎樣才能找出這些值?任何人都可以幫助找到這個嗎?

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer,NSError *誤差) { 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); 

,我試圖得到這樣的值,但它給輸出:

EXIF數據= { 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:(空) 2011-06-23 14:35:14.981 CameraExample [1464:307] shuttervalue:(空) 2011 -06-23 14:35:14.999 CameraExample [1464:307] ISO值:(空) 這樣

+0

你在captureStillImageAsynchronouslyFromConnection泄漏:塊。將圖像設置爲vImage.image後,圖像需要釋放。你也泄漏輸出設置(NSDictionary) –

回答

2

嘗試在你的塊添加以下代碼:

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

你應該找到你值正在尋找這些鑰匙:

  • kCGImagePropertyExifShutterSpeedValue(結果是一個NSNumber的)
  • kCGImagePropertyExifApertureValue(結果是一個NSNumber的)
  • kCGImagePropertyExifISOSpeedRatings(結果是一個的NSArray)
+0

謝謝你的回覆。我試圖找出,但問題是,[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer,NSError * error) 在這個塊,即使我得到的輸出也沒有執行block.Why它發生了像這樣,你有什麼想法。 –

+0

@sandhya:您可能在stillImageOutput.connections中沒有AVMediaTypeVideo連接(可能stillImageOutput甚至爲零)。如果是這樣,這意味着你沒有在正確的時間打電話給你!你可以檢查這個項目,看看事情是如何完成的:https://github.com/kronick/DoubleCamera(檢查Classes/CameraOverlayController.m文件)瞭解你的應用程序出了什麼問題! – Sylverb

+0

我正在捕獲圖像。但沒有獲得快門,iso值。 –