1

Is there a way to access the ambient light sensor of an iOS device, using the ARKit, without using the AR at all?

https://developer.apple.com/documentation/arkit/arlightestimate/2878308-ambientintensity

In other words, can I access the value of "ambientIntensity" without creating an AR scene.

Denis T
  • 13
  • 1
  • 4

3 Answers3

4

See the docs for ARLightEstimate.ambientIntensity:

This value is based on the internal exposure compensation of the camera device

In other words, if you want to use the device camera to estimate local lighting conditions and aren't otherwise using ARKit, you might be better off using the camera APIs. (For one thing, those APIs are available on all iOS 11 devices and several earlier iOS versions, rather than needing the steep OS/hardware requirements of ARKit.)

A quick tour of what you'll need to do there:

  1. Set up an AVCaptureSession and choose the camera AVCaptureDevice that you want. You may or may not need to wire up a video/photo capture output (which in your case will be mostly unused).
  2. Start running the capture session.
  3. Use KVO to monitor the exposure, temperature, and/or white balance related properties on AVCaptureDevice.

You can find (older, ObjC) code covering all this (and a lot more, so you'll need to extract the parts that are relevant to you) in Apple's AVCamManual sample code.

rickster
  • 124,678
  • 26
  • 272
  • 326
1

You don't need an ARSCNView but you do need to have a running ARSession https://developer.apple.com/documentation/arkit/arsession

Once you have that set up you can call currentFrame which will give you an ARFrame which has a lightEstimate property which contains the ambientIntensity estimate.

Tristan Burnside
  • 2,546
  • 1
  • 16
  • 23
  • Thank you, I managed to make it works (even if the value is a bit weird). I used `self.arConfig = [ARWorldTrackingSessionConfiguration new]; self.arConfig.lightEstimationEnabled = YES;` And I set the permission for using the camera. – Denis T Aug 01 '17 at 16:23
  • Sadly it seems that it's not possible to use it with the front camera, which I need. – Denis T Aug 02 '17 at 14:51
0

Yes, in the captureOutput function to override when adapting the Protocol AVCaptureVideoDataOutputSampleBufferDelegate

override func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

        //Retrieving EXIF data of camara frame buffer
        let rawMetadata = CMCopyDictionaryOfAttachments(allocator: nil, target: sampleBuffer, attachmentMode: kCMAttachmentMode_ShouldPropagate)
        let metadata = CFDictionaryCreateMutableCopy(nil, 0, rawMetadata) as NSMutableDictionary
        let exifData = metadata.value(forKey: "{Exif}") as? NSMutableDictionary
        
        if let light = exifData?[kCGImagePropertyExifBrightnessValue] as? NSNumber {
            print("Light \(light.floatValue)")
        } else {
            print("problem with light")
        }
}
Omar El Malak
  • 179
  • 1
  • 8