-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatLibCam.cpp
More file actions
125 lines (102 loc) · 3.37 KB
/
Copy pathchatLibCam.cpp
File metadata and controls
125 lines (102 loc) · 3.37 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <vector>
#include <sys/mman.h>
#include <libcamera/libcamera.h>
using namespace libcamera;
static std::shared_ptr<Camera> camera;
int main() {
CameraManager cm;
if (cm.start()) {
std::cerr << "Failed to start CameraManager\n";
return EXIT_FAILURE;
}
const auto &cams = cm.cameras();
if (cams.empty()) {
std::cerr << "No cameras found\n";
cm.stop();
return EXIT_FAILURE;
}
std::string camId = cams[0]->id();
camera = cm.get(camId);
if (!camera) {
std::cerr << "Failed to get camera " << camId << "\n";
cm.stop();
return EXIT_FAILURE;
}
if (camera->acquire()) {
std::cerr << "Failed to acquire camera\n";
cm.stop();
return EXIT_FAILURE;
}
// Still capture configuration
std::unique_ptr<CameraConfiguration> config = camera->generateConfiguration({ StreamRole::StillCapture });
if (!config) {
std::cerr << "Failed to generate configuration\n";
camera->release();
cm.stop();
return EXIT_FAILURE;
}
StreamConfiguration &streamCfg = config->at(0);
if (config->validate() != CameraConfiguration::Status::Valid) {
std::cerr << "Configuration changed on validation\n";
}
camera->configure(config.get());
FrameBufferAllocator allocator(camera.get());
if (allocator.allocate(streamCfg.stream()) < 0) {
std::cerr << "Failed to allocate buffers\n";
camera->release();
cm.stop();
return EXIT_FAILURE;
}
const auto &buffers = allocator.buffers(streamCfg.stream());
if (buffers.empty()) {
std::cerr << "No buffers allocated\n";
camera->release();
cm.stop();
return EXIT_FAILURE;
}
auto request = camera->createRequest();
if (!request) {
std::cerr << "Failed to create request\n";
camera->release();
cm.stop();
return EXIT_FAILURE;
}
if (request->addBuffer(streamCfg.stream(), buffers[0].get()) < 0) {
std::cerr << "Failed to add buffer\n";
camera->release();
cm.stop();
return EXIT_FAILURE;
}
bool done = false;
camera->requestCompleted.connect([&](Request *r) {
if (r->status() != Request::RequestCancelled) {
FrameBuffer *buffer = r->buffers().begin()->second;
const FrameMetadata &meta = buffer->metadata();
void *mem = mmap(nullptr, buffer->planes()[0].length, PROT_READ, MAP_SHARED, buffer->planes()[0].fd.get(), buffer->planes()[0].offset);
if (mem == MAP_FAILED) {
std::cerr << "mmap failed\n";
done = true;
return;
}
std::ofstream file("capture.raw", std::ios::binary);
file.write(reinterpret_cast<const char *>(mem), meta.planes()[0].bytesused);
munmap(mem, buffer->planes()[0].length);
std::cout << "Saved capture.raw (" << meta.planes()[0].bytesused << " bytes)\n";
}
done = true;
});
camera->start();
camera->queueRequest(request.get());
// Wait for capture
while (!done)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
camera->stop();
allocator.free(streamCfg.stream());
camera->release();
cm.stop();
return 0;
}