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

📄 webcamdlg.cpp

📁 PC端通过WINCE端实现摄像功能
💻 CPP
字号:
// webcamDlg.cpp : implementation file
//

#include "stdafx.h"
#include "webcam.h"
#include "webcamDlg.h"
#include ".\Self\\StrClass.h"
#include ".\ZC030_Camera\\zc030xlib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HWND tmp;
CTCPCustom_CE* pTcp;
/////////////////////////////////////////////////////////////////////////////
// CWebcamDlg dialog

CWebcamDlg::CWebcamDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWebcamDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWebcamDlg)
		// 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);
	m_hDisplay = NULL;
	m_hThread = NULL;
	m_fIsStop = TRUE;
	m_hStopEvent = NULL;
	m_fIsSave = FALSE;


	tmp=this->m_hWnd;
}

void CWebcamDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWebcamDlg)
	DDX_Control(pDX, IDC_SAVE_TO_FILE, m_Save2File);
	DDX_Control(pDX, IDC_EXIT, m_Exit);
	DDX_Control(pDX, IDC_CAM_LIST, m_ctrlCamList);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_StartTCPServer, m_StartTCPServer);
	DDX_Control(pDX, IDC_CloseTCPServer, m_CloseTCPServer);
	DDX_Control(pDX, IDC_StartCamera, m_StartCamera);
	DDX_Control(pDX, IDC_StopCamera, m_StopCamera);
}

BEGIN_MESSAGE_MAP(CWebcamDlg, CDialog)
	//{{AFX_MSG_MAP(CWebcamDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_SAVE_TO_FILE, OnSaveToFile)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_StartTCPServer, &CWebcamDlg::OnBnClickedStarttcpserver)
	ON_BN_CLICKED(IDC_CloseTCPServer, &CWebcamDlg::OnBnClickedClosetcpserver)
	ON_BN_CLICKED(IDC_StartCamera, &CWebcamDlg::OnBnClickedStartcamera)
	ON_BN_CLICKED(IDC_StopCamera, &CWebcamDlg::OnBnClickedStopcamera)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWebcamDlg message handlers

BOOL CWebcamDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	int i = capInitCamera();
	if (i > 0)
	{
		for (int j = 0; j < i; j ++)
		{
			CString strCam;
			strCam.Format(TEXT("Num %d"), j);

			m_ctrlCamList.AddString(strCam);
		}

		m_ctrlCamList.SetCurSel(0);

		LPTSTR lpszClassName = TEXT("Display Window Class");
		m_hDisplay = CreateWindow(lpszClassName, TEXT("Capture Window"), WS_VISIBLE, 15, 15, 320, 240, this->GetSafeHwnd(), NULL, NULL, this);
		if (m_hDisplay != NULL)
		{		
			::SetWindowPos(m_hDisplay, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

			// Enable start button
			m_StartCamera.EnableWindow();

			m_hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
		}
	}
	else
	{
		TCHAR szText[128];
		switch (i)
		{
		case INVALID_MACHINE_ID:
			wsprintf(szText, TEXT("Machine Id error."));
			break;
		case ERROR_LOAD_LIBRARY:
			wsprintf(szText, TEXT("Load Iphlplib error."));
			break;
		case ERROR_GET_ADDR:
			wsprintf(szText, TEXT("Get address error."), i);
			break;
		case INVALID_DEVICE_ID:
			wsprintf(szText, TEXT("Device Id error."));
			break;
		default:
			wsprintf(szText, TEXT("Init camera error %d."), i);
			break;			
		}
		
		::MessageBox(NULL, szText, TEXT("Notice"), MB_OK | MB_TOPMOST);		
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWebcamDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	capCloseCamera();

	if (m_hDisplay)
	{
		::DestroyWindow(m_hDisplay);
		m_hDisplay = NULL;
	}

	if (m_hStopEvent)
	{
		CloseHandle(m_hStopEvent);
		m_hStopEvent = NULL;
	}

	EndDialog(0);
}
/*关键------------------------------------------------------*/
DWORD WINAPI CWebcamDlg::CaptureThreadProc(LPVOID lpParameter)
{
	CWebcamDlg *pThis = (CWebcamDlg *)lpParameter;
	if (pThis == NULL)
		return 0;

	int index = pThis->m_ctrlCamList.GetCurSel();
	// 
	if (0 != capSetVideoFormat(index, VIDEO_PALETTE_RGB24, VIDEO_SIZE_SIF))
	{
		return 0;
	}

	DWORD dwSize = 320 * 240 * 3;
	DWORD dwJpg = 40960;
	//DWORD dwTime;
	DWORD dwRtnSize[2]; /* 0 - for bmp, 1 - for jpeg */

	LPBYTE lpFrameBuffer = (LPBYTE) malloc (dwSize);
	LPBYTE lpJpgBuffer = (LPBYTE) malloc (dwJpg);
	if (lpFrameBuffer == NULL || lpJpgBuffer == NULL) goto finish;

	if (capStartCamera(index) != 0) 
		goto finish;

	while (!pThis->m_fIsStop)
	{		
		memset(lpFrameBuffer, 0, dwSize);
		memset(lpJpgBuffer, 0, dwJpg);
		dwRtnSize[0] = dwRtnSize[1] = 0;

		if (capGetPicture(index, lpFrameBuffer, dwSize, lpJpgBuffer, dwJpg, dwRtnSize) == 0)
		{

			if (pThis->m_fIsSave)
			{
				pThis->m_fIsSave = FALSE;
//*************************************************************JPEGData[xxxxxxxx];
				char *buf=(char *)lpJpgBuffer;
				string message="JPEGData["+Int2Str(dwRtnSize[1])+"]";
				pTcp->SendData(tmp,message.c_str() , message.length());
				pTcp->SendData(tmp,buf , dwRtnSize[1]);
                  //  BYTE sss[]="aaaa";
					
//pTcpCustom->~CTCPCustom_CE();
//**************************************************************

			//	pThis->MakeJpeg(TEXT("out.jpg"), lpJpgBuffer, dwRtnSize[1]);

				pThis->SetWindowTextW(s2ws(Int2Str(dwRtnSize[1])).c_str());
			}

				char *buf=(char *)lpJpgBuffer;
				string message="JPEGData["+Int2Str(dwRtnSize[1])+"]";
				pTcp->SendData(tmp,message.c_str() , message.length());
				Sleep(1500);
				pTcp->SendData(tmp,buf , dwRtnSize[1]);
                Sleep(1500);
		}
		else
		{
			Sleep(15);
		}		
	}
	
finish:
	capStopCamera(index);

	if (lpFrameBuffer)
	{
		free (lpFrameBuffer);
		lpFrameBuffer = NULL;
	}
	
	if (lpJpgBuffer)
	{
		free (lpJpgBuffer);
		lpJpgBuffer = NULL;
	}

	SetEvent(pThis->m_hStopEvent);
	return 0;
}

BOOL CWebcamDlg::MakeJpeg(LPCTSTR jpgFileName, LPBYTE pJpgData, DWORD size)
{
	if (jpgFileName == NULL || pJpgData == NULL || size <= 0) return FALSE;

	HANDLE hFile = NULL;
	DWORD dwWritten = 0;

	hFile = CreateFile(jpgFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		WriteFile(hFile, pJpgData, size, &dwWritten, NULL);
		CloseHandle(hFile);	
		
		return TRUE;
	}		

	return FALSE;
}

void CWebcamDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (m_StopCamera.IsWindowEnabled()) return;

	this->OnExit();
	
	CDialog::OnClose();
}

