-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path0-print_camera_c.c
More file actions
134 lines (111 loc) · 4.31 KB
/
Copy path0-print_camera_c.c
File metadata and controls
134 lines (111 loc) · 4.31 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
/**
* @file ccap_c_example.c
* @brief Example demonstrating the use of ccap C interface
* @author wysaid (this@wysaid.org)
* @date 2025-05
*/
#include "ccap_c.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
// Callback function for new frames
bool frame_callback(const CcapVideoFrame* frame, void* userData) {
static int frameCount = 0;
frameCount++;
CcapVideoFrameInfo frameInfo;
if (ccap_video_frame_get_info(frame, &frameInfo)) {
printf("Frame %d: %dx%d, format=%d, timestamp=%" PRIu64 "\n",
frameCount, frameInfo.width, frameInfo.height,
frameInfo.pixelFormat, frameInfo.timestamp);
}
// Return false to keep the frame available for grab()
// Return true to consume the frame (won't be available for grab())
return false;
}
// 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() {
printf("ccap C Interface Example\n");
printf("Version: %s\n\n", ccap_get_version());
// Set error callback to receive error notifications
ccap_set_error_callback(error_callback, NULL);
// Create provider
CcapProvider* provider = ccap_provider_create();
if (!provider) {
printf("Failed to create provider\n");
return -1;
}
// Find available devices
CcapDeviceNamesList deviceList;
if (ccap_provider_find_device_names_list(provider, &deviceList)) {
printf("Found %zu camera device(s):\n", deviceList.deviceCount);
for (size_t i = 0; i < deviceList.deviceCount; i++) {
printf(" %zu: %s\n", i, deviceList.deviceNames[i]);
}
printf("\n");
} else {
printf("Failed to enumerate devices\n");
}
// Open default camera
if (!ccap_provider_open(provider, NULL, false)) {
printf("Failed to open camera\n");
ccap_provider_destroy(provider);
return -1;
}
printf("Camera opened successfully\n");
// Get device info
CcapDeviceInfo deviceInfo;
if (ccap_provider_get_device_info(provider, &deviceInfo)) {
printf("Device: %s\n", deviceInfo.deviceName[0] ? deviceInfo.deviceName : "Unknown");
printf("Supported pixel formats: %zu\n", deviceInfo.pixelFormatCount);
printf("Supported resolutions: %zu\n", deviceInfo.resolutionCount);
if (deviceInfo.resolutionCount > 0) {
printf("Supported resolutions:\n");
for (size_t i = 0; i < deviceInfo.resolutionCount; i++) {
printf(" %zu: %dx%d\n", i + 1,
deviceInfo.supportedResolutions[i].width,
deviceInfo.supportedResolutions[i].height);
}
}
}
// Set camera properties
ccap_provider_set_property(provider, CCAP_PROPERTY_WIDTH, 640);
ccap_provider_set_property(provider, CCAP_PROPERTY_HEIGHT, 480);
ccap_provider_set_property(provider, CCAP_PROPERTY_FRAME_RATE, 30.0);
// Set frame callback
ccap_provider_set_new_frame_callback(provider, frame_callback, NULL);
// Start capturing
if (!ccap_provider_start(provider)) {
printf("Failed to start camera\n");
ccap_provider_destroy(provider);
return -1;
}
printf("Camera started, capturing frames...\n");
// Capture frames for 5 seconds using both callback and grab methods
for (int i = 0; i < 10; i++) {
// Try to grab a frame (synchronous method)
CcapVideoFrame* frame = ccap_provider_grab(provider, 5000); // 1 second timeout
if (frame) {
CcapVideoFrameInfo frameInfo;
if (ccap_video_frame_get_info(frame, &frameInfo)) {
printf("Grabbed frame: %dx%d, size=%u bytes\n",
frameInfo.width, frameInfo.height, frameInfo.sizeInBytes);
}
// Release the frame
ccap_video_frame_release(frame);
} else {
printf("Failed to grab frame or timeout\n");
}
}
// Stop capturing
ccap_provider_stop(provider);
printf("Camera stopped\n");
// Close and cleanup
ccap_provider_close(provider);
ccap_provider_destroy(provider);
printf("Example completed successfully\n");
return 0;
}