SpireCV/video_io/sv_camera_def.cpp

136 lines
2.7 KiB
C++

#include "sv_camera_privately.h"
sv_p::CameraBase::CameraBase(int timeOut)
{
this->_timeOut = timeOut;
}
void sv_p::CameraBase::readThread(void)
{
int count = 0;
while (this->cap.isOpened())
{
if (this->cap.grab())
{
std::lock_guard<std::mutex> locker(this->frameMutex);
this->cap.retrieve(this->imageBuff);
this->frameEmpty.notify_all();
count = 0;
isGot = true;
}
else
{
count++;
if (count > this->_timeOut)
{
this->cap.release();
// 抛出异常 并返回
throw std::runtime_error("SpireCV (101) Camera has offline, check CAMERA!");
return;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
#define PARAM_SET_CHECK(param, value) param = (value > 0 ? value : param)
void sv_p::CameraBase::setWH(int width, int height)
{
if (!this->cap.isOpened())
{
PARAM_SET_CHECK(this->_width, width);
PARAM_SET_CHECK(this->_height, height);
}
}
void sv_p::CameraBase::setFps(int fps)
{
PARAM_SET_CHECK(this->_fps, fps);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_FPS, fps);
}
}
void sv_p::CameraBase::setBrightness(double brightness)
{
PARAM_SET_CHECK(this->_brightness, brightness);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_BRIGHTNESS, brightness);
}
}
void sv_p::CameraBase::setContrast(double contrast)
{
PARAM_SET_CHECK(this->_contrast, contrast);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_CONTRAST, contrast);
}
}
void sv_p::CameraBase::setSaturation(double saturation)
{
PARAM_SET_CHECK(this->_saturation, saturation);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_SATURATION, saturation);
}
}
void sv_p::CameraBase::setHue(double hue)
{
PARAM_SET_CHECK(this->_hue, hue);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_HUE, hue);
}
}
void sv_p::CameraBase::setExposure(double exposure)
{
PARAM_SET_CHECK(this->_exposure, exposure);
if (this->cap.isOpened())
{
this->cap.set(cv::CAP_PROP_EXPOSURE, exposure);
}
}
bool sv_p::CameraBase::open(void)
{
}
bool sv_p::CameraBase::read(cv::Mat &image)
{
bool ret = false;
std::lock_guard<std::mutex> locker(this->frameMutex);
if (this->frameEmpty.wait_for(this->frameMutex, std::chrono::milliseconds(this->_timeOut)) == std::cv_status::no_timeout)
{
// 获取图像
this->imageBuff.copyTo(image);
ret = true;
}
return ret;
}
bool sv_p::CameraBase::readNoBlock(cv::Mat &image)
{
bool ret = false;
std::lock_guard<std::mutex> locker(this->frameMutex);
if (this->isGot)
{
this->imageBuff.copyTo(image);
isGot = false;
}
return ret;
}
void sv_p::CameraBase::release(void)
{
if (this->cap.isOpened())
{
this->cap.release();
}
}