33#include < sys/mman.h>
44
55#include " common/logging.h"
6+ #include < libcamera/geometry.h>
67
78std::shared_ptr<LibcameraCapturer> LibcameraCapturer::Create (Args args) {
89 auto ptr = std::make_shared<LibcameraCapturer>(args);
910 ptr->Init (args.cameraId );
10- ptr->SetFps (args.fps )
11- .SetRotation (args.rotation_angle )
12- .SetFormat (args.width , args.height )
13- .StartCapture ();
11+ ptr->SetFps (args.fps ).SetRotation (args.rotation ).SetResolution (args.width , args.height );
12+
13+ if (args.gain ) {
14+ ptr->SetControls (libcamera::controls::ANALOGUE_GAIN_MODE,
15+ libcamera::controls::AnalogueGainModeManual)
16+ .SetControls (libcamera::controls::ANALOGUE_GAIN, args.gain );
17+ }
18+
19+ ptr->SetControls (libcamera::controls::SHARPNESS, args.sharpness )
20+ .SetControls (libcamera::controls::CONTRAST, args.contrast )
21+ .SetControls (libcamera::controls::BRIGHTNESS, args.brightness )
22+ .SetControls (libcamera::controls::SATURATION, args.saturation )
23+ .SetControls (libcamera::controls::EXPOSURE_VALUE, args.ev )
24+ .SetControls (libcamera::controls::EXPOSURE_TIME,
25+ args.shutter .get <std::chrono::microseconds>())
26+ .SetControls (libcamera::controls::AE_METERING_MODE, args.ae_metering_mode )
27+ .SetControls (libcamera::controls::AE_EXPOSURE_MODE, args.ae_mode )
28+ .SetControls (libcamera::controls::AWB_MODE, args.awb_mode )
29+ .SetControls (libcamera::controls::COLOUR_GAINS,
30+ libcamera::Span<const float , 2 >({args.awb_gain_r , args.awb_gain_b }))
31+ .SetControls (libcamera::controls::draft::NOISE_REDUCTION_MODE, args.denoise_mode );
32+
33+ if (args.af_mode == -1 ) {
34+ if (args.lens_position || args.set_default_lens_position ) {
35+ args.af_mode = libcamera::controls::AfModeManual;
36+ } else {
37+ args.af_mode =
38+ ptr->camera_ ->controls ().at (&libcamera::controls::AfMode).max ().get <int >();
39+ }
40+ }
41+ ptr->SetControls (libcamera::controls::AF_MODE, args.af_mode )
42+ .SetControls (libcamera::controls::AF_RANGE, args.af_range_mode )
43+ .SetControls (libcamera::controls::AF_SPEED, args.af_speed_mode );
44+
45+ if (args.af_window_width != 0 && args.af_window_height != 0 ) {
46+ libcamera::Rectangle sensor_area = ptr->camera_ ->controls ()
47+ .at (&libcamera::controls::ScalerCrop)
48+ .max ()
49+ .get <libcamera::Rectangle>();
50+ int x = args.af_window_x * sensor_area.width ;
51+ int y = args.af_window_y * sensor_area.height ;
52+ int w = args.af_window_width * sensor_area.width ;
53+ int h = args.af_window_height * sensor_area.height ;
54+ libcamera::Rectangle afwindows_rectangle[1 ];
55+ afwindows_rectangle[0 ] = libcamera::Rectangle (x, y, w, h);
56+ afwindows_rectangle[0 ].translateBy (sensor_area.topLeft ());
57+
58+ ptr->SetControls (libcamera::controls::AF_METERING, libcamera::controls::AfMeteringWindows)
59+ .SetControls (libcamera::controls::AF_WINDOWS,
60+ libcamera::Span<const libcamera::Rectangle>(afwindows_rectangle));
61+ }
62+
63+ if (args.af_mode == libcamera::controls::AfModeEnum::AfModeAuto) {
64+ ptr->SetControls (libcamera::controls::AF_TRIGGER, libcamera::controls::AfTriggerStart);
65+ } else if (args.lens_position || args.set_default_lens_position ) {
66+ float f;
67+ if (args.lens_position ) {
68+ f = args.lens_position .value ();
69+ } else {
70+ f = ptr->camera_ ->controls ().at (&libcamera::controls::LensPosition).def ().get <float >();
71+ }
72+ ptr->SetControls (libcamera::controls::LENS_POSITION, f);
73+ }
74+
75+ ptr->StartCapture ();
1476 return ptr;
1577}
1678
@@ -19,7 +81,7 @@ LibcameraCapturer::LibcameraCapturer(Args args)
1981 format_(args.format),
2082 config_(args) {}
2183
22- void LibcameraCapturer::Init (int deviceId ) {
84+ void LibcameraCapturer::Init (int cameraId ) {
2385 cm_ = std::make_unique<libcamera::CameraManager>();
2486 int ret = cm_->start ();
2587 if (ret) {
@@ -31,11 +93,11 @@ void LibcameraCapturer::Init(int deviceId) {
3193 throw std::runtime_error (" No camera is available via libcamera." );
3294 }
3395
34- if (config_. cameraId >= cameras.size ()) {
96+ if (cameraId >= cameras.size ()) {
3597 throw std::runtime_error (" Selected camera is not available." );
3698 }
3799
38- std::string const &cam_id = cameras[config_. cameraId ]->id ();
100+ std::string const &cam_id = cameras[cameraId]->id ();
39101 INFO_PRINT (" camera id: %s" , cam_id.c_str ());
40102 camera_ = cm_->get (cam_id);
41103 camera_->acquire ();
@@ -64,7 +126,7 @@ uint32_t LibcameraCapturer::format() const { return format_; }
64126
65127Args LibcameraCapturer::config () const { return config_; }
66128
67- LibcameraCapturer &LibcameraCapturer::SetFormat (int width, int height) {
129+ LibcameraCapturer &LibcameraCapturer::SetResolution (int width, int height) {
68130 DEBUG_PRINT (" camera original format: %s" , camera_config_->at (0 ).toString ().c_str ());
69131
70132 if (width && height) {
@@ -109,10 +171,17 @@ LibcameraCapturer &LibcameraCapturer::SetFps(int fps) {
109171 return *this ;
110172}
111173
112- LibcameraCapturer &LibcameraCapturer::SetControls (const int key, const int value) {
174+ LibcameraCapturer &LibcameraCapturer::SetControls (int key, ControlValue value) {
113175 std::lock_guard<std::mutex> lock (control_mutex_);
114- DEBUG_PRINT (" Set camera controls, key: %d, value: %d" , key, value);
115- controls_.set (key, value);
176+
177+ if (controls_.contains (key) && camera_->controls ().count (key) > 0 ) {
178+ std::visit (
179+ [&](auto &&v) {
180+ controls_.set (key, v);
181+ },
182+ value);
183+ }
184+
116185 return *this ;
117186}
118187
0 commit comments