读取 iPhone 的环境光传感器

发布于 2024-11-14 19:19:40 字数 238 浏览 1 评论 0原文

我注意到我的 iPhone 上,在阳光直射几秒钟后,屏幕会调整为更亮、更暗等。我想知道是否有办法与这个传感器交互?

我有一个在外面使用的应用程序。当您进入直射光时,在调整之前的一段时间内很难看清屏幕。即便如此,它并不总是像我希望的那样明亮。我想为室外观看实现高对比度皮肤,为室内观看实现低对比度。

是否可以读取光传感器数据?如果可以,如何提取这些传感器值?

不过,我假设有一个光传感器,因为相机知道何时使用闪光灯。

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?

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月牙弯弯 2024-11-21 19:19:40

另一方面,这是一个不同的想法(也许是一个愚蠢的想法),使用设备屏幕的亮度,您可以获得外部条件的一些值。

从 0.12(深色)到 0.99(浅色)

下一行将获取这些值,尝试一下,在设备上打开和关闭一些灯以获得不同的值。

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

显然,应该打开自动亮度功能才能使其正常工作。

问候。

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)

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.

渡你暖光 2024-11-21 19:19:40

要读取环境光传感器数据,需要使用IOKit框架中的IOHID。

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

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

但是,这需要私有标头,所以如果您使用它,苹果可能不会让你的应用进入应用商店。

我不断询问iOS论坛未来是否支持环境光传感器读数,但没有结果。

To read the ambient light sensor data, you need to use IOHID in the IOKit framework.

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

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.

I continually ask the iOS forums whether there will be support for ambient light sensor readings in the future, but to no avail.

一花一树开 2024-11-21 19:19:40

您实际上可以使用相机来执行此操作,这与用户的屏幕亮度设置无关(即使“自动亮度”关闭也可以工作)。

您可以从视频帧的元数据中读取亮度值,正如我在此 Stack Overflow 答案中所解释的那样。

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.

绮筵 2024-11-21 19:19:40

尝试使用 GSEventSetBacklightLevel();,这需要 。这就是如何以编程方式调整亮度级别。还有一个 get 选项,所以我认为它可能包含您想要的信息。

Try using GSEventSetBacklightLevel();, which requires <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.

表情可笑 2024-11-21 19:19:40

对于 iOS 14 及更高版本,Apple 提供了 SensorKit ( https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor )用于显式访问各种传感器和系统日志(通话记录、消息日志等)。除了原始勒克斯值之外,您还可以获得环境光的色度以及相对于设备传感器的方向。

(来自 https://developer.apple.com/documentation/sensorkit/srambientlightsample

测量光照度

var chromaticity: SRAmbientLightSample.Chromaticity 坐标对
它描述了样本的光亮度和色调。

struct SRAmbientLightSample.Chromaticity 一个坐标对
描述光的亮度和色调。

var lux:测量值,描述
样品的光通量。

var 位置:SRAmbientLightSample.SensorPlacement 灯光的
相对于传感器的位置。

enum SRAmbientLightSample.SensorPlacement 方向值
描述光源相对于传感器的位置。

但是,您需要请求批准此类应用程序才能被接受并在 App Store 上发布。

For iOS 14 and above, Apple has provided SensorKit (https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor ) for explicit access to all kinds of sensors and system logs (call logs, message logs, etc.). In addition to the raw lux value, you can also get the chromaticity of the ambient light and the orientation relative to the device sensor.

(From https://developer.apple.com/documentation/sensorkit/srambientlightsample )

Measuring Light Level

var chromaticity: SRAmbientLightSample.Chromaticity A coordinate pair
that describes the sample’s light brightness and tint.

struct SRAmbientLightSample.Chromaticity A coordinate pair that
describes light brightness and tint.

var lux: Measurement An object that describes the
sample’s luminous flux.

var placement: SRAmbientLightSample.SensorPlacement The light’s
location relative to the sensor.

enum SRAmbientLightSample.SensorPlacement Directional values that
describe light-source location with respect to the sensor.

However, you need to request for approval for such an App to be accepted and published on App Store.

一笑百媚生 2024-11-21 19:19:40

对于 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 )
    }
}

For Swift 5, here is how to use the brightness detection which indirectly gives you the luminosity of the outside:

/// 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 )
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文