void CWebcamDlg::OnSaveToFile() 
{

	m_fIsSave = TRUE;

}

#define ESC_HOT_KEY 0x0000

LRESULT CALLBACK CWebcamDlg::CaptureWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static CWebcamDlg *pThis = NULL;
		
	switch(uMsg) 
	{
	case WM_CREATE:
		pThis = (CWebcamDlg *)((LPCREATESTRUCT) lParam)->lpCreateParams;

		if (pThis == NULL) return -1;

		RegisterHotKey(hWnd, ESC_HOT_KEY, NULL, VK_ESCAPE);
		
		return 0;
	case WM_HOTKEY:		
		if ((UINT) HIWORD(lParam) == VK_ESCAPE)
		{						
			::SetParent(hWnd, pThis->GetSafeHwnd());
			::SetWindowPos(hWnd, HWND_TOP, 15, 15, 320, 240, NULL);
		}
		
		return 0;
	case WM_DESTROY:
		UnregisterHotKey(hWnd, ESC_HOT_KEY);
		break;;
		
	default: break;
	}
	
	return (::DefWindowProc(hWnd, uMsg, wParam, lParam));
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CALLBACK  CWebcamDlg::OnClientConnect(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{
	CWebcamDlg * pDlg = (CWebcamDlg*)pWnd;

pTcp=pTcpCustom;
	//pLstConn->AddString(pTcpCustom->m_RemoteHost + _T("建立连接"));
}
//客户端SOCKET关闭事件处理函数
void  CALLBACK CWebcamDlg::OnClientClose(CWnd* pWnd,CTCPCustom_CE* pTcpCustom)
{

}

//服务器端收到来自客户端的数据
void CALLBACK CWebcamDlg::OnClientRead(CWnd* pWnd,CTCPCustom_CE* pTcpCustom,const char * buf,int len )
{

}

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

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


void CWebcamDlg::OnBnClickedStarttcpserver()
{
			UpdateData(TRUE);
	//设置m_tcpServer属性
   	m_tcpServer.m_LocalPort = 9000;
	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;
	}
	m_StartTCPServer.EnableWindow(FALSE);
	m_CloseTCPServer.EnableWindow(TRUE);	

}

void CWebcamDlg::OnBnClickedClosetcpserver()
{
	if (m_tcpServer.Close() <=0)
	{
		AfxMessageBox(_T("关闭TCP服务器失败"));
		return;
	}
}

void CWebcamDlg::OnBnClickedStartcamera()
{
		// TODO: Add your control notification handler code here
	m_fIsStop = FALSE;
	ResetEvent(m_hStopEvent);
	m_StopCamera.EnableWindow();
	m_StopCamera.SetFocus();
	m_StartCamera.EnableWindow(FALSE);	
	m_Exit.EnableWindow(FALSE);
	m_Save2File.EnableWindow();

	m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) CaptureThreadProc, this, 0, NULL);

	}

void CWebcamDlg::OnBnClickedStopcamera()
{
		// TODO: Add your control notification handler code here
	m_StartCamera.EnableWindow();
	m_StartCamera.SetFocus();
	m_StopCamera.EnableWindow(FALSE);
	m_Exit.EnableWindow();
	m_Save2File.EnableWindow(FALSE);

	if (m_hThread == NULL) return;

	m_fIsStop = TRUE;
	
	DWORD dwRtn = 0;
	dwRtn = WaitForSingleObject(m_hStopEvent, 500);
	if (dwRtn == WAIT_TIMEOUT)
	{
		TerminateThread(m_hThread, 0);
	}

	CloseHandle(m_hThread);
	m_hThread = NULL;
}

⌨️ 快捷键说明

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