[Feature][添加测试程序]
This commit is contained in:
parent
f2146d30df
commit
cd1270f500
29
CMakeLists.txt
Normal file
29
CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(universal_camera_sdk)
|
||||||
|
|
||||||
|
# 设置 C++ 标准
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(THIRDPARTY "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty")
|
||||||
|
|
||||||
|
# 查找 OpenCV 库
|
||||||
|
set(OpenCV_DIR ${THIRDPARTY}/opencv_build/x86_64/install/lib/cmake/opencv4) # 根据实际 OpenCV 安装路径修改
|
||||||
|
find_package(OpenCV REQUIRED)
|
||||||
|
|
||||||
|
# 包含 OpenCV 头文件
|
||||||
|
include_directories(${OpenCV_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# 添加可执行文件
|
||||||
|
add_executable(universal_camera_sdk source/main.cpp)
|
||||||
|
|
||||||
|
# 链接 OpenCV 静态库
|
||||||
|
target_link_libraries(universal_camera_sdk ${OpenCV_LIBS})
|
||||||
|
|
||||||
|
# 链接 FFmpeg 和 x264 静态库(OpenCV 已包含相关依赖,这里可能无需额外指定)
|
||||||
|
# 若需要可添加以下内容
|
||||||
|
target_link_libraries(universal_camera_sdk
|
||||||
|
${THIRDPARTY}/ffmpeg_build/x86_64/install/lib/libavcodec.a
|
||||||
|
${THIRDPARTY}/ffmpeg_build/x86_64/install/lib/libavformat.a
|
||||||
|
${THIRDPARTY}/ffmpeg_build/x86_64/install/lib/libavutil.a
|
||||||
|
${THIRDPARTY}/ffmpeg_build/x86_64/install/lib/libswscale.a
|
||||||
|
${THIRDPARTY}/x264_build/x86_64/install/lib/libx264.a
|
||||||
|
)
|
12
build.sh
Executable file
12
build.sh
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -d out ]; then
|
||||||
|
rm -rf out
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir out
|
||||||
|
cd out
|
||||||
|
cmake ..
|
||||||
|
make -j4
|
||||||
|
|
||||||
|
cp -a universal_camera_sdk /mnt/c/Users/LIKE/Desktop/
|
99
source/main.cpp
Normal file
99
source/main.cpp
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <thread>
|
||||||
|
#include <queue>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
// 全局变量
|
||||||
|
std::queue<cv::Mat> frameQueue;
|
||||||
|
std::mutex queueMutex;
|
||||||
|
std::condition_variable queueCondition;
|
||||||
|
std::atomic<bool> isRecording(false);
|
||||||
|
std::atomic<bool> stopRecording(false);
|
||||||
|
|
||||||
|
// 帧获取线程函数
|
||||||
|
void frameCaptureThread() {
|
||||||
|
cv::VideoCapture cap("/dev/video2");
|
||||||
|
if (!cap.isOpened()) {
|
||||||
|
std::cerr << "无法打开摄像头" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 设置分辨率和帧率
|
||||||
|
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
|
||||||
|
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
|
||||||
|
cap.set(cv::CAP_PROP_FPS, 25);
|
||||||
|
|
||||||
|
cv::Mat frame;
|
||||||
|
while (isRecording) {
|
||||||
|
if (cap.read(frame)) {
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
frameQueue.push(frame.clone());
|
||||||
|
lock.unlock();
|
||||||
|
queueCondition.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cap.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 帧保存线程函数
|
||||||
|
void frameSavingThread(const std::string& outputPath) {
|
||||||
|
cv::Size frameSize(1920, 1080);
|
||||||
|
int fps = 25;
|
||||||
|
cv::VideoWriter writer(outputPath, cv::VideoWriter::fourcc('H', '2', '6', '4'), fps, frameSize);
|
||||||
|
if (!writer.isOpened()) {
|
||||||
|
std::cerr << "无法打开视频写入器" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!stopRecording || !frameQueue.empty()) {
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
queueCondition.wait(lock, [] { return!frameQueue.empty() || stopRecording; });
|
||||||
|
|
||||||
|
while (!frameQueue.empty()) {
|
||||||
|
cv::Mat frame = frameQueue.front();
|
||||||
|
frameQueue.pop();
|
||||||
|
writer.write(frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始录像函数
|
||||||
|
void startRecording(const std::string& outputPath) {
|
||||||
|
isRecording = true;
|
||||||
|
stopRecording = false;
|
||||||
|
|
||||||
|
std::thread captureThread(frameCaptureThread);
|
||||||
|
std::thread savingThread(frameSavingThread, outputPath);
|
||||||
|
|
||||||
|
captureThread.detach();
|
||||||
|
savingThread.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止录像函数
|
||||||
|
void stopRecordingFunction() {
|
||||||
|
isRecording = false;
|
||||||
|
stopRecording = true;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
queueCondition.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// 定义输出视频的参数
|
||||||
|
std::string outputPath = "output.mp4";
|
||||||
|
|
||||||
|
// 开始录像
|
||||||
|
startRecording(outputPath);
|
||||||
|
|
||||||
|
// 模拟录制一段时间
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||||
|
|
||||||
|
// 停止录像
|
||||||
|
stopRecordingFunction();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user