luma-sensor-expo
v0.1.2
Published
Luma sensor for brightness
Maintainers
Readme
luma-sensor-expo
An Expo module that measures real-time brightness (luma) from the device camera. It samples camera frames at configurable intervals and calculates perceived brightness using the BT.601 luma formula.
Works on both iOS and Android with physical devices.
How it works
The module taps into the camera video feed and samples pixel brightness at 5 points (center + four 25%-inset corners). Brightness is calculated as:
Y = 0.299 * R + 0.587 * G + 0.114 * BReturns a value between 0.0 (black) and 1.0 (white).
Installation
npx expo install luma-sensor-expoFor bare React Native projects, run npx pod-install after installing.
Requirements
- Expo SDK 54+
- iOS 15.1+ / Android API 26+
- Physical device (camera required)
- Camera permission granted (use
expo-camerafor permission handling)
Usage
import { useEffect, useState, useRef } from 'react';
import { CameraView, useCameraPermissions } from 'expo-camera';
import { LumaSensorExpoModule, startMonitoring, stopMonitoring } from 'luma-sensor-expo';
export default function App() {
const [permission, requestPermission] = useCameraPermissions();
const [brightness, setBrightness] = useState<number | null>(null);
const subscriptionRef = useRef<ReturnType<typeof LumaSensorExpoModule.addListener> | null>(null);
const start = () => {
subscriptionRef.current = LumaSensorExpoModule.addListener(
'onBrightnessChange',
({ brightness }) => setBrightness(brightness)
);
startMonitoring(5); // process every 5th frame
};
const stop = () => {
stopMonitoring();
subscriptionRef.current?.remove();
};
useEffect(() => () => stop(), []);
// brightness is 0.0 - 1.0
const percent = brightness !== null ? Math.round(brightness * 100) : null;
return (
<CameraView style={{ flex: 1 }} facing="back">
{/* Your UI here */}
</CameraView>
);
}API
startMonitoring(frameSkip?: number)
Start continuous brightness monitoring. Emits onBrightnessChange events.
frameSkip— process every Nth frame (default:5). Higher = less CPU usage.
stopMonitoring()
Stop monitoring and clean up resources.
getLuma(): Promise<number>
One-shot brightness reading. Returns a promise that resolves with the current brightness value (0.0 - 1.0).
Events
onBrightnessChange
{ brightness: number } // 0.0 (dark) to 1.0 (bright)Subscribe via LumaSensorExpoModule.addListener('onBrightnessChange', callback).
Platform notes
| | iOS | Android |
|---|---|---|
| Camera access | Attaches to existing AVCaptureSession from expo-camera | Uses PixelCopy / Canvas screen capture |
| Frame source | AVCaptureVideoDataOutput | Choreographer frame callbacks |
| Preset | Uses expo-camera's session | N/A |
iOS: The module finds expo-camera's active AVCaptureSession via the view hierarchy and attaches a video data output to it. No separate camera session is created. Camera must be active (via <CameraView>) before calling startMonitoring().
Android: Captures the screen content (including camera preview) using PixelCopy (API 26+) with a Canvas fallback.
License
MIT
