📄 capture1view.cpp
字号:
// capture1View.cpp : implementation of the CCapture1View class
//
#include "stdafx.h"
#include "capture1.h"
#include "capture1Doc.h"
#include "capture1View.h"
#include <string.h>
#pragma comment( lib, "vfw32.lib" )
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCapture1View
IMPLEMENT_DYNCREATE(CCapture1View, CFormView)
BEGIN_MESSAGE_MAP(CCapture1View, CFormView)
//{{AFX_MSG_MAP(CCapture1View)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_BN_CLICKED(IDC_Startcap, OnStartcap)
ON_BN_CLICKED(IDC_FREEZED, OnFreezed)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_FORMAT, OnFormat)
ON_BN_CLICKED(DC_SOURCE, OnSource)
ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
ON_BN_CLICKED(IDC_FREEZED, OnFreezed)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCapture1View construction/destruction
CCapture1View::CCapture1View()
: CFormView(CCapture1View::IDD)
{
//{{AFX_DATA_INIT(CCapture1View)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
lpImage=(LPBYTE)new BYTE[1280*960*3]; //为lpImage指针申请内存
BmpInfo.biSize=40; //DWORD
BmpInfo.biWidth=640; //LONG
BmpInfo.biHeight=480; //LONG
BmpInfo.biPlanes=1; //WORD
BmpInfo.biBitCount=24; //WORD
BmpInfo.biCompression=0; //DWORD
BmpInfo.biSizeImage=0; //DWORD
BmpInfo.biXPelsPerMeter=2835; //LONG
BmpInfo.biYPelsPerMeter=2835; //LONG
BmpInfo.biClrUsed=0; //DWORD
BmpInfo.biClrImportant=0; //DWORD
strcpy(Bmp.PathName,"");
Doc=NULL; //不能在此初始化文档指针
}
CCapture1View::~CCapture1View()
{
delete[] lpImage;
}
void CCapture1View::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCapture1View)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CCapture1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CCapture1View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
if(Doc==NULL) Doc=GetDocument(); //在此初始化文档指针
}
/////////////////////////////////////////////////////////////////////////////
// CCapture1View printing
BOOL CCapture1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCapture1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCapture1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CCapture1View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CCapture1View diagnostics
#ifdef _DEBUG
void CCapture1View::AssertValid() const
{
CFormView::AssertValid();
}
void CCapture1View::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CCapture1Doc* CCapture1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCapture1Doc)));
return (CCapture1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCapture1View message handlers
short CCapture1View::LoadBmpFile()
{
CFile f;
BOOL OK;
OK=f.Open( Bmp.PathName,
CFile::modeRead|CFile::typeBinary|CFile::shareExclusive,
NULL
);
if(!OK) return(-1);
f.Read(&FileHead,sizeof(FileHead));
if(FileHead.bfType!=0x4d42) {
f.Close();
return -2;
}
f.Read(&BmpInfo,sizeof(BmpInfo));
if(BmpInfo.biWidth>1280 || BmpInfo.biHeight>960) return(-3);
short x,y,z;
z=(BmpInfo.biWidth/4)*4+(BmpInfo.biWidth%4==0 ? 0 : 4);
f.Seek(FileHead.bfOffBits,0);
f.Read(lpImage,BmpInfo.biHeight*z*3);
f.Close();
for(y=BmpInfo.biHeight-1;y>=0;y--){
memcpy(Doc->C3[y],lpImage+(BmpInfo.biHeight-1-y)*z*3,z*3);
for(x=0;x<BmpInfo.biWidth;x++){
Doc->pix[y][x]=(Doc->C3[y][x].R*77+Doc->C3[y][x].G*150+Doc->C3[y][x].B*29)>>8;///256;
}
}
return(1);
}
void CCapture1View::OnDraw(CDC *pDC)
{
//----------- 显示图象--------------------
if(Bmp.PathName[0]!='\0')
{
LPBITMAPINFOHEADER lpBMIH;
lpBMIH=&BmpInfo;
pDC->SetStretchBltMode(COLORONCOLOR);//清除显示区
::StretchDIBits(pDC->GetSafeHdc(),
510,11,352,288,
0,0,BmpInfo.biWidth,BmpInfo.biHeight,
lpImage,(LPBITMAPINFO)lpBMIH,
DIB_RGB_COLORS,SRCCOPY
);
}
}
void CCapture1View::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog file( TRUE, //打开文件
_T("*.BMP,"),
Bmp.PathName, //初始显示在文件名编辑框中的文件名
OFN_HIDEREADONLY|OFN_EXPLORER,
_T("图像文件(*.BMP,)|*.BMP;|"),
NULL
);
if(file.DoModal( )==IDCANCEL) return ;
strcpy(Bmp.PathName,file.GetPathName());
BeginWaitCursor();
short OK;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(Bmp.PathName, drive, dir, fname, ext);
if(!stricmp(ext,".Bmp"))
{
OK=LoadBmpFile();
}
else OK=-1;
if(OK==-1) { MessageBox("不能打开文件!","错误",0); return; }
else if(OK==-2) { MessageBox("文件格式错!","错误",0); return; }
else if(OK==-3) { MessageBox("图像太大,内存不够!","错误",0); return; }
char txt[20],Title[100];
strcpy(Title,Bmp.PathName); strcat(Title,"(" );
itoa(BmpInfo.biWidth,txt,10); strcat(Title,txt); strcat(Title," X " );
itoa(BmpInfo.biHeight,txt,10); strcat(Title,txt); strcat(Title,")" );
Doc->SetTitle(Title);
Doc->UpdateAllViews(NULL); //触发所有视图类的Update()函数
}
void CCapture1View::OnStartcap()
{
if(GetDlgItem(IDC_FREEZED)-IsWindowEnabled())
capDriverDisconnect(m_hCapWnd);//如果当前状态是捕捉画面,则断开连接
m_hCapWnd=capCreateCaptureWindow((LPTSTR)TEXT("摄像头图像捕捉程序"),
WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_EX_DLGMODALFRAME,
40,11,352,288,
m_hWnd,0); // 设置预示窗口
ASSERT(m_hCapWnd);
if(capDriverConnect(m_hCapWnd,0))
{
// 连接第0 号驱动器
m_bInit=TRUE;
//得到驱动器的性能
capDriverGetCaps(m_hCapWnd,sizeof(CAPDRIVERCAPS), &m_CapDrvCap);
if(m_CapDrvCap.fCaptureInitialized)
{// 如果初始化成功
capGetStatus(m_hCapWnd, &m_CapStatus,sizeof(m_CapStatus)); // 得到驱动器状态
capPreviewRate(m_hCapWnd,30); // 设置预示帧频
capPreview(m_hCapWnd,TRUE); // 设置预示方式
}
else{// 初始化未成功
AfxMessageBox("摄像头初始化失败!");
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
}
else{// 未能连接到驱动器
AfxMessageBox("与摄像头连接失败!");
// AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
}
void CCapture1View::OnFreezed()
{
// TODO: Add your control notification handler code here
capPreview(m_hCapWnd,FALSE);// 冻结画面
capGrabFrameNoStop(m_hCapWnd);
capEditCopy(m_hCapWnd);//重复捕捉画面
}
void CCapture1View::OnSave()
{
// TODO: Add your control notification handler code here
CTime t=CTime::GetCurrentTime();
CString str;
str=t.Format("%Y%B%d%H%M%S");
str+=".bmp";
char str1[255];
strcpy(str1,str);
capFileSaveDIB(m_hCapWnd,str1);
}
void CCapture1View::OnFormat()
{
capDlgVideoFormat(m_hCapWnd);//设置图像的格式
}
void CCapture1View::OnSource()
{
capDlgVideoSource(m_hCapWnd);//设置图像源
}
void CCapture1View::OnDisconnect()
{
capDriverDisconnect(m_hCapWnd);//断开连接
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -