⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snaptestdlg.cpp

📁 有关数字图像处理的一些源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
*
* 文件名称:SnapTestDlg.cpp
* 摘    要:主对话框源文件
*
*
* 修改内容:主要是添加注释,修改了单张拍摄函数
*******************************************************************/

#include "stdafx.h"
#include "SnapTest.h"
#include "SnapTestDlg.h"
#include "SaveDlg.h"
#include "ExposureDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//标明在注册表中的位置
#define APP_PATH TEXT("SOFTWARE\\SNAP")        

HHV  m_hHV;//标志数字摄像机设备句柄
//BOOL bSnapStart;
//BOOL bSnapEnd;
//BOOL bPrewEnd;
BITMAPINFO *m_pBmpInfo;//BITMAPINFO 结构指针,显示图像时使用
char m_chBmpBuf[2048];	//BIMTAPINFO 存储缓冲区,m_pBmpInfo即指向此缓冲区
BYTE * m_pRawBuffer[36];       //原始图像缓冲区指针
BYTE * m_ImageBuffer[36];      //结果图像缓冲区指针
//颜色查找表
BYTE m_pLutR[256] ;
BYTE m_pLutG[256] ;
BYTE m_pLutB[256] ; 

//曝光时间函数声明
void SetExposureTime(int nWindWidth,long lTintUpper,long lTintLower);

#define  MY_ZERO 0.000000001
//DWORD dwStartTime;
//DWORD dwElapsedTime;

//const
const int DeviceNum = 1; //初始摄像机设为1号机
HV_RESOLUTION Resolution = RES_MODE0; //数字摄像机初始分辨率
const HV_SNAP_MODE SnapMode = CONTINUATION;//采集模式初始设为连续采集
const HV_BAYER_LAYOUT Layout = BAYER_GR;   //Bayer图象数据的排列

//BAYER2RGB_NEIGHBOUR;BAYER2RGB_PIXELGROUPING;像素成组法的清晰度最高
const HV_BAYER_CONVERT_TYPE ConvertType = BAYER2RGB_NEIGHBOUR;//转化类型
const long Gain = 8;  //增益大小
const long ExposureTint_Upper = 60; //曝光时间最小值
long ExposureTint_Lower = 100;  //曝光时间最大值

//const long ShutterDelay = 0;
long ADCLevel = ADC_LEVEL0; //模拟/数字转换级别
const int XStart = 0;   //输出窗口左上角X坐标
const int YStart = 0;  //输出窗口左上角Y坐标
int Width =2048;//1280  //输出窗口宽度
int Height =1536;//1024  //输出窗口高度

//const int Width = 2048;//1280
//const int Height = 1536;//1024

HANDLE hThread;
DWORD ThreadID;




/******
*函数名称:ContinueSnap(void)
*
*函数介绍:连续拍摄图像
*
*
******/
void ContinueSnap(void);


BITMAPFILEHEADER bfh	= {0}; //文件头初始化
DWORD dwImageSize	= 0;  //图象大小
DWORD dwBytesRead	= 0;

//int nCount = 0;

CString str;  //文件名字符串
CString strPath; //保存路径字符串

HANDLE hFile = NULL; 

CRect ShowRect;  //显示窗口对象

BOOL bOpen;    //摄像机打开标志变量
BOOL bStart;   //摄像机循环拍摄开始标志
BOOL bSingleSnap; //摄像机单张拍摄标志

BOOL bRAW;  
BOOL bBMP;

CSnapTestDlg SnapTest;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()  //宏定义,消息文件处理消息
};
//about对话框
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) //用基类的构造函数来定义派生类的构造函数 
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnapTestDlg dialog

CSnapTestDlg::CSnapTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSnapTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSnapTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//加载图标资源

}

void CSnapTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSnapTestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSnapTestDlg, CDialog)
	//{{AFX_MSG_MAP(CSnapTestDlg)
	ON_WM_SYSCOMMAND()   
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_SNAP, OnSnap)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_LEVEL0, OnLevel0)
	ON_BN_CLICKED(IDC_LEVEL1, OnLevel1)
	ON_BN_CLICKED(IDC_LEVEL2, OnLevel2)
	ON_BN_CLICKED(IDC_LEVEL3, OnLevel3)
	ON_BN_CLICKED(IDC_MODE0, OnMode0)
	ON_BN_CLICKED(IDC_MODE1, OnMode1)
	ON_BN_CLICKED(IDC_MODE2, OnMode2)
	ON_BN_CLICKED(IDC_SINGLE, OnSingle)
	ON_MESSAGE(WM_SNAP_CHANGE, OnSnapChange)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_EXPOSURE, OnExposure)
	ON_BN_CLICKED(IDC_PREVIEW, OnPreview)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_CCDSTART_BUTTON, OnCcdstartButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnapTestDlg message handlers
//SnapTestDlg对话框初始化
BOOL CSnapTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();



	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	CButton *ModeButton = (CButton *)GetDlgItem(IDC_MODE0);
	ModeButton->SetCheck(1);
	
	CButton *LevelButton = (CButton *)GetDlgItem(IDC_LEVEL0);
	LevelButton ->SetCheck(1);
	
	for( int i = 0; i < 36 ; i++)
	{
		m_pRawBuffer[i] = new BYTE[Width * Height];//为原图分配内存空间
		m_ImageBuffer[i] = new BYTE[Width * Height * 3];//为保存图象分配空间
	}
	
	for(i=0;i<256;i++)
	{
		m_pLutR[i] = i;
		m_pLutG[i] = i;
		m_pLutB[i] = i;
	}
	
	//	bSnapStart = FALSE;
	//	bSnapEnd = FALSE;
	//	bPrewEnd = FALSE;
	m_pBmpInfo = NULL;
	

	
	
		
	// TODO: Add extra initialization here

//	SetTimer(1,10,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSnapTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
//绘制图标
void CSnapTestDlg::OnPaint() 
{
	CWnd *pnd = GetDlgItem(IDC_SHOW);
	pnd->GetWindowRect(m_BrushSwatch);
	ScreenToClient(&m_BrushSwatch);
	CClientDC dc(this);
	CBrush brush(RGB(0,0,0));
	dc.FillRect(&m_BrushSwatch,&brush);
	dc.DeleteDC();


	CWnd* pWnd = GetDlgItem(IDC_SHOW);
	CDC* pdc = pWnd->GetDC(); 
	pWnd->GetClientRect(&m_rc);

	CPen pen(PS_DOT, 1, RGB(255, 255, 255)); 
	CPen *pOldPen = pdc->SelectObject(&pen);  

	//去除背景
	pdc->SetBkMode(TRANSPARENT); 
	
    pdc->MoveTo(m_rc.left + m_rc.Width()/2,m_rc.top);
	pdc->LineTo(m_rc.left + m_rc.Width()/2,m_rc.bottom);
	pdc->MoveTo(m_rc.left,m_rc.Height()/2 + m_rc.top);
	pdc->LineTo(m_rc.right,m_rc.Height()/2 + m_rc.top);
	pdc->SelectObject(pOldPen);
	
	pdc->DeleteDC();



	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);//获得系统客户区尺度
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}


}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSnapTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSnapTestDlg::OnExit() 
{
	DestroyWindow();
}

/******
 *函数名称:SetExposureTime(int nWindWidth,long lTintUpper,long lTintLower);

 *函数介绍:设置曝光时间
 *输入参数:
 *        nWindWidth-图象窗口宽度
 *        lTintUpper-曝光时间最小值
 *        lTintLower-曝光时间最大值
 *
 *输出参数:(无)
 *返回值:  (无)

********/

