1、首先去大恒官网安装软件
安装完成后有以下文件夹
2、配置VS2010的环境
3、编写C++代码
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <sstream>
#include <opencv2\opencv.hpp>
#include <opencv.hpp>
#include <windows.h>
//相机头文件
#include "GxIAPI.h"
#include "DxImageProc.h"
#include <iostream>
using namespace std;
using namespace cv;
using std::vector;
using std::string;
using std::ostringstream;
ostringstream name;
GX_DEV_HANDLE m_hDevice; ///< 设备句柄
BYTE *m_pBufferRaw; ///< 原始图像数据
BYTE *m_pBufferRGB; ///< RGB图像数据,用于显示和保存bmp图像
int64_t m_nImageHeight; ///< 原始图像高
int64_t m_nImageWidth; ///< 原始图像宽
int64_t m_nPayLoadSize;
int64_t m_nPixelColorFilter; ///< Bayer格式
Mat test;
Mat gray;
#define MaxImageWidth 1292
#define MaxImageHeigth 964
#define WChouYang 1
#define HChouYang 1
static void GX_STDC OnFrameCallbackFun(GX_FRAME_CALLBACK_PARAM* pFrame)
{
if (pFrame->status == 0)
{
memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);
// RGB转换
DxRaw8toRGB24(m_pBufferRaw
, m_pBufferRGB
, (VxUint32)(m_nImageWidth)
, (VxUint32)(m_nImageHeight)
, RAW2RGB_NEIGHBOUR
, DX_PIXEL_COLOR_FILTER(m_nPixelColorFilter)
, false);
memcpy(test.data, m_pBufferRGB, m_nImageWidth*m_nImageHeight * 3);//m_pBufferRGB赋值给test
imshow("test",test);
imwrite("./picturecollection/image" + name.str() + ".bmp", test);
waitKey(15);
}
return;
}
/***********相机关闭函数**************/
void cameraoff()
{
GX_STATUS emStatus = GX_STATUS_SUCCESS;
//发送停采命令
emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_STOP);
//注销采集回调
emStatus = GXUnregisterCaptureCallback(m_hDevice);
cout<<"停止采集"<<emStatus<<endl;
//关闭设备
emStatus = GXCloseDevice(m_hDevice);
if (emStatus != GX_STATUS_SUCCESS)
{
// 错误处理
}
cout<<"设备已关闭"<<emStatus<<endl;
}
int CameraOpreation()
{
// 初始化库
GX_STATUS emStatus = GX_STATUS_SUCCESS;
emStatus = GXInitLib();
if (emStatus != GX_STATUS_SUCCESS)
{
return 1;
}
//打开相机
GX_OPEN_PARAM openParam;
uint32_t nDeviceNum = 0;
openParam.accessMode = GX_ACCESS_EXCLUSIVE;
openParam.openMode = GX_OPEN_INDEX;
openParam.pszContent = "1";
//打开设备
emStatus = GXOpenDevice(&openParam, &m_hDevice);
cout<<"emStatus="<<emStatus<<endl;
//cout<<"GX_STATUS_SUCCESS="<<GX_STATUS_SUCCESS<<endl;
// 枚举设备列表
emStatus = GXUpdateDeviceList(&nDeviceNum, 1000);
if ((emStatus != GX_STATUS_SUCCESS) || (nDeviceNum <= 0))
{
return 0;
}
//设置采集模式连续采集
emStatus = GXSetEnum(m_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);//设置自动采集模式
//
emStatus = GXSetInt(m_hDevice, GX_INT_ACQUISITION_SPEED_LEVEL, 1);
emStatus = GXSetEnum(m_hDevice, GX_ENUM_BALANCE_WHITE_AUTO, GX_BALANCE_WHITE_AUTO_CONTINUOUS);//设置自动白平衡
bool bColorFliter = false;
// 获取图像大小
emStatus = GXGetInt(m_hDevice, GX_INT_PAYLOAD_SIZE, &m_nPayLoadSize);
// 获取宽度
emStatus = GXGetInt(m_hDevice, GX_INT_WIDTH, &m_nImageWidth);
// 获取高度
emStatus = GXGetInt(m_hDevice, GX_INT_HEIGHT, &m_nImageHeight);
test.create(m_nImageHeight, m_nImageWidth, CV_8UC3);
//判断相机是否支持bayer格式
bool m_bColorFilter;
emStatus = GXIsImplemented(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_bColorFilter); 判断设备是否支持流通道数据包功能
if (m_bColorFilter)
{
emStatus = GXGetEnum(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_nPixelColorFilter);
}
m_pBufferRGB = new BYTE[(size_t)(m_nImageWidth * m_nImageHeight * 3)];// //输出图像BGR数据
if (m_pBufferRGB == NULL)
{
return false;
}
//为存储原始图像数据申请空间
m_pBufferRaw = new BYTE[(size_t)m_nPayLoadSize];
if (m_pBufferRaw == NULL)
{
delete[]m_pBufferRGB;
m_pBufferRGB = NULL;
return false;
}
/*********注册图像处理回调函数 **************/
emStatus = GXRegisterCaptureCallback(m_hDevice, NULL, OnFrameCallbackFun);
/**********发送开采命令**********/
emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_START);
cout<<"开始采集!"<<endl;
}
int Picturecatch()
{
CameraOpreation();
cout<<"get picture!"<<endl;
return 0;
}
void main(void)
{
Picturecatch();
waitKey(200);
Sleep(10000);
cameraoff();
}
测试效果如下:
文章来源——公众号:机器人视觉