-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
89 lines (81 loc) · 2.97 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "LibCamera.h"
using namespace cv;
int main() {
time_t start_time = time(0);
int frame_count = 0;
float lens_position = 100;
float focus_step = 50;
LibCamera cam;
uint32_t width = 1920;
uint32_t height = 1080;
uint32_t stride;
char key;
int window_width = 1920;
int window_height = 1080;
if (width > window_width)
{
cv::namedWindow("libcamera-demo", cv::WINDOW_NORMAL);
cv::resizeWindow("libcamera-demo", window_width, window_height);
}
int ret = cam.initCamera();
cam.configureStill(width, height, formats::RGB888, 1, 0);
ControlList controls_;
int64_t frame_time = 1000000 / 10;
// Set frame rate
controls_.set(controls::FrameDurationLimits, libcamera::Span<const int64_t, 2>({ frame_time, frame_time }));
// Adjust the brightness of the output images, in the range -1.0 to 1.0
controls_.set(controls::Brightness, 0.5);
// Adjust the contrast of the output image, where 1.0 = normal contrast
controls_.set(controls::Contrast, 1.5);
// Set the exposure time
controls_.set(controls::ExposureTime, 20000);
cam.set(controls_);
if (!ret) {
bool flag;
LibcameraOutData frameData;
cam.startCamera();
cam.VideoStream(&width, &height, &stride);
while (true) {
flag = cam.readFrame(&frameData);
if (!flag)
continue;
Mat im(height, width, CV_8UC3, frameData.imageData, stride);
imshow("libcamera-demo", im);
key = waitKey(1);
if (key == 'q') {
break;
} else if (key == 'f') {
ControlList controls;
controls.set(controls::AfMode, controls::AfModeAuto);
controls.set(controls::AfTrigger, 0);
cam.set(controls);
} else if (key == 'a' || key == 'A') {
lens_position += focus_step;
} else if (key == 'd' || key == 'D') {
lens_position -= focus_step;
}
// To use the manual focus function, libcamera-dev needs to be updated to version 0.0.10 and above.
if (key == 'a' || key == 'A' || key == 'd' || key == 'D') {
ControlList controls;
controls.set(controls::AfMode, controls::AfModeManual);
controls.set(controls::LensPosition, lens_position);
cam.set(controls);
}
frame_count++;
if ((time(0) - start_time) >= 1){
printf("fps: %d\n", frame_count);
frame_count = 0;
start_time = time(0);
}
cam.returnFrameBuffer(frameData);
}
destroyAllWindows();
cam.stopCamera();
}
cam.closeCamera();
return 0;
}