131 lines
3.9 KiB
C++
131 lines
3.9 KiB
C++
#include "camera.h"
|
|
|
|
Camera::Camera(QObject *parent)
|
|
: QObject{parent}
|
|
{
|
|
state = false;
|
|
PylonInitialize();
|
|
cam = new CBaslerUniversalInstantCamera ( CTlFactory::GetInstance().CreateFirstDevice());
|
|
qDebug() << "Using device " << cam->GetDeviceInfo().GetModelName() ;
|
|
|
|
|
|
}
|
|
|
|
Camera::~Camera()
|
|
{
|
|
state = false;
|
|
task->join();
|
|
PylonTerminate();
|
|
}
|
|
|
|
void Camera::open()
|
|
{
|
|
state = true;
|
|
try {
|
|
// 确保相机未在采集
|
|
if(cam->IsGrabbing()){
|
|
cam->StopGrabbing();
|
|
}
|
|
|
|
// 重新打开相机确保干净状态
|
|
if(cam->IsOpen()) {
|
|
cam->Close();
|
|
}
|
|
cam->Open();
|
|
|
|
// 等待相机初始化
|
|
Sleep(50);
|
|
|
|
INodeMap& nodemap = cam->GetNodeMap();
|
|
|
|
// 使用安全的方式设置参数
|
|
auto setIntegerParameter = [&nodemap](const char* name, int64_t value) {
|
|
try {
|
|
CIntegerParameter param(nodemap, name);
|
|
if (param.IsWritable()) {
|
|
// 检查值范围
|
|
int64_t min = param.GetMin();
|
|
int64_t max = param.GetMax();
|
|
int64_t inc = param.GetInc();
|
|
|
|
// 调整值到有效范围内
|
|
int64_t adjustedValue = max(min, min(value, max));
|
|
// 对齐到步长
|
|
if (inc > 1) {
|
|
adjustedValue = (adjustedValue / inc) * inc;
|
|
}
|
|
|
|
param.SetValue(adjustedValue);
|
|
qDebug() << name << "set to:" << adjustedValue;
|
|
return true;
|
|
} else {
|
|
qDebug() << name << "is not writable";
|
|
return false;
|
|
}
|
|
} catch (const GenericException& e) {
|
|
qDebug() << "Failed to set" << name << ":" << e.GetDescription();
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// 设置参数
|
|
setIntegerParameter("OffsetX", 1736);
|
|
setIntegerParameter("OffsetY", 1264);
|
|
setIntegerParameter("Width", 640);
|
|
setIntegerParameter("Height", 480);
|
|
|
|
// 设置自动曝光
|
|
if (cam->ExposureAuto.IsWritable()) {
|
|
cam->ExposureAuto.SetValue(Basler_UniversalCameraParams::ExposureAuto_Continuous);
|
|
}
|
|
} catch (const GenericException &e) {
|
|
qDebug() << "Exception in setCameraParameters:" << e.GetDescription();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
task = new std::thread(&Camera::getSinglePic,this);
|
|
}
|
|
|
|
void Camera::close(){
|
|
state = false;
|
|
task->join();
|
|
PylonTerminate();
|
|
}
|
|
void Camera::getSinglePic()
|
|
{
|
|
float sec = 1/FRAME;
|
|
int timeout = GRABTIMEOUT;
|
|
while(state){
|
|
CGrabResultPtr ptrGrabResult;
|
|
|
|
// auto now = std::chrono::system_clock::now();
|
|
|
|
try {
|
|
cam->GrabOne(timeout,ptrGrabResult,TimeoutHandling_ThrowException);
|
|
} catch (const GenericException &e) {
|
|
qDebug() << "Exception in setCameraParameters:" << e.GetDescription();
|
|
}
|
|
|
|
// auto now_ = std::chrono::system_clock::now();
|
|
// auto duration = now_ - now;
|
|
// auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
// qDebug()<<ms ;
|
|
// qDebug()<<ptrGrabResult->GetWidth();
|
|
// qDebug()<<ptrGrabResult->GetHeight();
|
|
if(ptrGrabResult->GrabSucceeded()){
|
|
uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
|
|
QImage img(pImageBuffer,ptrGrabResult->GetWidth(),ptrGrabResult->GetHeight(),ptrGrabResult->GetWidth(),QImage::Format_Grayscale8);
|
|
emit sendPicData(img.copy());
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds((int)(sec*1000)));
|
|
}
|
|
|
|
// qDebug()<<ptrGrabResult->GetW
|
|
|
|
}
|
|
|