[英]Accessing the ambient light sensor in iOS
我正在做一個項目,在這個項目中確實有必要訪問環境光傳感器。
我在 Google 和 Stackoverflow 上搜索了很多,但找不到任何有用的信息。 甚至有可能這樣做嗎?
我還嘗試通過計算相機輸入的亮度來計算環境光值,但結果並不十分精確,因為相機對圖像進行了大量調整,從而扭曲了結果。
要讀取環境光傳感器數據,需要在IOKit框架中使用IOHID( 參考 )
我可能來不及參加聚會,但我從開發人員文檔中了解到SensorKit允許我們這樣做,
https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor
我通過訪問相機解決了這個問題
- (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.