使用 ARKit 读取 iOS 光传感器

Reading iOS light sensor with ARKit

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

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

换句话说,我可以在不创建AR场景的情况下访问"ambientIntensity"的值吗?

您不需要 ARSCNView,但您确实需要 运行 ARSession https://developer.apple.com/documentation/arkit/arsession

设置完成后,您可以调用 currentFrame,这将为您提供一个 ARFrame,其中包含一个 lightEstimate 属性,其中包含 ambientIntensity估计。

the docs for ARLightEstimate.ambientIntensity:

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

换句话说,如果您想使用设备摄像头来估计局部光照条件而不使用 ARKit,您最好使用 camera APIs。 (一方面,这些 API 在所有 iOS 11 设备和几个更早的 iOS 版本上可用,而不需要 ARKit 的陡峭 OS/hardware 要求。)

快速浏览您需要在那里做什么:

  1. 设置AVCaptureSession并选择相机AVCaptureDevice 你要的那个。您可能需要也可能不需要连接 video/photo 捕获输出(在您的情况下,这将大部分未使用)。
  2. 开始 运行 捕获会话。
  3. 使用 KVO 监控 AVCaptureDevice 上的曝光、温度、and/or 白平衡相关属性。

您可以在 Apple 的 AVCamManual sample code.[=18= 中找到涵盖所有这些内容(以及更多内容,因此您需要提取与您相关的部分)的(较旧的 ObjC)代码]

是的,在适配协议AVCaptureVideoDataOutputSampleBufferDelegate时覆盖captureOutput函数

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")
        }
}