代码
使用了回调采集,在while(1){}循环时,控制台窗口会不断的输出图像的尺寸,说明是已经采集到了图像了。
#include<iostream>
#include<tchar.h>
//请用户提前配置好工程头文件目录,需要包含GalaxyIncludes.h
#include"GalaxyIncludes.h"
#include<opencv2\core.hpp>
#include<opencv2\imgproc.hpp>
#include<opencv2\highgui.hpp>
#include<string>
using namespace std;
using namespace cv;
class CSampleCaptureEventHandler : public ICaptureEventHandler{
public:
void DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam)
{
if (objImageDataPointer->GetStatus() == GX_FRAME_STATUS_SUCCESS)
{
//图像获取为完整帧,可以读取图像宽、高、数据格式等
uint64_t nWidth = objImageDataPointer->GetWidth();
uint64_t nHeight = objImageDataPointer->GetHeight();
GX_PIXEL_FORMAT_ENTRY emPixelFormat = objImageDataPointer->GetPixelFormat();
//其他图像信息的获取参见IImageData接口定义
//如果正常连续输出图片的宽,高,说明处于连续采集的状态
cout << nWidth << " " << nHeight << endl;
//cv::Mat SrcImage(objImageDataPointer->GetHeight(), objImageDataPointer->GetWidth(), CV_8UC1);
//void* pRaw8Buffer = NULL;
//pRaw8Buffer = objImageDataPointer->ConvertToRaw8(GX_BIT_0_7);
//memcpy(SrcImage.data, pRaw8Buffer, (objImageDataPointer->GetHeight())*(objImageDataPointer->GetWidth()));
//cv::flip(SrcImage, SrcImage, 0);//大恒的图像要进行翻转
//imshow("1", SrcImage);
}
}
};
int main() {
//在调用其他接口之前必须调用IGXFactory::GetInstance().Init()接口执行初始化。
IGXFactory::GetInstance().Init();
//枚举设备
GxIAPICPP::gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
for (uint32_t i = 0; i < vectorDeviceInfo.size(); i++) {
cout << vectorDeviceInfo[i].GetVendorName() << endl;
cout << vectorDeviceInfo[i].GetModelName() << endl;
//更多设备信息详见IGXDeviceInfo接口
}
//打开设备
if (vectorDeviceInfo.size() > 0) {
//打开链表中的第一个设备
CGXDevicePointer objDevicePtr;
GxIAPICPP::gxstring strSN = vectorDeviceInfo[0].GetSN();
cout << strSN << endl;
objDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(strSN, GX_ACCESS_EXCLUSIVE);//目前只能通过strSN打开
CGXStreamPointer objStreamPtr = objDevicePtr->OpenStream(0);
//注册采集回调函数,注意第一个参数是用户私有参数,用户可以传入任何object对象,也可以是null
//用户私有参数在回调函数内部还原使用,如果不使用私有参数,可以传入null
ICaptureEventHandler* pCaptureEventHandler = new CSampleCaptureEventHandler();
objStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, NULL);
//开启流通道采集
objStreamPtr->StartGrab();
//给设备发送开采命令
CGXFeatureControlPointer objFeatureControlPtr = objDevicePtr->GetRemoteFeatureControl();
objFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
//回调采集过程,参见回调函数
//while (1) {
// //进入循环之后会进行连续采集,不断地打印出图片的长和宽
//}
//停采、注销采集回调函数
objFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
objStreamPtr->StopGrab();
objStreamPtr->UnregisterCaptureCallback();
delete pCaptureEventHandler;
pCaptureEventHandler = NULL;
objDevicePtr->Close();//关闭相机并释放资源
}
IGXFactory::GetInstance().Uninit();
return 0;
}
示例代码
使用相机型号:大恒MER-503-36U3M黑白相机
发现注册远端相机事件会触发异常,因此将这段代码给注释掉了,否则会直接跳入异常而跳出。
这段代码在控制台中使用了了一个getchar()命令,方便在后期的时候能够跳出控制台程序
//#include"stdafx.h"
#include<iostream>
using namespace std;
//请用户提前配置好工程头文件目录,需要包含GalaxyIncludes.h
#include"GalaxyIncludes.h"
//用户继承掉线事件处理类
class CSampleDeviceOfflineEventHandler : public IDeviceOfflineEventHandler
{
public:
void DoOnDeviceOfflineEvent(void* pUserParam)
{
cout<<"收到设备掉线事件!"<<endl;
}
};
//用户继承属性更新事件处理类
class CSampleFeatureEventHandler : public IFeatureEventHandler
{
public:
void DoOnFeatureEvent(const GxIAPICPP::gxstring&strFeatureName, void* pUserParam)
{
cout<<"收到曝光结束事件!"<<endl;
}
};
//用户继承采集事件处理类
class CSampleCaptureEventHandler : public ICaptureEventHandler
{
public:
void DoOnImageCaptured(CImageDataPointer&objImageDataPointer, void* pUserParam)
{
cout<<"收到一帧图像!"<<endl;
cout<<"ImageInfo: "<<objImageDataPointer->GetStatus() <<endl;
cout<<"ImageInfo: "<<objImageDataPointer->GetWidth() <<endl;
cout<<"ImageInfo: "<<objImageDataPointer->GetHeight() <<endl;
cout<<"ImageInfo: "<<objImageDataPointer->GetPayloadSize() <<endl;
}
};
int main(int argc, char* argv[])
{
//声明事件回调对象指针
IDeviceOfflineEventHandler* pDeviceOfflineEventHandler = NULL;///<掉线事件回调对象
IFeatureEventHandler* pFeatureEventHandler = NULL;///<远端设备事件回调对象
ICaptureEventHandler* pCaptureEventHandler = NULL;///<采集回调对象
//初始化
IGXFactory::GetInstance().Init();
try
{
do
{
//枚举设备
gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
if (0 == vectorDeviceInfo.size())
{
cout<<"无可用设备!"<<endl;
break;
}
//打开第一台设备以及设备下面第一个流
CGXDevicePointer ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(
vectorDeviceInfo[0].GetSN(),
GX_ACCESS_EXCLUSIVE);
CGXStreamPointer ObjStreamPtr = ObjDevicePtr->OpenStream(0);
//注册设备掉线事件【目前只有千兆网系列相机支持此事件通知】
GX_DEVICE_OFFLINE_CALLBACK_HANDLE hDeviceOffline = NULL;
pDeviceOfflineEventHandler = new CSampleDeviceOfflineEventHandler();
hDeviceOffline = ObjDevicePtr->RegisterDeviceOfflineCallback(pDeviceOfflineEventHandler, NULL);
//获取远端设备属性控制器
CGXFeatureControlPointer ObjFeatureControlPtr = ObjDevicePtr->GetRemoteFeatureControl();
//设置曝光时间(示例中写死us,只是示例,并不代表真正可工作参数)
ObjFeatureControlPtr->GetFloatFeature("ExposureTime")->SetValue(20000);
注册远端设备事件:曝光结束事件【目前只有千兆网系列相机支持曝光结束事件】
选择事件源
//ObjFeatureControlPtr->GetEnumFeature("EventSelector")->SetValue("ExposureEnd");
使能事件
//ObjFeatureControlPtr->GetEnumFeature("EventNotification")->SetValue("On");
//GX_FEATURE_CALLBACK_HANDLE hFeatureEvent = NULL;
//pFeatureEventHandler = new CSampleFeatureEventHandler();
//hFeatureEvent = ObjFeatureControlPtr->RegisterFeatureCallback(
// "EventExposureEnd",
// pFeatureEventHandler,
// NULL);
//注册回调采集
pCaptureEventHandler = new CSampleCaptureEventHandler();
ObjStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, NULL);
//发送开采命令
ObjStreamPtr->StartGrab();
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
//此时开采成功,控制台打印信息,直到输入任意键继续
getchar();
//发送停采命令
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
ObjStreamPtr->StopGrab();
//注销采集回调
ObjStreamPtr->UnregisterCaptureCallback();
//注销远端设备事件
//ObjFeatureControlPtr->UnregisterFeatureCallback(hFeatureEvent);
//注销设备掉线事件
ObjDevicePtr->UnregisterDeviceOfflineCallback(hDeviceOffline);
//释放资源
ObjStreamPtr->Close();
ObjDevicePtr->Close();
} while (0);
}
catch(CGalaxyException&e)
{
cout<<"错误码: "<<e.GetErrorCode() <<endl;
cout<<"错误描述信息: "<<e.what() <<endl;
}
catch(std::exception&e)
{
cout<<"错误描述信息: "<<e.what() <<endl;
}
//反初始化库
IGXFactory::GetInstance().Uninit();
//销毁事件回调指针
if (NULL != pCaptureEventHandler)
{
delete pCaptureEventHandler;
pCaptureEventHandler = NULL;
}
if (NULL != pDeviceOfflineEventHandler)
{
delete pDeviceOfflineEventHandler;
pDeviceOfflineEventHandler = NULL;
}
if (NULL != pFeatureEventHandler)
{
delete pFeatureEventHandler;
pFeatureEventHandler = NULL;
}
return 0;
}