This repository contains the camera.h wrapper class for accessing USB Video Class (UVC) compliant cameras within the Nodepp Project environment. It utilizes the libuvc library to provide a clean, asynchronous C++ interface for discovery, control, and frame streaming.
#libuvc-dev
🪟: pacman -S mingw-w64-x86_64-libuvc
🐧: sudo apt install libuvc-devinclude(FetchContent)
FetchContent_Declare(
nodepp
GIT_REPOSITORY https://github.com/NodeppOfficial/nodepp
GIT_TAG origin/main
GIT_PROGRESS ON
)
FetchContent_MakeAvailable(nodepp)
FetchContent_Declare(
nodepp-camera
GIT_REPOSITORY https://github.com/NodeppOfficial/nodepp-camera
GIT_TAG origin/main
GIT_PROGRESS ON
)
FetchContent_MakeAvailable(nodepp-camera)
#[...]
target_link_libraries( #[...]
PUBLIC nodepp nodepp-camera #[...]
)- RAII Compliant: Safe resource management using constructors, destructors, and explicit
close()/free()methods ensures libuvc resources are properly released. - Asynchronous Streaming: Integrates with the Nodepp event loop and coroutines for non-blocking video frame retrieval.
- Device Discovery: The
camera::scan()function allows for simple enumeration of all connected UVC devices. - Format Mapping: Easy setup using Nodepp frame format enums
(e.g., FORMAT_YUYV, FORMAT_MJPEG)which are mapped internally to libuvc. - Detailed Metadata: Provides methods to retrieve Vendor ID, Product ID, Manufacturer, and Serial Number.
Scanning and Initialization
Use the camera::scan() function to discover devices. This returns a smart pointer array (ptr_t<camera_t>).
#include <nodepp/nodepp.h>
#include <camera/camera.h>
using namespace nodepp;
void onMain() {
// Scan for all available UVC devices
ptr_t<camera_t> devices = camera::scan();
if( devices.empty() || devices[0].is_closed() ) {
console::error("No camera found.");
return;
}
// Log device information
console::log("Found camera:", devices[0].get_device_product());
// ... rest of the code
}Starting and Processing Frames Use a Nodepp coroutine to continuously check for and process new frames from the camera in a non-blocking manner.
// Continuing from the onMain() example above...
// Start streaming at 640x480 resolution, 30 FPS, using UYVY format.
devices[0].start_recording( FORMAT_UYVY, 640, 480, 30 );
process::add( coroutine::add( COROUTINE(){
coBegin
while( devices[0].is_available() ){
// next() executes the lambda only if a fresh frame is available
devices[0].next([=]( camera_frame_t frame ){
console::log("Frame received:", frame.width, "x", frame.height);
});
coNext; } // Yield control to the event loop
// Cleanup when streaming stops
devices[0].stop_recording();
devices[0].close();
coFinish
}));Frame Data Structure Frames are delivered via the following structure:
struct camera_frame_t {
uint count=0; // Frame sequence number (reset on frame read)
int height;
int width;
int type; // camera_frame_format enum value
void* data; // Raw buffer pointer
ulong size; // Size of the raw buffer in bytes
};Available Frame Formats
FORMAT_YUYV, FORMAT_UYVY,
FORMAT_RGB, FORMAT_BGR,
FORMAT_MJPEG, FORMAT_H264,
FORMAT_GRAY8, FORMAT_GRAY16,
FORMAT_BY8, FORMAT_BA81,
FORMAT_SGRBG8, FORMAT_SGBRG8,
FORMAT_SRGGB8, FORMAT_SBGGR8,
FORMAT_NV12, FORMAT_P010,
# Compilation
g++ -o main main.cpp -I./include -luvc -lpthread -lusb-1.0414019691-de205b84-2156-4bef-aa79-989991fc62e9.webm Click here
Nodepp-Camera is distributed under the MIT License. See the LICENSE file for more details.