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

📄 mainfrm.cpp

📁 WinCE下实现的罗技usb摄像头驱动程序 镜像启动後插入摄像头并输入驱动dll名称进行加载 附带测试程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "LGCAMCECTRL.h"

#include "MainFrm.h"
#include "StillCapDlg.h"
#include "CamSettingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// Array tbSTDButton contains relevant buttons of bitmap IDB_STD_SMALL_COLOR
static TBBUTTON tbButtons[] = {
	{0, 0,				TBSTATE_ENABLED, TBSTYLE_SEP,    0, 0, 0,  0},	
	{1, ID_APP_ABOUT,	TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
	{0, 0,				TBSTATE_ENABLED, TBSTYLE_SEP,    0, 0, 0,  0}
};
const int nNumButtons = 1;
const int nNumImages = 1;
const DWORD dwAdornmentFlags = 0; // exit button

BOOL fDraw = TRUE;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_MENU_PREVIEW_START, OnMenuPreviewStart)
	ON_COMMAND(ID_MENU_PREVIEW_STOP, OnMenuPreviewStop)
	ON_COMMAND(ID_MENU_PREVIEW_PAUSE, OnMenuPreviewPause)
	ON_COMMAND(ID_MENU_CAPSTILLIMAGE, OnMenuCapstillimage)
	ON_COMMAND(ID_MENU_CAMSETTING, OnMenuCamsetting)
	ON_COMMAND(ID_MENU_STARTNET, OnMenuStartnet)
	ON_COMMAND(ID_MENU_CLOSENET, OnMenuClosenet)
	ON_COMMAND(ID_MENU_TRANS_SIMAGE, OnMenuTransSimage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	pcurview = NULL;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Add the buttons and adornments to the CommandBar.
	if (!InsertButtons(tbButtons, nNumButtons, IDR_MAINFRAME, nNumImages) ||
	    !AddAdornments(dwAdornmentFlags))
	{
		TRACE0("Failed to add toolbar buttons\n");
		return -1;
	}

	((CLGCAMCECTRLApp*)AfxGetApp())->m_pMFrame=this;

	// 一些初始化操作	
	// Format of 1, 1, 3 is 160x120 at 15 fps
	wFmt = 1;
	wFrame = 1;    
	wInterval = 3;
	nFmtCount = 0;

	fDraw = TRUE;

	CString str;
	// See if we can talk to the camera
	int rc = InitCamera ();
	if (rc)
	{
		str.Format(_T("Can't open camera driver.\r\nError code = %d\r\nIs the camera plugged in?\r\n"), rc);		
		AfxMessageBox (str);
		return -1;
	}

	// See if imaging library is installed with JPEG support	
	rc = InitDisplayFrame (TEXT("JPEG"));
	if (rc == ERROR_NOT_SUPPORTED)
	{
		AfxMessageBox (_T("JPEG decoder not included in this build of the operating sytsem."));
		return -1;
	} 
	else if (rc)
	{
		str.Format(_T("An error occured loading the imaging library.\r\nError code = %d\r\nIs the imaging component in this build of Windows CE?\r\n"), rc);		
		AfxMessageBox (str);
		return -1;
	}
	else	
		ReleaseDisplayFrame ();  // Free up the object

	// Get the number and data on video formats in camera
	nFmtCount = dim (Formats);
	rc = GetVideoFormats (Formats, &nFmtCount);
	if (rc || (nFmtCount == 0))
	{
		str.Format(_T("he driver reports no format supported or an error getting formats.\r\nError code = %d"), rc);		
		AfxMessageBox (str);
		return -1;
	}
	if (Formats[0].wFormatType != VIDFORMAT_MJPEG)
	{
		str.Format(_T("This program only supports MJPEG stream formats.\r\nThe driver reports format %d supported\r\n"), Formats[0].wFormatType);
		AfxMessageBox (str);
		return -1;
	}

	// 网络初始化
	srvState = FALSE;
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs


	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//CE_Server
//
///////////////////////////////////////////////////////////////////////////////////
//客户端连接建立事件处理函数
void CALLBACK  CMainFrame::OnClientConnect(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{	
	RETAILMSG(1, (TEXT("***LGCAM: PC socket connect\r\n")));

	CMainFrame * pMainFrame = (CMainFrame*)pWnd;
	CLGCAMCECTRLView *pLGCamCtrlView = (CLGCAMCECTRLView *)pMainFrame->pcurview;
	if (pLGCamCtrlView)
	{		
		pLGCamCtrlView->UpdateData();
	}
	else
	{
		RETAILMSG(1, (TEXT("***LGCAM: Get pLGCamCtrlView Error\r\n")));
	}
	
}
//客户端SOCKET关闭事件处理函数
void  CALLBACK CMainFrame::OnClientClose(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{	
	
	CMainFrame * pMainFrame = (CMainFrame*)pWnd;
	CLGCAMCECTRLView *pLGCamCtrlView = (CLGCAMCECTRLView *)pMainFrame->pcurview;
	if (pTcpCustom == pMainFrame->m_tcpServer.pActiveCustom)
	{
		// PC端连接断开,关闭与PC的socket连接
		RETAILMSG(1, (TEXT("***LGCAM: PC socket close\r\n")));
		if (pLGCamCtrlView)
		{		
			pMainFrame->SetDlgItemText(IDC_EDIT_STATUS,_T("PC已断开"));
		}

		pMainFrame->m_tcpServer.pActiveCustom->Close();		
		pMainFrame->m_tcpServer.pActiveCustom = NULL;		
	}	
}

//服务器端收到来自客户端的数据
void CALLBACK CMainFrame::OnClientRead(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,N_Packet *pNP)
{	
	// 现在是单向传输,不需作接收数据处理
}

//客户端Socket错误事件处理函数
void CALLBACK CMainFrame::OnClientError(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,int nErrorCode)
{
	// 不处理网络错误
}

//服务器端Socket错误事件处理函数
void CALLBACK CMainFrame::OnServerError(CWnd* pWnd,CTCPServer_CE* pTcpServer_CE,int nErrorCode)
{
	// 不处理
}

///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

// 开始预览
void CMainFrame::OnMenuPreviewStart() 
{
	// TODO: Add your command handler code here
	// Start the stream
	StreamToClientWindow ();
}

void CMainFrame::OnMenuPreviewStop() 
{
	// TODO: Add your command handler code here
	// Stop the stream
	StopStreaming ();
}

void CMainFrame::OnMenuPreviewPause() 
{
	// TODO: Add your command handler code here
	// Stop the stream
	if (fDraw)
		fDraw = FALSE;
	else
		fDraw = TRUE;
}

void CMainFrame::OnMenuCapstillimage() 
{
	// TODO: Add your command handler code here
	LPBYTE pData = 0;
	DWORD dwSize = 0;
	FORMATPROPS fp;	
	int rc;

	CStillCapDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		// Go get that image!
		memset (&fp, 0, sizeof (fp));

		fp.wFormatIndex = dlg.scStruct.wFormat;
		fp.wFrameIndex = dlg.scStruct.wFrame;

		rc = GetStillImage (1, dlg.scStruct.wFrame, &fp, &pData, &dwSize);
		CString strerr;
		if (rc == 0)
		{
			// Write the file
			rc = WriteJPEG (dlg.scStruct.szFileName, pData, dwSize);
			if (rc != 0)
			{
				strerr.Format(L"Error writing still image to file rc %d\r\n", rc);
				AfxMessageBox(strerr);
			}			
		}
		else
		{
			strerr.Format(L"Error capturing still image rc %d\r\n", rc);
			AfxMessageBox(strerr);
		}

		// Free the buffer
		LocalFree (pData);

		// Report to the user
		CString str;
		if (rc == 0)
		{
			str.Format(_T("Image file created"));
		}			
		else if (rc == 258)
		{
			str.Format(_T("Timeout Error waiting for still.\r\n"));			
		}
		else 
		{
			str.Format(_T("Error capturing still.\r\nError %d"),rc);			
		}

		AfxMessageBox (str);
	}
}

void CMainFrame::OnMenuCamsetting() 
{
	// TODO: Add your command handler code here
	CCamSettingDlg dlg;	
	if (dlg.DoModal() == IDOK)
	{

	}
}

void CMainFrame::OnMenuStartnet() 
{
	// TODO: Add your command handler code here
	//如果没有进行连接,则启动连接
	if(srvState==FALSE)
	{
		//设置m_tcpServer属性
   		m_tcpServer.m_LocalPort = 20000;
		m_tcpServer.m_pOwnerWnd = this;
		m_tcpServer.OnClientConnect = OnClientConnect;
		m_tcpServer.OnClientClose = OnClientClose;
		m_tcpServer.OnClientRead = OnClientRead;
		m_tcpServer.OnClientError = OnClientError;
		m_tcpServer.OnServerError = OnServerError;
		
		if (m_tcpServer.Open() <= 0)
		{
			AfxMessageBox(_T("监听失败"));
			return;
		}	
		
		srvState = TRUE;		
		AfxMessageBox(_T("启用网络成功"));
	}
}

void CMainFrame::OnMenuClosenet() 
{
	// TODO: Add your command handler code here
	if(srvState==TRUE)
	{  
		//如果连接已经建立,那么断开连接
		if (m_tcpServer.Close() <=0)
		{
			AfxMessageBox(_T("断开网络失败"));			
			return;
		}
		srvState = FALSE;
		AfxMessageBox(_T("断开网络成功"));		
	}	
}

/////////////////////////////////////////////////////////////////////////////////////////
int CMainFrame::StreamToClientWindow (void)
{
	
	// Setting right and bot to 0 tells the code to use frame size
	RECT rect;
	SetRect (&rect, 0, 0, 160, 120);

	// Find the frames per second value 
	DWORD dwFrameInterval = -1;  // Assume slowest interval
	if (wInterval < Formats[wFrame].nNumInterval)
		dwFrameInterval = Formats[wFrame].dwInterval[wInterval];
	
	// 获取视图指针
	CLGCAMCECTRLView *pLGCamCtrlView = (CLGCAMCECTRLView *)pcurview;
	if (pLGCamCtrlView == NULL)
	{
		RETAILMSG(1, (TEXT("***LGCAM: Can't Get pLGCamCtrlView\r\n")));
		return -1;
	}
	CStatic *pVideoWindow = (CStatic *)(pLGCamCtrlView->GetDlgItem(IDC_VIDEO_WINDOW));
	ASSERT(pVideoWindow);
	CDC *pDC = pVideoWindow->GetDC();
	HDC hdcVideo = pDC->GetSafeHdc();
	StartStreaming (hdcVideo, &rect, wFmt, wFrame, dwFrameInterval);
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////
BOOL CMainFrame::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	ShutdownCamera ();

	return CFrameWnd::DestroyWindow();
}

void CMainFrame::OnMenuTransSimage() 
{
	// TODO: Add your command handler code here
	LPBYTE pData = 0;
	DWORD dwSize = 0;
	FORMATPROPS fp;	
	int rc;

	CStillCapDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		// Go get that image!
		memset (&fp, 0, sizeof (fp));

		fp.wFormatIndex = dlg.scStruct.wFormat;
		fp.wFrameIndex = dlg.scStruct.wFrame;

		rc = GetStillImage (1, dlg.scStruct.wFrame, &fp, &pData, &dwSize);
		CString strerr;
		if (rc == 0)
		{
			// Write the file
			rc = WriteJPEG (dlg.scStruct.szFileName, pData, dwSize);
			if (rc != 0)
			{
				strerr.Format(L"Error writing still image to file rc %d\r\n", rc);
				AfxMessageBox(strerr);
			}			
		}
		else
		{
			strerr.Format(L"Error capturing still image rc %d\r\n", rc);
			AfxMessageBox(strerr);
		}		

		// Report to the user
		CString str;
		if (rc == 0)
		{
			str.Format(_T("Image file created"));
			/*
			// 传输测试
			HRESULT hr = FALSE;
			hr = SendCMDData(C_T_STILL_IMAGE,(char *)pData,dwSize);			
			
			if (hr == FALSE)
			{
				RETAILMSG(1, (TEXT("***LGCAM  Send Still Image Fail!\r\n")));	
			}	
			
			RETAILMSG(1, (TEXT("***LGCAM  dwSize =%d!\r\n"),dwSize));
			*/
		}			
		else if (rc == 258)
		{
			str.Format(_T("Timeout Error waiting for still.\r\n"));			
		}
		else 
		{
			str.Format(_T("Error capturing still.\r\nError %d"),rc);			
		}

		// Free the buffer
		LocalFree (pData);

		AfxMessageBox (str);
	}
}

//////////////////////////////////////////////////////////////////////
//
// 底层网络交互
//////////////////////////////////////////////////////////////////////

// 发送命令给终端
void CMainFrame::SendCMD(long cmd)
{
	// 安全发送数据
	if (m_tcpServer.pActiveCustom)
	{
		m_tcpServer.pActiveCustom->SendCMD(cmd);
	}
}

// 向终端发送命令数据
bool CMainFrame::SendCMDData(long cmd, const char * pbuf , int len)
{
	BOOL hr = FALSE;
	// 安全发送数据
	if (m_tcpServer.pActiveCustom)
	{
		hr = m_tcpServer.pActiveCustom->SendCMDData(cmd,pbuf,len);
	}
	return hr;
}

// 处理网络命令
void CMainFrame::ProcessWebCmd(void)
{
	CString msg;
	// 解析包类型
	switch (P_RCV.packet_type)
	{			
	// PC命令
	case PH_PC_CMD:		//--PC信息命令包--//
		{
			/*
		// 解析命令	
		switch (P_RCV.packet_cmd)
		{						
		default:		
			break;
		}
		*/
		break;
		}

	// PC命令数据
	case PH_PC_CMD_DATA:				//--PC信息命令数据包--//
		{
		// 解析命令	
			/*
		switch (P_RCV.packet_cmd)
		{
		
		default:
			{
				// 发送反馈信息
				SendCMD(C_T_EXE_ERR);
				break;
			}
		}
		*/
		break;
		}
		
	default:
		{				
			RETAILMSG(1, (TEXT("***LGCAM  <S>Receive unknown info!\r\n")));				
			break;
		}
	}
	
}

⌨️ 快捷键说明

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