使用ARKit读取iOS光传感器

使用ARKit读取iOS光传感器,ios,arkit,light-sensor,Ios,Arkit,Light Sensor,有没有一种方法可以使用ARKit访问iOS设备的环境光传感器,而不使用AR 换句话说,我可以在不创建AR场景的情况下访问ambientIntensity的值。您不需要ARSCNView,但需要运行ARSession 设置完成后,您可以调用currentFrame,它将为您提供一个ARFrame,该ARFrame具有lightEstimate属性,该属性包含环境强度估计。您不需要ARSCNView,但需要运行ARSession 设置完成后,您可以调用currentFrame,它将为您提供一个ARF

有没有一种方法可以使用ARKit访问iOS设备的环境光传感器,而不使用AR

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

您不需要ARSCNView,但需要运行ARSession

设置完成后,您可以调用currentFrame,它将为您提供一个ARFrame,该ARFrame具有lightEstimate属性,该属性包含环境强度估计。

您不需要ARSCNView,但需要运行ARSession

设置完成后,您可以调用currentFrame,它将为您提供一个ARFrame,该ARFrame具有lightEstimate属性,该属性包含环境强度估计值。

请参阅:

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

换句话说,如果您想使用设备摄像头估计局部照明条件,而不是使用ARKit,那么最好使用。首先,这些API可以在所有iOS 11设备和几个早期iOS版本上使用,而不需要ARKit苛刻的操作系统/硬件要求

快速了解您需要在那里做什么:

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

请参阅:

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

换句话说,如果您想使用设备摄像头估计局部照明条件,而不是使用ARKit,那么最好使用。首先,这些API可以在所有iOS 11设备和几个早期iOS版本上使用,而不需要ARKit苛刻的操作系统/硬件要求

快速了解您需要在那里做什么:

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

是的,在调整协议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")
        }
}

是,在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")
        }
}

谢谢,我设法使它工作,即使值有点奇怪。我使用self.arConfig=[ARWorldTrackingSessionConfiguration new];self.arConfig.lightEstimationEnabled=是;我设置了使用摄像头的权限。遗憾的是,我需要的前置摄像头似乎无法使用它。谢谢你,即使值有点奇怪,我还是设法让它工作了。我使用self.arConfig=[ARWorldTrackingSessionConfiguration new];self.arConfig.lightEstimationEnabled=是;我设置了使用摄像头的权限。遗憾的是,似乎无法将其与我需要的前置摄像头一起使用。