void SetExposureTime(int nWindWidth,long lTintUpper,long lTintLower)
{
	int size = sizeof(HVTYPE);
	HVTYPE type;
    //获得设备信息
	HVGetDeviceInfo(m_hHV,DESC_DEVICE_TYPE, &type, &size);	
	
	//When outputwindow changes, change the exposure 
	//请参考曝光系数转换公式
	long lClockFreq = 24000000; 
	int nOutputWid = nWindWidth;
	double dExposure = 0.0;
	double dTint = max((double)lTintUpper/(double)lTintLower,MY_ZERO);
	if(type == HV1300UCTYPE || type == HV1301UCTYPE)	
	{
		long lTb = 0;
		dExposure = (dTint* lClockFreq + 180.0)/((double)nOutputWid + 244.0 + lTb);
	}
	else
	{		
		long lTb = 0;
		dExposure = (dTint* lClockFreq + 180.0)/((double)nOutputWid + 305.0 + lTb) + 1;
	}
	
	if (dExposure > 16383) 
		dExposure = 16383;
	HVAECControl(m_hHV, AEC_EXPOSURE_TIME, (long)dExposure);
	
}

//连续拍摄函数
void ContinueSnap(void)
{
    //保存为BMP格式
	if(bBMP == TRUE)
	{
		BYTE *pBuffer[6];

		for( int i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i];
		}
		HVSnapShot(m_hHV, pBuffer,6);//采集图像到内存区

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 6];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 12];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 18];
		}
		HVSnapShot(m_hHV, pBuffer,6);
		
		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 24];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		
		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 30];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 36 ; i++)
		{
			//Bayer格式转化为RGB24位
			ConvertBayer2Rgb(m_ImageBuffer[i],
				             m_pRawBuffer[i],
							 Width,Height,
							 ConvertType,
							 m_pLutR,m_pLutG,m_pLutB,
							 true,
							 Layout
							 );
		}

		
		CTime time;

		for( i = 0; i < 36 ; i++)
		{
			time = CTime::GetCurrentTime();
			str = time.Format("%H-%M-%S");
			str = strPath + str;// + ".bmp";
			str.Format("%s-%d.bmp",str, i);

			hFile = ::CreateFile(str,GENERIC_WRITE ,0,NULL,	CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
			if (hFile == INVALID_HANDLE_VALUE)//返回句柄是否存在
			{
			
				MessageBox(NULL,"不可用的文件句柄!","保存连续图片失败!",MB_OK);
			}
			else
			{
				//保存图象的文件头、信息头、象素区
				::WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytesRead, NULL );
				::WriteFile(hFile, m_pBmpInfo, sizeof(BITMAPINFOHEADER), &dwBytesRead, NULL );
				::WriteFile(hFile, m_ImageBuffer[i], dwImageSize, &dwBytesRead, NULL );
			}
			CloseHandle(hFile);
		}
	}
	//保存原始图像
	else
	{
		BYTE *pBuffer[6];

		for( int i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 6];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 12];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 18];
		}
		HVSnapShot(m_hHV, pBuffer,6);
		
		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 24];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		
		for( i = 0; i < 6 ; i++)
		{
			pBuffer[i] = m_pRawBuffer[i + 30];
		}
		HVSnapShot(m_hHV, pBuffer,6);

		//文件名
		CTime time;

		for( i = 0; i < 36 ; i++)
		{
			time = CTime::GetCurrentTime();
			str = time.Format("%H-%M-%S");
			str = strPath + str;// + ".raw";
			str.Format("%s-%d.raw",str, i);

			hFile = ::CreateFile(str,GENERIC_WRITE ,0,NULL,	CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
			if (hFile == INVALID_HANDLE_VALUE)
			{
				MessageBox(NULL,"不可用的文件句柄!","保存连续图片失败!",MB_OK);
			}
			else
			{
				::WriteFile(hFile, &(m_pBmpInfo->bmiHeader.biWidth), sizeof(DWORD), &dwBytesRead, NULL );
				::WriteFile(hFile, &(m_pBmpInfo->bmiHeader.biHeight), sizeof(DWORD), &dwBytesRead, NULL );
				::WriteFile(hFile, m_pRawBuffer[i], dwImageSize, &dwBytesRead, NULL );
			}
			CloseHandle(hFile);
		}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -