使用ARKit读取iOS光传感器

时间:2017-08-01 14:09:48

标签: ios arkit light-sensor

有没有办法使用ARKit访问iOS设备的环境光线传感器,而根本不使用AR?

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

换句话说,我可以访问" ambientIntensity"的值。没有创建AR场景。

3 个答案:

答案 0 :(得分:3)

请参阅the docs for ARLightEstimate.ambientIntensity

  

此值基于相机设备的内部曝光补偿

换句话说,如果您想使用设备相机来估算当地的照明条件而不是使用ARKit,那么最好使用camera APIs。 (首先,所有iOS 11设备和几个早期iOS版本都提供这些API,而不是需要ARKit的苛刻的操作系统/硬件要求。)

快速浏览一下你需要做什么:

  1. 设置AVCaptureSession并选择相机AVCaptureDevice 你要的那个。您可能需要也可能不需要连接视频/照片捕获输出(在您的情况下将大部分未使用)。
  2. 开始运行捕获会话。
  3. 使用KVO监控AVCaptureDevice上的曝光,温度和/或白平衡相关属性。
  4. 您可以在Apple的AVCamManual sample code中找到(较旧的,ObjC)代码,涵盖所有这些(以及更多内容,因此您需要提取与您相关的部分)。

答案 1 :(得分:1)

您不需要ARSCNView但是您需要有一个正在运行的ARSession https://developer.apple.com/documentation/arkit/arsession

完成设置后,您可以致电currentFrame,这会为您提供ARFrame,其lightEstimate属性包含ambientIntensity估算值。

答案 2 :(得分:0)

是的,在captureOutput函数中可以在改写协议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")
        }
}