-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path2-capture_grab_c.c
More file actions
172 lines (143 loc) · 5.46 KB
/
Copy path2-capture_grab_c.c
File metadata and controls
172 lines (143 loc) · 5.46 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file 2-capture_grab_c.c
* @brief Example demonstrating frame capture with grab method using ccap C interface
* @author wysaid (this@wysaid.org)
* @date 2025-05
*/
#include "ccap_c.h"
#include "ccap_utils_c.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
int selectCamera(CcapProvider* provider) {
char** deviceNames;
size_t deviceCount;
if (ccap_provider_find_device_names(provider, &deviceNames, &deviceCount) && deviceCount > 1) {
printf("Multiple devices found, please select one:\n");
for (size_t i = 0; i < deviceCount; i++) {
printf(" %zu: %s\n", i, deviceNames[i]);
}
int selectedIndex;
printf("Enter the index of the device you want to use: ");
if (scanf("%d", &selectedIndex) != 1) {
selectedIndex = 0;
}
if (selectedIndex < 0 || selectedIndex >= (int)deviceCount) {
selectedIndex = 0;
fprintf(stderr, "Invalid index, using the first device: %s\n", deviceNames[0]);
} else {
printf("Using device: %s\n", deviceNames[selectedIndex]);
}
ccap_provider_free_device_names(deviceNames, deviceCount);
return selectedIndex;
}
if (deviceCount > 0) {
ccap_provider_free_device_names(deviceNames, deviceCount);
}
return -1; // One or no device, use default.
}
void createDirectory(const char* path) {
struct stat st = {0};
if (stat(path, &st) == -1) {
mkdir(path, 0700);
}
}
int main(int argc, char** argv) {
printf("ccap C Interface Capture Grab Example\n");
printf("Version: %s\n\n", ccap_get_version());
// Enable verbose log to see debug information
ccap_set_log_level(CCAP_LOG_LEVEL_VERBOSE);
// Get current working directory and create capture directory
char cwd[1024];
if (argc > 0 && argv[0][0] != '.') {
strncpy(cwd, argv[0], sizeof(cwd) - 1);
cwd[sizeof(cwd) - 1] = '\0';
// Find last slash
char* lastSlash = strrchr(cwd, '/');
if (!lastSlash) {
lastSlash = strrchr(cwd, '\\');
}
if (lastSlash) {
*lastSlash = '\0';
}
} else {
if (!getcwd(cwd, sizeof(cwd))) {
strcpy(cwd, ".");
}
}
char captureDir[1024];
snprintf(captureDir, sizeof(captureDir), "%s/image_capture", cwd);
createDirectory(captureDir);
// Create provider
CcapProvider* provider = ccap_provider_create();
if (!provider) {
printf("Failed to create provider\n");
return -1;
}
// Set camera properties
int requestedWidth = 1920;
int requestedHeight = 1080;
double requestedFps = 60.0;
ccap_provider_set_property(provider, CCAP_PROPERTY_WIDTH, requestedWidth);
ccap_provider_set_property(provider, CCAP_PROPERTY_HEIGHT, requestedHeight);
ccap_provider_set_property(provider, CCAP_PROPERTY_PIXEL_FORMAT_OUTPUT, CCAP_PIXEL_FORMAT_RGB24);
ccap_provider_set_property(provider, CCAP_PROPERTY_FRAME_RATE, requestedFps);
// Select and open camera
int deviceIndex;
if (argc > 1 && isdigit(argv[1][0])) {
deviceIndex = atoi(argv[1]);
} else {
deviceIndex = selectCamera(provider);
}
if (!ccap_provider_open_by_index(provider, deviceIndex, true)) {
printf("Failed to open camera\n");
ccap_provider_destroy(provider);
return -1;
}
if (!ccap_provider_is_started(provider)) {
fprintf(stderr, "Failed to start camera!\n");
ccap_provider_destroy(provider);
return -1;
}
// Get actual camera properties
int realWidth = (int)ccap_provider_get_property(provider, CCAP_PROPERTY_WIDTH);
int realHeight = (int)ccap_provider_get_property(provider, CCAP_PROPERTY_HEIGHT);
double realFps = ccap_provider_get_property(provider, CCAP_PROPERTY_FRAME_RATE);
printf("Camera started successfully, requested resolution: %dx%d, real resolution: %dx%d, requested fps %.1f, real fps: %.1f\n",
requestedWidth, requestedHeight, realWidth, realHeight, requestedFps, realFps);
// Capture frames with 3000ms timeout
int frameCount = 0;
while (frameCount < 10) {
CcapVideoFrame* frame = ccap_provider_grab(provider, 3000);
if (frame) {
CcapVideoFrameInfo frameInfo;
if (ccap_video_frame_get_info(frame, &frameInfo)) {
printf("VideoFrame %llu grabbed: width = %d, height = %d, bytes: %d\n",
frameInfo.frameIndex, frameInfo.width, frameInfo.height, frameInfo.sizeInBytes);
// Save frame to directory
char outputPath[2048];
int result = ccap_dump_frame_to_directory(frame, captureDir, outputPath, sizeof(outputPath));
if (result > 0) {
printf("VideoFrame saved to: %s\n", outputPath);
} else {
fprintf(stderr, "Failed to save frame!\n");
}
}
ccap_video_frame_release(frame);
frameCount++;
if (frameCount >= 10) {
printf("Captured 10 frames, stopping...\n");
break;
}
} else {
fprintf(stderr, "Failed to grab frame or timeout!\n");
break;
}
}
// Cleanup
ccap_provider_destroy(provider);
return 0;
}