简体   繁体   English

访问iOS中的环境光传感器

[英]Accessing the ambient light sensor in iOS

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.我在 Google 和 Stackoverflow 上搜索了很多,但找不到任何有用的信息。 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.我还尝试通过计算相机输入的亮度来计算环境光值,但结果并不十分精确,因为相机对图像进行了大量调整,从而扭曲了结果。

To read the ambient light sensor data, you need to use IOHID in the IOKit framework ( Reference ) 要读取环境光传感器数据,需要在IOKit框架中使用IOHID( 参考

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

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

I could be too late to the party but I learn from developer documentation that SensorKit would allow us to do it,我可能来不及参加聚会,但我从开发人员文档中了解到SensorKit允许我们这样做,

https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor

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
    }

});

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM