-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path2-capture_grab_c.c
More file actions
139 lines (115 loc) · 4.65 KB
/
2-capture_grab_c.c
File metadata and controls
139 lines (115 loc) · 4.65 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
/**
* @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 "utils/helper.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Error callback function
void error_callback(CcapErrorCode errorCode, const char* errorDescription, void* userData) {
(void)userData; // Unused parameter
printf("Camera Error - Code: %d, Description: %s\n", (int)errorCode, errorDescription);
}
int main(int argc, char** argv) {
printf("ccap C Interface Capture Grab Example\n");
printf("Version: %s\n\n", ccap_get_version());
ExampleCommandLine commandLine = { 0 };
initExampleCommandLine(&commandLine, argc, argv);
applyExampleCameraBackend(&commandLine);
// Enable verbose log to see debug information
ccap_set_log_level(CCAP_LOG_LEVEL_VERBOSE);
// Set error callback to receive error notifications
ccap_set_error_callback(error_callback, NULL);
// Get current working directory and create capture directory
char cwd[1024];
if (commandLine.argc > 0 && commandLine.argv[0][0] != '.') {
strncpy(cwd, commandLine.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 (getCurrentWorkingDirectory(cwd, sizeof(cwd)) != 0) {
strncpy(cwd, ".", sizeof(cwd) - 1);
cwd[sizeof(cwd) - 1] = '\0';
}
}
char captureDir[2048];
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 = selectCamera(provider, &commandLine);
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 %d grabbed: width = %d, height = %d, bytes: %d\n",
(int)frameInfo.frameIndex, frameInfo.width, frameInfo.height, (int)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;
}