Add a camera app: photo and video on libcamera or V4L2
CI / test (push) Failing after 52s

- Discover libcamera cameras, falling back to V4L2; label them Back, Front or External and pick the rear sensor first
- Photo mode: JPEG saves to ~/Pictures/Camera, with an optional high-res still capture
- Video mode: H.264 MP4 recording through ffmpeg, saved to ~/Videos/Camera
- Rear torch LED toggle via sysfs, falling back to logind SetBrightness
- GTK3 + Cairo Wayland UI with a viewfinder, timer, grid, camera switch and gallery view
- Add the desktop entry, icon and camera tests
This commit is contained in:
nandi committed 2026-11-28 12:22:36 -08:00
1 parent 74235cb0ca
commit 1c954aa657
10 files changed
+3479 -8

No files matched your search

+1881
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+11
View File
@@ -0,0 +1,11 @@
[Desktop Entry]
Type=Application
Name=Camera
GenericName=Camera
Comment=Take photos and record videos
Exec=/usr/local/bin/chandra-camera
Icon=chandra-camera
Terminal=false
Categories=AudioVideo;Video;Photography;Utility;
StartupNotify=true
StartupWMClass=chandra-camera
+25
View File
@@ -0,0 +1,25 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128">
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#1b223a"/>
<stop offset="100%" stop-color="#0f1322"/>
</linearGradient>
<linearGradient id="lensRing" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#8ba6ff"/>
<stop offset="100%" stop-color="#4e6ee8"/>
</linearGradient>
<radialGradient id="glass" cx="40%" cy="35%" r="60%">
<stop offset="0%" stop-color="#2c3e75"/>
<stop offset="55%" stop-color="#11172b"/>
<stop offset="100%" stop-color="#080b14"/>
</radialGradient>
</defs>
<rect x="8" y="8" width="112" height="112" rx="28" fill="url(#bg)" stroke="#3b4973" stroke-width="2"/>
<path d="M36 42 L44 33 C46 31 48 30 51 30 L77 30 C80 30 82 31 84 33 L92 42 L98 42 C104 42 108 46 108 52 L108 86 C108 92 104 96 98 96 L30 96 C24 96 20 92 20 86 L20 52 C20 46 24 42 30 42 Z"
fill="#242c47" stroke="#53669c" stroke-width="2"/>
<circle cx="64" cy="68" r="22" fill="#0d111d" stroke="url(#lensRing)" stroke-width="4"/>
<circle cx="64" cy="68" r="15" fill="url(#glass)"/>
<circle cx="58" cy="61" r="4.5" fill="#9fb7ff" fill-opacity="0.72"/>
<circle cx="71" cy="74" r="2.2" fill="#6e8cff" fill-opacity="0.55"/>
<circle cx="94" cy="52" r="3.5" fill="#6e8cff"/>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
"""Host-side unit tests for chandra-camera frame/camera helpers (no hardware required)."""
import importlib.machinery
import importlib.util
import os
import sys
import unittest
try:
import cv2
import numpy as np
HAS_CV2_NUMPY = True
except ImportError:
cv2 = None
np = None
HAS_CV2_NUMPY = False
CAM_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "chandra-camera")
_loader = importlib.machinery.SourceFileLoader("chandra_camera_mod", CAM_PATH)
_spec = importlib.util.spec_from_loader("chandra_camera_mod", _loader)
cam_mod = importlib.util.module_from_spec(_spec)
sys.modules["chandra_camera_mod"] = cam_mod
_loader.exec_module(cam_mod)
class TestCameraHelpers(unittest.TestCase):
def test_sort_and_label_cameras(self):
raw = [
("/base/front", "libcamera", "imx371", 0, 90, 4656 * 3496),
("/base/back2", "libcamera", "imx376", 1, 270, 2592 * 1944),
("/base/back1", "libcamera", "imx519", 1, 270, 4656 * 3496),
]
cams = cam_mod.sort_and_label_cameras(raw)
self.assertEqual([c.model for c in cams], ["imx519", "imx371", "imx376"])
self.assertEqual([c.label for c in cams], ["Back · imx519", "Front · imx371", "Back 2 · imx376"])
self.assertEqual(cam_mod.select_initial_camera(cams, ""), 0)
self.assertEqual(cam_mod.select_initial_camera(cams, "front"), 1)
self.assertEqual(cam_mod.select_initial_camera(cams, "back"), 0)
self.assertEqual(cam_mod.select_initial_camera(cams, "imx376"), 2)
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_convert_abgr8888_with_stride_and_rotations(self):
# 2x3 image with stride padding (row = 2*4 + 4 padding bytes = 12)
h, w, stride = 3, 2, 12
buf = bytearray(h * stride)
# Pixel (x=0, y=0): ABGR8888 in LE memory is [R, G, B, A] = [10, 20, 200, 255] -> BGR [200, 20, 10]
buf[0:4] = bytes([10, 20, 200, 255])
# Pixel (x=1, y=0): R=40, G=50, B=60 -> BGR [60, 50, 40]
buf[4:8] = bytes([40, 50, 60, 255])
bgr0 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=0, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr0.shape, (3, 2, 3))
self.assertEqual(tuple(bgr0[0, 0]), (200, 20, 10))
self.assertEqual(tuple(bgr0[0, 1]), (60, 50, 40))
bgr90 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=90, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr90.shape, (2, 3, 3))
# Clockwise 90 deg puts original (row=0, col=0) at (row=0, col=h-1=2)
self.assertEqual(tuple(bgr90[0, 2]), (200, 20, 10))
bgr270 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=270, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr270.shape, (2, 3, 3))
# Counter-clockwise 90 deg puts original (row=0, col=0) at (row=w-1=1, col=0)
self.assertEqual(tuple(bgr270[1, 0]), (200, 20, 10))
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_convert_argb_rgb_and_cairo_bgra(self):
# ARGB8888 in LE memory is [B, G, R, A]
buf = bytes([210, 110, 30, 255, 10, 20, 30, 255])
bgr = cam_mod.convert_frame(buf, 2, 1, 8, "ARGB8888", rot=0, cv2_mod=cv2, np_mod=np)
self.assertEqual(tuple(bgr[0, 0]), (210, 110, 30))
rgb_buf = bytes([11, 22, 33, 44, 55, 66])
bgr2 = cam_mod.convert_frame(rgb_buf, 2, 1, 6, "RGB888", rot=180, cv2_mod=cv2, np_mod=np)
# Inverted horizontally+vertically and channel-flipped -> [66, 55, 44] then [33, 22, 11]
self.assertEqual(tuple(bgr2[0, 0]), (66, 55, 44))
self.assertEqual(tuple(bgr2[0, 1]), (33, 22, 11))
argb32 = cam_mod.bgr_to_cairo_argb32(bgr, np_mod=np)
self.assertEqual(argb32.shape, (1, 2, 4))
self.assertEqual(tuple(argb32[0, 0]), (210, 110, 30, 255))
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_ensure_even_frame(self):
odd = np.zeros((101, 203, 3), dtype=np.uint8)
even = cam_mod.ensure_even_frame(odd)
self.assertEqual(even.shape, (100, 202, 3))
if __name__ == "__main__":
unittest.main()