📄 usbcameradlg.cpp
字号:
// USBCameraDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "USBCamera.h"
#include "USBCameraDlg.h"
#include "webcamsdk.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define MAXFORMATS 15
FORMATPROPS Formats[MAXFORMATS];
CUSBCameraDlg::CUSBCameraDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUSBCameraDlg::IDD, pParent)
, pCameraCode(NULL)
, pMjpeg2bmp(NULL)
, nFmtCount(0)
, m_bCameraOpen(false)
, wFmt(1)
, wFrame(1)
, wInterval(3)
, IMAGE_WIDTH(160)
, IMAGE_HEIGHT(120)
, m_strFileName(_T("Sample.jpg"))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUSBCameraDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_edtImageName, m_strFileName);
}
BEGIN_MESSAGE_MAP(CUSBCameraDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_btnOpen, &CUSBCameraDlg::OnBnClickedbtnopen)
ON_BN_CLICKED(IDC_btnGetStillImage, &CUSBCameraDlg::OnBnClickedbtngetstillimage)
ON_BN_CLICKED(IDC_btnStartCapture, &CUSBCameraDlg::OnBnClickedbtnstartcapture)
ON_BN_CLICKED(IDC_btnStopCapture, &CUSBCameraDlg::OnBnClickedbtnstopcapture)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_btnClose, &CUSBCameraDlg::OnBnClickedbtnclose)
ON_CBN_SELENDOK(IDC_cmbImageSize, &CUSBCameraDlg::OnCbnSelendokcmbimagesize)
ON_CBN_SELENDOK(IDC_cmbImageRate, &CUSBCameraDlg::OnCbnSelendokcmbimagerate)
END_MESSAGE_MAP()
// CUSBCameraDlg 消息处理程序
BOOL CUSBCameraDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ReSizeImage();
pCameraCode= new CCameraCode;
pMjpeg2bmp=new CMjpeg2bmp;
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CUSBCameraDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_USBCAMERA_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_USBCAMERA_DIALOG));
}
#endif
void CUSBCameraDlg::OnBnClickedbtnopen()
{
CString strMsg;
TCHAR szName[128];
int rc=pCameraCode->InitCamera();
if(rc)
{
strMsg.Format(_T("不能打开摄像头驱动.\r\n错误号=%d\r\n摄像头是否插入?"),rc);
MessageBox(strMsg,_T("系统提示"),MB_OK);
m_bCameraOpen=false;
return;
}
m_bCameraOpen=true;
rc=pMjpeg2bmp->InitDisplayFrame(_T("JPEG"));
if(rc==ERROR_NOT_SUPPORTED)
{
strMsg.Format(_T("操作系统不支持JPEG"));
MessageBox(strMsg,_T("系统提示"),MB_OK);
return;
}
else if(rc)
{
strMsg.Format(_T("装载Image库的出错。\r\n错误号=%d"),rc);
MessageBox(strMsg,_T("系统提示"),MB_OK);
return;
}
else
pMjpeg2bmp->ReleaseDisplayFrame();
nFmtCount = dim (Formats);
rc=pCameraCode->GetVideoFormats(Formats,&nFmtCount);
if (rc || (nFmtCount == 0))
{
strMsg.Format(_T("The driver reports no format supported or an error getting formats.\r\nError code = %d"),rc);
MessageBox(strMsg,_T("系统提示"),MB_OK);
return;
}
if (Formats[0].wFormatType != VIDFORMAT_MJPEG)
{
strMsg.Format(TEXT("This test program only supports MJPEG stream formats.\r\n")
TEXT("The driver reports format %d supported\r\n"), Formats[0].wFormatType);
MessageBox(strMsg,_T("系统提示"),MB_OK);
return;
}
CComboBox *pcmbImageSize=(CComboBox*)GetDlgItem(IDC_cmbImageSize);
for (int i = 0; (i < nFmtCount); i++)
{
wsprintf (szName, TEXT("%dx%d"), Formats[i].dwWidth, Formats[i].dwHeight);
pcmbImageSize->InsertString(i,szName);
if (i+1 == Formats[i].wFormatIndex )
pcmbImageSize->SetCurSel(i);
}
pcmbImageSize=(CComboBox*)GetDlgItem(IDC_cmbImageRate);
if (Formats[0].nNumInterval)
{
for (int i = 0; (i < Formats[0].nNumInterval); i++)
{
wsprintf (szName, TEXT("%d fps"), 1000/(Formats[0].dwInterval[i]/10000));
pcmbImageSize->InsertString(i,szName);
if (i ==3)
{
pcmbImageSize->SetCurSel(i);
}
}
}
StartCapture();
}
void CUSBCameraDlg::OnBnClickedbtngetstillimage()
{
UpdateData(TRUE);
LPBYTE pData = 0;
DWORD dwSize = 0;
FORMATPROPS fp;
int FmtCnt;
CString strMsg;
FORMATPROPS StillFormats[MAXFORMATS];
memset(&fp,0,sizeof(fp));
int rc=pCameraCode->GetStillFormats (StillFormats,&FmtCnt);
fp.wFormatIndex =StillFormats[0].wFormatIndex ;
fp.wFrameIndex =StillFormats[0].wFrameIndex ;
rc = pCameraCode->GetStillImage (1, StillFormats[0].wFrameIndex, &fp, &pData, &dwSize);
if (rc == 0)
{
rc = pCameraCode->WriteJPEG ((LPTSTR)(LPCTSTR)m_strFileName, pData, dwSize);
if (rc != 0)
MessageBox(_T("Error writing still image to file"),_T("System Information"),MB_OK);
}
else
{
MessageBox(_T ("Error capturing still image"),_T("System Information"),MB_OK);
}
/* pCameraCode->StopStreaming();
CWnd* pWnd=GetDlgItem(IDC_REMOTEVIDEO);
HWND hWnd=pWnd->m_hWnd;
HDC hDC=::GetDC(pWnd->m_hWnd);
RECT rect;
pWnd->GetClientRect(&rect);
pMjpeg2bmp->DisplayFrame(pData,512,fp.cbSize,hDC,&rect);*/
LocalFree (pData);
}
void CUSBCameraDlg::OnBnClickedbtnstartcapture()
{
if(m_bCameraOpen)
StartCapture();
}
void CUSBCameraDlg::OnBnClickedbtnstopcapture()
{
pCameraCode->StopStreaming();
}
void CUSBCameraDlg::OnDestroy()
{
CDialog::OnDestroy();
if(m_bCameraOpen)
pCameraCode->ShutdownCamera();
}
void CUSBCameraDlg::OnBnClickedbtnclose()
{
if(m_bCameraOpen)
{
m_bCameraOpen=FALSE;
pCameraCode->ShutdownCamera();
}
}
void CUSBCameraDlg::OnCbnSelendokcmbimagesize()
{
if(m_bCameraOpen)
{
pCameraCode->StopStreaming();
CComboBox *pcmbImageSize=(CComboBox*)GetDlgItem(IDC_cmbImageSize);
wFrame=pcmbImageSize->GetCurSel()+1;
switch(wFrame)
{
case 1:
IMAGE_WIDTH=160;
IMAGE_HEIGHT=120;
break;
case 2:
IMAGE_WIDTH=174;
IMAGE_HEIGHT=144;
break;
case 3:
IMAGE_WIDTH=320;
IMAGE_HEIGHT=240;
break;
case 4:
IMAGE_WIDTH=352;
IMAGE_HEIGHT=288;
break;
case 5:
IMAGE_WIDTH=432;
IMAGE_HEIGHT=240;
break;
case 6:
IMAGE_WIDTH=480;
IMAGE_HEIGHT=360;
break;
case 7:
IMAGE_WIDTH=512;
IMAGE_HEIGHT=288;
break;
case 8:
IMAGE_WIDTH=640;
IMAGE_HEIGHT=360;
break;
default:
IMAGE_WIDTH=160;
IMAGE_HEIGHT=120;
break;
}
ReSizeImage();
StartCapture();
}
}
void CUSBCameraDlg::StartCapture(void)
{
CWnd* pWnd=GetDlgItem(IDC_REMOTEVIDEO);
HWND hWnd=pWnd->m_hWnd;
HDC hDC=::GetDC(pWnd->m_hWnd);
RECT rect;
pWnd->GetClientRect(&rect);
DWORD dwFrameInterval = -1;
if (wInterval < Formats[wFrame].nNumInterval)
dwFrameInterval = Formats[wFrame].dwInterval[wInterval];
pCameraCode->fDraw=TRUE;
pCameraCode->StartStreaming(hDC,&rect,wFmt,wFrame,dwFrameInterval);
}
void CUSBCameraDlg::ReSizeImage(void)
{
CRect rect,brect;
CWnd *wnd=GetDlgItem(IDC_REMOTEVIDEO); // Video display window
CWnd *bwnd=GetDlgItem(IDC_REMOTEBORDER); // Border window...
bwnd->GetWindowRect(brect);
ScreenToClient(brect);
remote_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
remote_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
wnd->SetWindowPos(&wndTop,remote_wnd_x-4,remote_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);
}
void CUSBCameraDlg::OnCbnSelendokcmbimagerate()
{
if(m_bCameraOpen)
{
pCameraCode->StopStreaming();
CComboBox *pcmbImageRate=(CComboBox*)GetDlgItem(IDC_cmbImageRate);
wInterval=pcmbImageRate->GetCurSel()+1;
StartCapture();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -