Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

ios - detecting if iPhone is in a dark room

Is there a way to use the iPhone proximity sensor to detect whether the phone is in a room with no light?

This question seems to imply that this is not possible...Does iPhone allow Light sensors as input?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a much simpler way of using the camera to find out how bright a scene is. (Obviously, it only reads the data that can be "seen" in the camera's field of view, so it's not a true ambient light sensor...)

Using the AVFoundation framework, set up a video input and then, using the ImageIO framework, read the metadata that's coming in with each frame of the video feed (you can ignore the actual video data):

#import <ImageIO/ImageIO.h>

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

You now have the Brightness Value for the scene updated (typically—you can configure this) 15-30 times per second. Lower numbers are darker.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...