SpireCV/video_io/driver/sv_camera_mipi.cpp

96 lines
1.9 KiB
C++

/*
* @Description:
* @Author: L LC @amov
* @Date: 2023-12-19 18:30:17
* @LastEditors: L LC @amov
* @LastEditTime: 2023-12-21 17:57:12
* @FilePath: /SpireCV/video_io/driver/sv_camera_mipi.cpp
*/
#include "sv_camera_privately.h"
#include <fstream>
class sv_camera_MIPI : public sv_p::CameraBase
{
private:
int _index = -2;
std::string _name = "\0";
public:
bool setName(const std::string &name);
bool setIndex(int index);
sv::CameraType getType(void) { return sv::CameraType::MIPI; }
std::string getName(void) { return _name; }
bool open(void);
sv_camera_MIPI(int timeOut);
~sv_camera_MIPI();
static CameraBase *creat(int timeOut)
{
return new sv_camera_MIPI(timeOut);
}
};
sv_camera_MIPI::sv_camera_MIPI(int timeOut) : sv_p::CameraBase(timeOut)
{
}
bool sv_camera_MIPI::setName(const std::string &name)
{
bool ret = false;
if (!this->cap.isOpened())
{
this->_name = name;
ret = true;
}
return ret;
}
bool sv_camera_MIPI::setIndex(int index)
{
bool ret = false;
if (!this->cap.isOpened())
{
this->_index = index;
ret = true;
}
return ret;
}
// 普通的mipi相机 采用 v4l2 框架打开
bool sv_camera_MIPI::open(void)
{
bool ret = false;
std::ostringstream pipeline;
pipeline << "v4l2src ";
if (this->_name != "\0")
{
pipeline << "device=" << this->_name << " !";
}
else if (this->_index != -2)
{
pipeline << "device-fd=" << this->_index << " !";
}
pipeline << " video/x-raw,format=(string)NV12,"
<< "width=(int)" << this->getW()
<< "height=(int)" << this->getH()
<< "framerate=" << this->getExpectFps() << "/1 !"
<< " videoconvert ! video/x-raw, format=(string)BGR ! appsink";
if (this->cap.open(pipeline.str(), cv::CAP_GSTREAMER))
{
// 开启读取线程
std::thread readLoop(&CameraBase::readThread, this);
this->readThreadHandle = readLoop.native_handle();
readLoop.detach();
ret = true;
}
return ret;
}