Accessing the ambient light sensor in iOS

天大地大妈咪最大 提交于 2019-11-29 12:04:18

问题


I'm working on a project in which it is really necessary to access the ambient light sensor.

I searched a lot in Google and Stackoverflow, but couldn't find any useful information. Is it even possible to do so?

I also tried to calculate the ambient light value by calculating the brightness out of the camera input, but the results aren't really precise, as the camera makes lot's of adjustments to the images, which distort the results.


回答1:


To read the ambient light sensor data, you need to use IOHID in the IOKit framework (Reference)

http://iphonedevwiki.net/index.php/AppleISL29003

http://iphonedevwiki.net/index.php/IOKit.framework




回答2:


I solve this problem With access to the camera

- (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];
float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];

//THIS IS INFORMATION THAT COMES FROM THE SENSOR
_Sensor = [[NSNumber numberWithFloat:brightnessValue] stringValue];
 NSLog(@" %@",_Sensor);

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){


        if ([_Sensor isEqualToString:@"-5.575654"]) {

       // YOU CODE HER

        }
        else {

       // YOU CODE HER
    }

});

}


来源:https://stackoverflow.com/questions/18271145/accessing-the-ambient-light-sensor-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!