990 lines
24 KiB
C++
990 lines
24 KiB
C++
#include "Hd_25DCameraVJ_module1.h"
|
|
#include <QDebug>
|
|
|
|
#include "KglDevice.h"
|
|
#include "KglStream.h"
|
|
#include "KglSystem.h"
|
|
#include "KglBuffer.h"
|
|
#include "KglDefine.h"
|
|
#include "KglResult.h"
|
|
using namespace Keyence;
|
|
#include <QDir>
|
|
|
|
KglDevice* kglDevice;
|
|
KglStream* kglStream;
|
|
KglGenParameterArray* kgllFeatureNodes;
|
|
KglBuffer* kglBuffer;
|
|
std::string sModelName;
|
|
// Camera type
|
|
#define CameraTypeArea 1
|
|
#define CameraTypeLine 2
|
|
#define CameraTypeXT 3
|
|
#define CameraTypeRB 4
|
|
int iCameraType;
|
|
|
|
QQueue<vector<cv::Mat>> queuePic;
|
|
QMutex* n_mutex = new QMutex();
|
|
std::vector<std::string> sImageType;
|
|
static int imgNum = 1;
|
|
|
|
#pragma region Device control
|
|
void Connect(int index)
|
|
{
|
|
KglSystem kglSystem;
|
|
const KglDeviceInfo* deviceInfo;
|
|
std::string inputkey;
|
|
if (KGL_SUCCESS != kglSystem.find()) {
|
|
qDebug() << "Failed to find camera device.";
|
|
return;
|
|
}
|
|
|
|
uint32_t count = kglSystem.getDeviceCount();
|
|
if (count == 0)
|
|
{
|
|
qDebug() << ("No camera device can be found.\n");
|
|
return;
|
|
}
|
|
/*else
|
|
{
|
|
qDebug() <<("Select the camera device number.\n\n");
|
|
for (uint32_t i = 0; i < count; ++i)
|
|
{
|
|
deviceInfo = kglSystem.getDeviceInfo(i);
|
|
sModelName = deviceInfo->getModelName();
|
|
std::string sMACAddress = (std::string)deviceInfo->getMACAddress();
|
|
qDebug() <<("%d : %s_KEYENCE CORPORATION_%s\n", i + 1, sMACAddress.c_str(), sModelName.c_str());
|
|
}
|
|
std::cin >> inputkey;
|
|
qDebug() <<("------------------------------\n");
|
|
}*/
|
|
|
|
//uint32_t selectNum = atoi(inputkey.c_str()) - 1;
|
|
if ((index < 0) || (count <= index))
|
|
{
|
|
qDebug() << ("The entry is invalid.\n");
|
|
return;
|
|
}
|
|
|
|
kglDevice = new KglDevice();
|
|
kglStream = new KglStream();
|
|
|
|
deviceInfo = kglSystem.getDeviceInfo(index);
|
|
sModelName = deviceInfo->getModelName();
|
|
if ((sModelName == ("CA-HL02MX")) ||
|
|
(sModelName == ("CA-HL04MX")) ||
|
|
(sModelName == ("CA-HL08MX")))
|
|
{
|
|
iCameraType = CameraTypeLine;
|
|
}
|
|
else if ((sModelName == ("XT-024")) ||
|
|
(sModelName == ("XT-060")))
|
|
{
|
|
iCameraType = CameraTypeXT;
|
|
}
|
|
else if ((sModelName == ("RB-500")) ||
|
|
(sModelName == ("RB-800")) ||
|
|
(sModelName == ("RB-1200")))
|
|
{
|
|
iCameraType = CameraTypeRB;
|
|
}
|
|
else {
|
|
iCameraType = CameraTypeArea;
|
|
}
|
|
|
|
if (KGL_SUCCESS != kglDevice->connect(deviceInfo->getIPAddress())) {
|
|
qDebug() << ("Cannot connect to the camera device.\n");
|
|
return;
|
|
}
|
|
|
|
if (KGL_SUCCESS != kglStream->open(*kglDevice)) {
|
|
qDebug() << ("Cannot connect to the stream.\n");
|
|
return;
|
|
}
|
|
|
|
// Get FeatureNode of camera device
|
|
kgllFeatureNodes = kglDevice->getDeviceParameters();
|
|
}
|
|
|
|
void Disconnect()
|
|
{
|
|
if ((kglStream != NULL) && (kglStream->isOpen()))
|
|
{
|
|
KglResult result = kglStream->close(*kglDevice);
|
|
if (KGL_SUCCESS != result) {
|
|
qDebug() << ("Cannot disconnect from the stream.\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ((kglDevice != NULL) && (kglDevice->isConnected()))
|
|
{
|
|
if (KGL_SUCCESS != kglDevice->disconnect()) {
|
|
qDebug() << ("Cannot disconnect from the camera device.\n");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region Feature access
|
|
void setIntegerValue(const std::string sFeatureName, const int64_t value)
|
|
{
|
|
KglResult result;
|
|
KglString sConfigurationLastFailureCause;
|
|
|
|
result = kgllFeatureNodes->setIntegerValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : setParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void setFloatValue(const std::string sFeatureName, const double value)
|
|
{
|
|
KglResult result;
|
|
KglString sConfigurationLastFailureCause;
|
|
|
|
result = kgllFeatureNodes->setFloatValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : setParam\n");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void setBooleanValue(const std::string sFeatureName, const bool value)
|
|
{
|
|
KglResult result;
|
|
KglString sConfigurationLastFailureCause;
|
|
|
|
result = kgllFeatureNodes->setBooleanValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : setParam\n");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void setEnumValue(const std::string sFeatureName, std::string value)
|
|
{
|
|
KglResult result;
|
|
KglString sConfigurationLastFailureCause;
|
|
|
|
result = kgllFeatureNodes->setEnumValue(sFeatureName.c_str(), value.c_str());
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : setParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void setStringValue(const std::string sFeatureName, const std::string value)
|
|
{
|
|
KglResult result;
|
|
KglString sConfigurationLastFailureCause;
|
|
|
|
result = kgllFeatureNodes->setStringValue(sFeatureName.c_str(), value.c_str());
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : setParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void executeCommand(const std::string sFeatureName)
|
|
{
|
|
KglResult result;
|
|
|
|
result = kgllFeatureNodes->executeCommand(sFeatureName.c_str());
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : execCommand\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void getIntegerValue(const std::string sFeatureName, int64_t& value)
|
|
{
|
|
KglResult result;
|
|
|
|
result = kgllFeatureNodes->getIntegerValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : getParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void getFloatValue(const std::string sFeatureName, double& value)
|
|
{
|
|
KglResult result;
|
|
|
|
result = kgllFeatureNodes->getFloatValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : getParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void getBooleanValue(const std::string sFeatureName, bool& value)
|
|
{
|
|
KglResult result;
|
|
|
|
result = kgllFeatureNodes->getBooleanValue(sFeatureName.c_str(), value);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : getParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void getEnumValue(const std::string sFeatureName, std::string& value)
|
|
{
|
|
KglResult result;
|
|
KglString featurevalue;
|
|
|
|
result = kgllFeatureNodes->getEnumValue(sFeatureName.c_str(), featurevalue);
|
|
value = featurevalue;
|
|
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : getParam\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void getStringValue(const std::string sFeatureName, std::string& value)
|
|
{
|
|
KglResult result;
|
|
KglString featurevalue;
|
|
|
|
result = kgllFeatureNodes->getStringValue(sFeatureName.c_str(), featurevalue);
|
|
value = featurevalue;
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : getParam\n");
|
|
return;
|
|
}
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region Import XML
|
|
void ImportDeviceParameters(std::string sFilePath)
|
|
{
|
|
setEnumValue("OperationMode", "SetupMode");
|
|
|
|
if (!sFilePath.empty())
|
|
{
|
|
KglTargetDataSet target = (KglTargetDataSet)(KglTargetData::CameraParameters)
|
|
| (KglTargetDataSet)(KglTargetData::ParameterSetOfCameraParameter)
|
|
| (KglTargetDataSet)(KglTargetData::ModelImageOfCameraParameter)
|
|
| (KglTargetDataSet)(KglTargetData::CameraParametersOfEnvironmentSettings)
|
|
| (KglTargetDataSet)(KglTargetData::CommonParametersOfEnvironmentSettings)
|
|
| (KglTargetDataSet)(KglTargetData::CommonCameraParameters)
|
|
| (KglTargetDataSet)(KglTargetData::CalibrationOfCameraParameter);
|
|
KglImportDeviceParamOption option = KglImportDeviceParamOption();
|
|
option.bOverwriteModelImage = true;
|
|
option.bOverwriteCalibration = true;
|
|
KglString failFeature;
|
|
|
|
KglResult ret = kglDevice->importDeviceParameters(sFilePath.c_str(), failFeature, target, option);
|
|
if (ret != KGL_SUCCESS)
|
|
{
|
|
qDebug() << ("Failed to load a Feature List file.\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
setEnumValue("OperationMode", "RunMode");
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region Optional Functions
|
|
std::vector<std::string> GetEnableImageType(std::string sImageType)
|
|
{
|
|
std::vector<std::string> EnableImageType;
|
|
|
|
std::string StdImageTypeTable[1][2] =
|
|
{
|
|
{ "StdNormalImageEnable", "StdNormalImage" }
|
|
};
|
|
|
|
std::string LtrxNmlImageTypeTable[15][2] =
|
|
{
|
|
{ "LtrxNormalImageEnable", "LtrxNormalImage" },
|
|
{ "LtrxUpperImageEnable", "LtrxUpperImage" },
|
|
{ "LtrxUpperRightImageEnable", "LtrxUpperRightImage" },
|
|
{ "LtrxRightImageEnable", "LtrxRightImage" },
|
|
{ "LtrxLowerRightImageEnable", "LtrxLowerRightImage" },
|
|
{ "LtrxLowerImageEnable", "LtrxLowerImage" },
|
|
{ "LtrxLowerLeftImageEnable", "LtrxLowerLeftImage" },
|
|
{ "LtrxLeftImageEnable", "LtrxLeftImage" },
|
|
{ "LtrxUpperLeftImageEnable", "LtrxUpperLeftImage" },
|
|
{ "LtrxShapeImage1Enable", "LtrxShapeImage1" },
|
|
{ "LtrxShapeImage2Enable", "LtrxShapeImage2" },
|
|
{ "LtrxShapeImage3Enable", "LtrxShapeImage3" },
|
|
{ "LtrxTextureImageEnable", "LtrxTextureImage" },
|
|
{ "LtrxGradientXImageEnable", "LtrxGradientXImage" },
|
|
{ "LtrxGradientYImageEnable", "LtrxGradientYImage" },
|
|
};
|
|
|
|
std::string MlspImageTypeTable[11][2] =
|
|
{
|
|
{ "MlspUVImageEnable", "MlspUVImage" },
|
|
{ "MlspBlueImageEnable", "MlspBlueImage" },
|
|
{ "MlspGreenImageEnable", "MlspGreenImage" },
|
|
{ "MlspAmberImageEnable", "MlspAmberImage" },
|
|
{ "MlspRedImageEnable", "MlspRedImage" },
|
|
{ "MlspFarRedImageEnable", "MlspFarRedImage" },
|
|
{ "MlspIRImageEnable", "MlspIRImage" },
|
|
{ "MlspWhiteImageEnable", "MlspWhiteImage" },
|
|
{ "MlspAverageGrayscaleImageEnable", "MlspAverageGrayscaleImage" },
|
|
{ "MlspColorImageEnable", "MlspColorImage" },
|
|
{ "MlspColorDifferenceImageEnable", "MlspColorDifferenceImage" }
|
|
};
|
|
|
|
std::string SprfImageTypeTable[16][2] =
|
|
{
|
|
{ "SprfX1ImageEnable", "SprfX1Image" },
|
|
{ "SprfX2ImageEnable", "SprfX2Image" },
|
|
{ "SprfX3ImageEnable", "SprfX3Image" },
|
|
{ "SprfX4ImageEnable", "SprfX4Image" },
|
|
{ "SprfY1ImageEnable", "SprfY1Image" },
|
|
{ "SprfY2ImageEnable", "SprfY2Image" },
|
|
{ "SprfY3ImageEnable", "SprfY3Image" },
|
|
{ "SprfY4ImageEnable", "SprfY4Image" },
|
|
{ "SprfSpecularReflectionImageEnable", "SprfSpecularReflectionImage" },
|
|
{ "SprfDiffuseReflectionImageEnable", "SprfDiffuseReflectionImage" },
|
|
{ "SprfShapeImage1Enable", "SprfShapeImage1" },
|
|
{ "SprfShapeImage2Enable", "SprfShapeImage2" },
|
|
{ "SprfPhaseXImageEnable", "SprfPhaseXImage" },
|
|
{ "SprfPhaseYImageEnable", "SprfPhaseYImage" },
|
|
{ "SprfGlossRatioImageEnable", "SprfGlossRatioImage" },
|
|
{ "SprfNormalImageEnable", "SprfNormalImage" }
|
|
};
|
|
|
|
std::string Cap3DImageTypeTable[2][2] =
|
|
{
|
|
{ "Cap3DAreascan3DImageEnable", "Areascan3DImage" },
|
|
{ "Cap3DAreascan2DImageEnable", "Areascan2DImage" },
|
|
};
|
|
|
|
std::string RBImageTypeTable[2][2] =
|
|
{
|
|
{ "Areascan3DImageEnable", "Areascan3DImage" },
|
|
{ "Areascan2DGrayscaleImageEnable", "Areascan2DGrayscaleImage" },
|
|
};
|
|
|
|
std::string XTImageTypeTable[3][2] =
|
|
{
|
|
{ "Areascan3DImageEnable", "Areascan3DImage" },
|
|
{ "Areascan2DColorImageEnable", "Areascan2DColorImage" },
|
|
{ "Areascan2DGrayscaleImageEnable", "Areascan2DGrayscaleImage" }
|
|
};
|
|
|
|
std::string FilteredImageTable[8][2] =
|
|
{
|
|
{ "FilteredImageEnable", "FilteredImage1" },
|
|
{ "FilteredImageEnable", "FilteredImage2" },
|
|
{ "FilteredImageEnable", "FilteredImage3" },
|
|
{ "FilteredImageEnable", "FilteredImage4" },
|
|
{ "FilteredImageEnable", "FilteredImage5" },
|
|
{ "FilteredImageEnable", "FilteredImage6" },
|
|
{ "FilteredImageEnable", "FilteredImage7" },
|
|
{ "FilteredImageEnable", "FilteredImage8" }
|
|
};
|
|
|
|
int64_t sTargetParameterSet;
|
|
getIntegerValue("TargetParameterSet", sTargetParameterSet);
|
|
setIntegerValue("ParameterSetSelector", sTargetParameterSet);
|
|
|
|
if (iCameraType == CameraTypeLine)
|
|
{
|
|
std::string sCaptureMode;
|
|
getEnumValue("CaptureMode", sCaptureMode);
|
|
if (sCaptureMode == "Fixed_MultipleImages")
|
|
{
|
|
bool sEnable;
|
|
getBooleanValue("IndividualFilterPerFixedCaptureNoEnable", sEnable);
|
|
if (sEnable)
|
|
{
|
|
int64_t sTargetCaptureCount;
|
|
getIntegerValue("TargetFixedCaptureNo", sTargetCaptureCount);
|
|
setIntegerValue("FixedCaptureNoSelector", sTargetCaptureCount);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool sEnable;
|
|
if (iCameraType == CameraTypeRB)
|
|
{
|
|
for (UINT i = 0; i < 2; i++)
|
|
{
|
|
std::string sFeatureName = RBImageTypeTable[i][0];
|
|
std::string sImageName = RBImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else if (iCameraType == CameraTypeXT)
|
|
{
|
|
for (UINT i = 0; i < 3; i++)
|
|
{
|
|
std::string sFeatureName = XTImageTypeTable[i][0];
|
|
std::string sImageName = XTImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::string sLightingMode = "";
|
|
getEnumValue("ImagingMode", sLightingMode);
|
|
|
|
if (sLightingMode == "StandardLighting")
|
|
{
|
|
for (UINT i = 0; i < 1; i++)
|
|
{
|
|
std::string sFeatureName = StdImageTypeTable[i][0];
|
|
std::string sImageName = StdImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else if (sLightingMode == "LumiTrax")
|
|
{
|
|
for (UINT i = 0; i < 15; i++)
|
|
{
|
|
std::string sFeatureName = LtrxNmlImageTypeTable[i][0];
|
|
std::string sImageName = LtrxNmlImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else if (sLightingMode == "MultiSpectrum")
|
|
{
|
|
for (UINT i = 0; i < 11; i++)
|
|
{
|
|
std::string sFeatureName = MlspImageTypeTable[i][0];
|
|
std::string sImageName = MlspImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else if (sLightingMode == "LumiTraxSpecularReflection")
|
|
{
|
|
for (UINT i = 0; i < 16; i++)
|
|
{
|
|
std::string sFeatureName = SprfImageTypeTable[i][0];
|
|
std::string sImageName = SprfImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
else if (sLightingMode == "Capture3D")
|
|
{
|
|
for (UINT i = 0; i < 2; i++)
|
|
{
|
|
std::string sFeatureName = Cap3DImageTypeTable[i][0];
|
|
std::string sImageName = Cap3DImageTypeTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (UINT i = 0; i < 8; i++)
|
|
{
|
|
setIntegerValue("FilteredImageSelector", (i + 1));
|
|
|
|
std::string sFeatureName = FilteredImageTable[i][0];
|
|
std::string sImageName = FilteredImageTable[i][1];
|
|
getBooleanValue(sFeatureName, sEnable);
|
|
if (sEnable)
|
|
{
|
|
EnableImageType.push_back(sImageName);
|
|
}
|
|
}
|
|
|
|
UINT ImageTypeSize = (UINT)EnableImageType.size();
|
|
if (ImageTypeSize < 5)
|
|
{
|
|
for (int i = ImageTypeSize; i < 5; i++)
|
|
{
|
|
EnableImageType.push_back("");
|
|
}
|
|
}
|
|
|
|
qDebug() << ("Select an image type.\n\n");
|
|
|
|
for (UINT i = 0; i < ImageTypeSize; ++i)
|
|
{
|
|
qDebug() << ((std::to_string(i + 1) + " :" + EnableImageType[i] + "\n").c_str());
|
|
}
|
|
|
|
uint32_t iSelected = atoi(sImageType.c_str());
|
|
if ((iSelected == 0) || (iSelected > ImageTypeSize))
|
|
{
|
|
qDebug() << ("The entry is invalid.\n");
|
|
return EnableImageType;
|
|
}
|
|
return EnableImageType;
|
|
}
|
|
|
|
void ConvertRGBA8toBGRA8(void* buffer, int pixelSize)
|
|
{
|
|
std::byte dataR;
|
|
std::byte dataB;
|
|
|
|
for (int i = 0; i < pixelSize; i++)
|
|
{
|
|
dataR = *((std::byte*)(buffer)+(i * 4));
|
|
dataB = *((std::byte*)(buffer)+(i * 4) + 2);
|
|
|
|
*((std::byte*)(buffer)+(i * 4)) = dataB;
|
|
*((std::byte*)(buffer)+(i * 4) + 2) = dataR;
|
|
}
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region Acquisition control
|
|
|
|
bool AcquisitionStart()
|
|
{
|
|
KglResult result;
|
|
|
|
result = kglStream->startAcquisition(*kglDevice);
|
|
if (KGL_SUCCESS != result) {
|
|
qDebug() << ("Error : acquisitionStart\n error:")<< result;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void AcquisitionStop()
|
|
{
|
|
KglResult result;
|
|
|
|
result = kglStream->stopAcquisition(*kglDevice);
|
|
if (KGL_SUCCESS != result) {
|
|
qDebug() << ("Error : acquisitionStop\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool QueueBuffer()
|
|
{
|
|
KglResult result;
|
|
KglResult operationResult;
|
|
|
|
uint32_t payloadsize = kglDevice->getPayloadSize();
|
|
if (payloadsize == 0)
|
|
{
|
|
qDebug() << ("Error : getPayloadSize\n");
|
|
return false;
|
|
}
|
|
|
|
kglBuffer = new KglBuffer();
|
|
result = kglBuffer->allocate(payloadsize);
|
|
if (KGL_SUCCESS != result)
|
|
{
|
|
qDebug() << ("Error : allocate\n");
|
|
return false;
|
|
}
|
|
|
|
result = kglStream->queueBuffer(kglBuffer);
|
|
if ((KGL_SUCCESS != result) && (KGL_PENDING != result))
|
|
{
|
|
qDebug() << ("Error : queueBuffer\n");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
cv::Mat RetrieveBuffer()
|
|
{
|
|
KglResult result;
|
|
KglResult operationResult;
|
|
|
|
result = kglStream->retrieveBuffer(&kglBuffer, operationResult);
|
|
if ((KGL_SUCCESS != result) || (KGL_SUCCESS != operationResult))
|
|
{
|
|
if (kglBuffer != NULL)
|
|
{
|
|
kglBuffer->free();
|
|
delete(kglBuffer);
|
|
kglBuffer = NULL;
|
|
}
|
|
qDebug() << ("Error : retrieveBuffer\n error:")<< kglStream;
|
|
return Mat();
|
|
}
|
|
|
|
UINT bmpSize = kglBuffer->getAcquiredSize();
|
|
char* data = (char*)malloc(bmpSize);
|
|
|
|
CopyMemory((void*)data, kglBuffer->getDataPointer(), bmpSize);
|
|
|
|
cv::Mat mat;
|
|
int64_t width;
|
|
int64_t height;
|
|
std::string sPixelFormat;
|
|
getIntegerValue("Width", width);
|
|
getIntegerValue("Height", height);
|
|
getEnumValue("PixelFormat", sPixelFormat);
|
|
|
|
if (sPixelFormat == "Mono8")
|
|
{
|
|
mat = cv::Mat(height, width, CV_8UC1, data);
|
|
}
|
|
|
|
else if (sPixelFormat == "Mono16")
|
|
{
|
|
mat = cv::Mat(height, width, CV_16UC1, data);
|
|
}
|
|
|
|
else if (sPixelFormat == "BGR8Packed")
|
|
{
|
|
mat = cv::Mat(height, width, CV_8UC3, data);
|
|
}
|
|
|
|
else if (sPixelFormat == "RGB8Packed")
|
|
{
|
|
mat = cv::Mat(height, width, CV_8UC3, data);
|
|
cv::cvtColor(mat, mat, COLOR_BGR2RGB);
|
|
|
|
}
|
|
else if (sPixelFormat == "BGRA8Packed")
|
|
{
|
|
mat = cv::Mat(height, width, CV_8UC4, data);
|
|
}
|
|
|
|
else if (sPixelFormat == "RGBA8Packed")
|
|
{
|
|
mat = cv::Mat(height, width, CV_8UC3, data);
|
|
cv::cvtColor(mat, mat, COLOR_BGRA2RGBA);
|
|
}
|
|
|
|
if (kglBuffer != NULL)
|
|
{
|
|
kglBuffer->free();
|
|
delete(kglBuffer);
|
|
kglBuffer = NULL;
|
|
}
|
|
|
|
cv::Mat bmpMat;
|
|
mat.copyTo(bmpMat);
|
|
if (data)
|
|
{
|
|
free(data);
|
|
}
|
|
//imwrite("C:\\pic\\Img" + to_string(imgNum) + ".bmp", bmpMat);
|
|
//imgNum++;
|
|
return bmpMat;
|
|
}
|
|
|
|
void SaveBMP(cv::Mat bmp)
|
|
{
|
|
std::string inputkey;
|
|
std::string sFilePath;
|
|
|
|
qDebug() << ("Do you want to save the image as a BMP file?\n\n");
|
|
qDebug() << ("1 :Yes\n");
|
|
qDebug() << ("2 : No\n");
|
|
std::cin >> inputkey;
|
|
qDebug() << ("------------------------------\n");
|
|
switch (atoi(inputkey.c_str()))
|
|
{
|
|
case 1:
|
|
qDebug() << ("Enter the full path of the BMP file.\n\n");
|
|
std::cin >> sFilePath;
|
|
qDebug() << ("------------------------------\n");
|
|
break;
|
|
case 2:
|
|
return;
|
|
break;
|
|
default:
|
|
qDebug() << ("The entry is invalid.\n");
|
|
return;
|
|
break;
|
|
}
|
|
try
|
|
{
|
|
/*System::String^ FilePath = gcnew System::String(sFilePath.c_str());
|
|
FileStream^ stream = gcnew FileStream(FilePath, FileMode::Create, FileAccess::Write);
|
|
TiffBitmapEncoder^ encoder = gcnew TiffBitmapEncoder();
|
|
encoder->Compression = TiffCompressOption::None;
|
|
encoder->Frames->Add(BitmapFrame::Create(bmp));
|
|
encoder->Save(stream);*/
|
|
//cv::imwrite(sFilePath, bmp);
|
|
}
|
|
catch (char)
|
|
{
|
|
qDebug() << ("Failed to save the BMP file.\n");
|
|
qDebug() << ("------------------------------");
|
|
}
|
|
}
|
|
|
|
void TriggerSoftware()
|
|
{
|
|
Sleep(5000);
|
|
kgllFeatureNodes->executeCommand("TriggerSoftware");
|
|
}
|
|
|
|
|
|
bool AcquisitionStartEx_SingleFrame(bool bMultiCaptureUpdateImage, std::string sMultiCaptureImageType, std::vector<cv::Mat>& matVec)
|
|
{
|
|
std::string sPixelFormat;
|
|
|
|
setBooleanValue("MultiCaptureUpdateImage", bMultiCaptureUpdateImage);
|
|
setEnumValue("MultiCaptureImageType", sMultiCaptureImageType);
|
|
|
|
//cv::Mat bmp;
|
|
|
|
//bmp = QueueBuffer();
|
|
cv::Mat bmp;
|
|
|
|
if (!QueueBuffer())
|
|
return false;
|
|
|
|
AcquisitionStart();
|
|
bmp = RetrieveBuffer();
|
|
if (!bmp.empty())
|
|
{
|
|
matVec.push_back(bmp);
|
|
AcquisitionStop();
|
|
bmp.release();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
AcquisitionStop();
|
|
bmp.release();
|
|
return false;
|
|
}
|
|
//if (sPixelFormat == "RGBA8Packed")
|
|
//{
|
|
// ConvertRGBA8toBGRA8(BitmapImage->BackBuffer.ToPointer(), (BitmapImage->PixelWidth * BitmapImage->PixelHeight));
|
|
//}
|
|
|
|
//SaveBMP(bmp);
|
|
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
|
|
void MyThread::run()
|
|
{
|
|
while (m_runFlag)
|
|
{
|
|
std::vector<cv::Mat> matVecs;
|
|
if(AcquisitionStartEx_SingleFrame(TRUE, sImageType[0], matVecs)==true)//为true的话就是重新获取一组图片中的一张sImageType类型的图片 false就是在同一组图片获取sImageType类型的图片
|
|
for (int i = 1; i < sImageType.size(); i++)
|
|
AcquisitionStartEx_SingleFrame(FALSE, sImageType[i], matVecs);//为true的话就是重新获取一组图片中的一张sImageType类型的图片 false就是在同一组图片获取sImageType类型的图片
|
|
if (matVecs.size() > 0)
|
|
{
|
|
n_mutex->lock();
|
|
queuePic.append(matVecs);
|
|
m_flag = true;
|
|
n_mutex->unlock();
|
|
|
|
}
|
|
QThread::msleep(5);
|
|
}
|
|
}
|
|
|
|
Hd_25DCameraVJ_module* create()
|
|
{
|
|
qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << "Hd_MvCamera_module create success!";
|
|
return new Hd_25DCameraVJ_module;
|
|
}
|
|
|
|
void destory(Hd_25DCameraVJ_module* ptr)
|
|
{
|
|
if (ptr)
|
|
{
|
|
delete ptr;
|
|
ptr = nullptr;
|
|
}
|
|
qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << "Hd_MvCamera_module destory success!";
|
|
}
|
|
|
|
Hd_25DCameraVJ_module::Hd_25DCameraVJ_module()
|
|
{
|
|
//m_thread = new MyThread;
|
|
//m_mutex = new QMutex();
|
|
workThread = new MyThread();
|
|
}
|
|
|
|
Hd_25DCameraVJ_module::~Hd_25DCameraVJ_module()
|
|
{
|
|
if (workThread)
|
|
{
|
|
workThread->setFlag(false);
|
|
workThread->wait();
|
|
workThread->quit();
|
|
workThread->deleteLater();
|
|
workThread = nullptr;
|
|
}
|
|
|
|
Disconnect();
|
|
|
|
if (n_mutex)
|
|
{
|
|
delete n_mutex;
|
|
n_mutex = nullptr;
|
|
}
|
|
|
|
qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " delete success!";
|
|
}
|
|
|
|
bool Hd_25DCameraVJ_module::sendData(std::vector<cv::Mat>& mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(data)
|
|
|
|
n_mutex->lock();
|
|
if (queuePic.empty())
|
|
{
|
|
n_mutex->lock();
|
|
qCritical() << " [ error] " << __FUNCTION__ << " line:" << __LINE__ << " srcImage is null";
|
|
return false;
|
|
}
|
|
vector<cv::Mat> dstMat = queuePic.dequeue();
|
|
n_mutex->unlock();
|
|
//for (int i=0;i<dstMat.size();i++)
|
|
// imwrite("D://CCD//2.5//2.5D_" + to_string(clock()) + "_"+ sImageType [i] + ".bmp", dstMat[i]);
|
|
mat.assign(dstMat.begin(), dstMat.end());
|
|
qCritical() << " [ error] " << __FUNCTION__ << " line:" << __LINE__ << "get image";
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Hd_25DCameraVJ_module::reciveData(std::vector<cv::Mat>& mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(mat)
|
|
|
|
cameraIndex = data.toInt();
|
|
return true;
|
|
}
|
|
|
|
bool Hd_25DCameraVJ_module::initParas(QByteArray&)
|
|
{
|
|
#if 1
|
|
KglResult initRes = KglSystem::initialize();
|
|
Connect(0);
|
|
setBooleanValue("GevGVCPPendingAck", TRUE);
|
|
|
|
//ImportDeviceParameters("C://EVE2.5dConfig//CA-HL08MX_hardTrigger.XML");
|
|
ImportDeviceParameters("C://EVE2.5dConfig//CA-HL08MX_softWareTrigger.xml");
|
|
setEnumValue("AcquisitionMode", "SingleFrame");
|
|
bool bBGEnable;
|
|
getBooleanValue("ImageCaptureBufferEnable", bBGEnable);
|
|
if (bBGEnable)
|
|
{
|
|
executeCommand("ImageCaptureBufferClear");
|
|
executeCommand("BufferCaptureAcquisitionStart");
|
|
}
|
|
std::string sImageType1 = "1";
|
|
sImageType = GetEnableImageType(sImageType1); //获取图像类别 6张
|
|
|
|
std::string sTriggerMode;
|
|
getEnumValue("TriggerMode", sTriggerMode);
|
|
sTriggerSource = "";
|
|
if (sTriggerMode == "On")
|
|
{
|
|
getEnumValue("TriggerSource", sTriggerSource);
|
|
//if (sTriggerSource.compare("Software"))
|
|
// workThread->start();
|
|
qDebug() << __FUNCTION__ << __LINE__ << "sTriggerSource:" << QString::fromStdString(sTriggerSource);
|
|
}
|
|
else
|
|
{
|
|
//workThread->start();
|
|
qDebug() << __FUNCTION__ << __LINE__ << "hardTrigger";
|
|
}
|
|
std::vector<cv::Mat> matVecs;
|
|
workThread->start();
|
|
//workThread->run();
|
|
|
|
//KglResult res = kgllFeatureNodes->executeCommand("TriggerSoftware");
|
|
#endif
|
|
return true;
|
|
}
|
|
|
|
bool Hd_25DCameraVJ_module::run()//软触发
|
|
{
|
|
//qDebug() << "[info]" << __FUNCTION__ << " line:" << __LINE__ << " run start" <<QString::fromStdString(Username);
|
|
try
|
|
{
|
|
#if 0
|
|
if (queuePic.size() > 0)
|
|
{
|
|
n_mutex->lock();
|
|
result = true;
|
|
n_mutex->unlock();
|
|
softwareTriggerStatus = 0;
|
|
return true;
|
|
}
|
|
if (!sTriggerSource.compare("Software")&& softwareTriggerStatus==0)
|
|
{
|
|
KglResult res = kgllFeatureNodes->executeCommand("TriggerSoftware");//软触发
|
|
softwareTriggerStatus = 1;
|
|
return false;
|
|
}
|
|
#endif
|
|
int timeout = 0; m_flag = false;
|
|
KglResult res = kgllFeatureNodes->executeCommand("TriggerSoftware");//软触发
|
|
while (true)
|
|
{
|
|
if (m_flag == true)
|
|
{
|
|
//cout << "2.5d exist" << endl;
|
|
break;
|
|
}
|
|
if (timeout > 20000)
|
|
{
|
|
cout << "2.5d timeout" << endl;
|
|
break;//20秒
|
|
}
|
|
timeout++;
|
|
QThread::msleep(1);
|
|
}
|
|
m_flag = false;
|
|
return true;
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qDebug() << "[info] FUNCTION:" << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
|
}
|
|
//qDebug() << "[info]"<< __FUNCTION__ << " line:" << __LINE__ << " run end" ;
|
|
return true;
|
|
} |