2012-12-04 24 views
6

我正在使用iPhone/iPad攝像頭獲取視頻流並在視頻流上進行識別,但隨着燈光變化,它對強健性產生了負面影響。我已經在不同的燈光下測試了不同的設置,並且可以使其工作,但是嘗試在運行時調整設置是我需要的。有沒有辦法獲得相機流的iOS上的亮度級別?

我可以計算每一幀的簡單亮度檢查,但相機會調整並拋出我的結果。我可以觀察到劇烈的變化,然後運行檢查,但逐漸改變也會導致我的結果。

理想情況下,我想訪問流的相機/ EXIF數據,看看它是如何註冊未過濾的亮度,有沒有辦法做到這一點?

(我工作的設備的iOS 5及以上)

謝謝

+0

如果有的話,請使用[光感應器](http://stackoverflow.com/questions/6408840/about-ambient-light-sensor-in-iphone)。 –

回答

8

可提供的iOS 4.0及以上。可以從CMSampleBufferRef獲取EXIF信息。

//Import ImageIO & include framework in your project. 
#import <ImageIO/CGImageProperties.h> 

在你的樣品緩衝委託免費橋接將得到CoreMedia的CMGetAttachment結果的NSDictionary的。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 
    NSDictionary* dict = (NSDictionary*)CMGetAttachment(sampleBuffer, kCGImagePropertyExifDictionary, NULL); 
1

完整代碼,在自己的應用程序中使用:

- (void)setupAVCapture { 

//-- Setup Capture Session. 
_session = [[AVCaptureSession alloc] init]; 
[_session beginConfiguration]; 

//-- Set preset session size. 
[_session setSessionPreset:AVCaptureSessionPreset1920x1080]; 

//-- Creata a video device and input from that Device. Add the input to the capture session. 
AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
if(videoDevice == nil) 
    assert(0); 

//-- Add the device to the session. 
NSError *error; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; 
if(error) 
    assert(0); 

[_session addInput:input]; 

//-- Create the output for the capture session. 
AVCaptureVideoDataOutput * dataOutput = [[AVCaptureVideoDataOutput alloc] init]; 
[dataOutput setAlwaysDiscardsLateVideoFrames:YES]; // Probably want to set this to NO when recording 

//-- Set to YUV420. 
[dataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] 
                 forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; // Necessary for manual preview 

// Set dispatch to be on the main thread so OpenGL can do things with the data 
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; 

[_session addOutput:dataOutput]; 
[_session commitConfiguration]; 

[_session startRunning]; 
} 

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, 
                   sampleBuffer, kCMAttachmentMode_ShouldPropagate); 
    NSDictionary *metadata = [[NSMutableDictionary alloc] 
           initWithDictionary:(__bridge NSDictionary*)metadataDict]; 
    CFRelease(metadataDict); 
    NSDictionary *exifMetadata = [[metadata 
            objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy]; 
    self.autoBrightness = [[exifMetadata 
         objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue]; 

    float oldMin = -4.639957; // dark 
    float oldMax = 4.639957; // light 
    if (self.autoBrightness > oldMax) oldMax = self.autoBrightness; // adjust oldMax if brighter than expected oldMax 

    self.lumaThreshold = ((self.autoBrightness - oldMin) * ((3.0 - 1.0)/(oldMax - oldMin))) + 1.0; 

    NSLog(@"brightnessValue %f", self.autoBrightness); 
    NSLog(@"lumaThreshold %f", self.lumaThreshold); 
} 

的lumaThreshold變量被作爲一個統一的變量,以我的片段着色器,它乘與Y樣質地找到基於理想光度對環境的亮度。現在,它使用後置攝像頭;我可能會切換到前置攝像頭,因爲我只是改變屏幕的「亮度」以適應室內/室外觀看,並且用戶的眼睛位於相機的前部(而不是後部)。

相關問題