简体   繁体   English

读取 iPhone 的环境光传感器

[英]Reading the iPhone's Ambient Light sensor

I notice on my iPhone, after a few seconds of being in direct sun light, the screen will adjust to become brighter, dimmer etc. I was wondering if there was a way to interact with this sensor?我注意到在我的 iPhone 上,在阳光直射几秒钟后,屏幕会调整为更亮、更暗等。我想知道是否有办法与这个传感器进行交互?

I have an application which is used outside.我有一个在外面使用的应用程序。 When you go into direct light, it becomes very difficult to see the screen for a few momments, before it adjusts.当您将 go 置于直射光下时,在调整之前,您很难在几分钟内看到屏幕。 And even then, it's not always as bright as I'd like it to be.即便如此,它并不总是像我希望的那样明亮。 I would like to implement a high contrast skin for outdoor viewing, and a low contrast for indoor viewing.我想为户外观看实现高对比度皮肤,为室内观看实现低对比度。

Is this possible to read light sensor data, and if so, how do I extract these sensor values?这是否可以读取光传感器数据,如果可以,我如何提取这些传感器值?

I would assume there is a light sensor however, as the camera knows when to use the flash.我会假设有一个光传感器,因为相机知道何时使用 flash。

On the other hand this is a different idea (maybe a silly one), using the brightness of the device's screen you can get some value of the external conditions.另一方面,这是一个不同的想法(可能是一个愚蠢的想法),使用设备屏幕的亮度可以获得一些外部条件的值。

From 0.12 (Dark) to 0.99 (Light)从 0.12(暗)到 0.99(亮)

The next line will get those values, give it a try, put some light on and off over the device to get different values.下一行将获取这些值,试一试,在设备上打开和关闭一些灯以获得不同的值。

NSLog(@"Screen Brightness: %f",[[UIScreen mainScreen] brightness]);

Obviously Automatic Brightness feature should be turned on in order to get this to work.显然,应该打开自动亮度功能才能使其正常工作。

Regards.问候。

To read the ambient light sensor data, you need to use IOHID in the IOKit framework.要读取环境光传感器数据,需要在 IOKit 框架中使用 IOHID。

http://iphonedevwiki.net/index.php/AppleISL29003 http://iphonedevwiki.net/index.php/AppleISL29003

http://iphonedevwiki.net/index.php/IOKit.framework http://iphonedevwiki.net/index.php/IOKit.framework

However, this requires private headers, so if you use it, Apple probably won't let your app into the app store.但是,这需要私有标头,因此如果您使用它,Apple 可能不会让您的应用进入应用商店。

I continually ask the iOS forums whether there will be support for ambient light sensor readings in the future, but to no avail.我不断询问 iOS 论坛将来是否会支持环境光传感器读数,但无济于事。

You can actually use the camera to do this, which is independent of the user's screen brightness settings (and works even if Automatic Brightness is OFF).您实际上可以使用相机来执行此操作,这与用户的屏幕亮度设置无关(即使自动亮度关闭也可以使用)。

You read the Brightness Value from the video frames' metadata as I explain in this Stack Overflow answer .正如我在此 Stack Overflow 答案中解释的那样,您可以从视频帧的元数据中读取亮度值。

Try using GSEventSetBacklightLevel();尝试使用GSEventSetBacklightLevel(); , which requires <GraphicsServices/GraphicsServices.h> . ,这需要<GraphicsServices/GraphicsServices.h> This is how one can programmatically adjust the brightness levels.这就是人们可以通过编程方式调整亮度级别的方式。 There is also a get option, so I think that may have the information you're after.还有一个get选项,所以我认为这可能包含您想要的信息。

For Swift 5, here is how to use the brightness detection which indirectly gives you the luminosity of the outside:对于 Swift 5,以下是如何使用亮度检测,间接为您提供外部亮度:

/// A view controller (you can use any UIView or AnyObj)
class MyViewConroller: UIViewController { 

    /// Remove observers on deinit
    deinit {
        removeObservers()
    }

    // MARK: - Observers management helpers

    /// Add my observers to the vc
    func addObservers() {

        NotificationCenter.default.addObserver(self, selector: #selector(onScreenBrightnessChanged(_:)), name: UIScreen.brightnessDidChangeNotification, object:nil)
    }

    /// Clean up observers
    func removeObservers() {
        NotificationCenter.default.removeObserver(self)
    }

    /// Load the views
    func loadView() {
        // Add my observes to the vc
        addObservers()
    }

    /**
    Handles brightness changes
    */
    @objc func onScreenBrightnessChanged(_ sender: Notification) {

        // Tweak as needed: 0.5 is a good value for me
        let isDark = UIScreen.main.brightness < 0.5.   // in 0...1
        // Do whatever you want with the `isDark` flag: here I turn the headlights off
        vehicle.turnOnTheHeadlights( isDark )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM