/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // Intel License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/// Camera.cpp: implementation of the CCamera class. // //////////////////////////////////////////////////////////////////////#include "stdafx.h" #include "Camera.h" #include <assert.h>#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////CCamera::CCamera() { m_capWnd = 0; m_isRunning = false; m_hic = 0; m_fourcc = -1; }CCamera::~CCamera() { Uninitialize(); } void CCamera::UpdateParent( bool whole ) { HWND parent = GetParent( m_capWnd ); RECT r = { 0, 0, 0, 0 }; IplImage* img = m_frame.GetImage(); if( img ) { r.right = img->width; r.bottom = img->height; }
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // Intel License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/// Camera.h: interface for the CCamera class. // //////////////////////////////////////////////////////////////////////#if !defined(AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_) #define AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000#include "highgui.h" #include <vfw.h>class CCamera { public: CImage& GetFrame() { return m_frame; } CCamera(); virtual ~CCamera();
bool IsInitialized() { return m_capWnd != 0; }; bool IsRunning() { return m_isRunning; }; virtual void OnFrame(); bool Initialize( int width, int height, int format, HWND parent, int camIndex=-1 ); void Uninitialize(); void ClearRect(); void Start(); void Stop(); void VideoFormatDlg(); void VideoSourceDlg(); void UpdateParent( bool whole ); /* internal use only */ void PutCompressedFrame( VIDEOHDR* hdr, BITMAPINFO* vfmt );protected: CImage m_frame; HWND m_capWnd; bool m_isRunning; CAPDRIVERCAPS m_caps; DWORD m_fourcc; HIC m_hic; };#endif // !defined(AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_)
我这里有例子,我邮箱[email protected],可以给我发email!
http://www.naughter.com/download/vfwwnd.zip二、用VC ++6.0 实现视频捕捉的编程实例 笔者前不久刚刚用VC ++6.0 完成了一套光学自动报靶系统的开发,该系统的前端用到了视频捕捉。下面结合这部分实例,说明对视频捕捉编程的具体实现方法。
1、用Visual C ++的AppWizard 生成一个基于对话框的应用程序,并在SrdAfx.h 中加入以下内容:
#include < vfw.h >
#pragma comment(lib,"vfw32.lib")
2、修改对话框资源,添加静态控制IDC_VIDEO 和按钮“捕捉”、“停止”、“设置格式”、“设置图像源”、“设置压缩”、“单帧捕获”、“定帧”、“退出”。
3、在对话框的头文件中加入以下说明。
HWND m_hCapWnd;// 预示窗口
BOOL m_bInit;// 捕捉器初始化
CAPDRIVERCAPS m_CapDrvCap;// CAP
DRIVERCAPS 结构,定义驱动器性能
CAPSTATUS m_CapStatus;// CAPSTATUS 结构,
定义捕捉窗口当前状态
CAPTUREPARMS m_Parms;
// CAPTURE PARMS 结构,定义捕捉参数
CString m_CapFileName;// 捕捉文件名称
4、在对话框类的OnInitDialog() 函数中加入捕捉器初始化以及连接驱动器。
m_bInit=FALSE;
CWnd *pWnd=AfxGetMainWnd() ->GetDlgItem
(IDC_VIDEO); // 得到预示窗口指针
CRect rect;
pWnd ->GetWindowRect( &rect); // 得到窗口大小
m_hCapWnd=capCreateCaptureWindow
((LPTSTR)TEXT("视频捕捉测试程序"),
WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|
WS_EX_DLGMODALFRAME,0,0,rect.Width(),rect.Width(),
pWnd ->GetSafeHwnd(),0); // 设置预示窗口
ASSERT(m_hCapWnd);
if(capDriverConnect(m_hCapWnd,0)){
// 连接第0 号驱动器
m_bInit=TRUE;
// 得到驱动器的性能
capDriverGetCaps(m_hCapWnd,sizeof
(CAPDRIVE RCAPS), &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);
}
m_CapFileName="c:\\Capture.avi";// 设置捕获文件
capFileSetCaptureFile(m_hCapWnd,m_CapFile
Name.GetBuffer(255));
5、在对话框类中加入响应“设置格式”消息的函数OnFormat()。
capDlgVideoFormat(m_hCapWnd);
// 设置格式对话框
6、在对话框类中加入响应“设置图像源”消息的函数OnSource()。
capDlgVideoSource(m_hCapWnd);
// 设置图像源对话框
7、在对话框类中加入响应“设置压缩”消息的函数OnCompress()。
capDlgVideoCompression(m_hCapWnd);
// 设置压缩对话框
8、在对话框类中加入响应“捕捉”消息的函数OnCapture()。
capCaptureGetSetup(m_hCapWnd,
&m_Parms,sizeof(m_Parms));// 得到设置参数
if(capCaptureSetSetup(m_hCapWnd,
&m_Parms,sizeof(m_Parms))==TRUE){
BOOL suc=TRUE;
suc=capCaptureSequence(m_hCapWnd); // 捕捉到文件
return suc};
else
return FALSE;
9、在对话框类中加入响应“定帧”消息的函数OnFreezed()。
capPreview(m_hCapWnd,FALSE);// 定帧
10、在对话框类中加入响应“单帧捕获”消息的函数OnImage()。
capGrabFrameNoStop(m_hCapWnd);
// 截获当前图像
capEditCopy(m_hCapWnd);
// 将图像拷贝到剪贴板
11、在对话框类中加入响应“停止”消息的函数OnStop()。
capCaptureStop(m_hCapWnd);// 停止捕捉
12、退出前断开捕捉器与驱动器的连接。
capDriverDisconnect(m_hCapWnd);
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/// Camera.cpp: implementation of the CCamera class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "Camera.h"
#include <assert.h>#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CCamera::CCamera()
{
m_capWnd = 0;
m_isRunning = false;
m_hic = 0;
m_fourcc = -1;
}CCamera::~CCamera()
{
Uninitialize();
}
void CCamera::UpdateParent( bool whole )
{
HWND parent = GetParent( m_capWnd );
RECT r = { 0, 0, 0, 0 };
IplImage* img = m_frame.GetImage(); if( img )
{
r.right = img->width;
r.bottom = img->height;
}
InvalidateRect( parent, whole ? 0 : &r, whole );
UpdateWindow( parent );
}
// Frame callback
LRESULT PASCAL FrameCallbackProc( HWND hWnd, VIDEOHDR* hdr )
{
BITMAPINFO vfmt;
CCamera* camera = (CCamera*)capGetUserData(hWnd);
int sz; if (!hWnd) return FALSE;
if (camera && !camera->IsRunning() ) return FALSE;
sz = capGetVideoFormat( hWnd, &vfmt, sizeof(vfmt));
if( hdr && hdr->lpData && sz != 0 && camera )
camera->PutCompressedFrame( hdr, &vfmt ); return (LRESULT)TRUE;
}
void CCamera::OnFrame()
{
UpdateParent(0);
}void CCamera::PutCompressedFrame( VIDEOHDR* hdr, BITMAPINFO* vfmt )
{
long code = ICERR_OK;
char* frame_data = (char*)hdr->lpData;
if( vfmt->bmiHeader.biCompression != BI_RGB ||
vfmt->bmiHeader.biBitCount != 24 )
{
BITMAPINFOHEADER& vfmt0 = vfmt->bmiHeader;
BITMAPINFOHEADER vfmt1;
code = ICERR_ERROR;
memset( &vfmt1, 0, sizeof(vfmt1));
vfmt1.biSize = sizeof(vfmt1);
vfmt1.biWidth = vfmt0.biWidth;
vfmt1.biHeight = vfmt0.biHeight;
vfmt1.biBitCount = 24;
vfmt1.biCompression = BI_RGB;
vfmt1.biPlanes = 1; if( m_hic == 0 || m_fourcc != vfmt0.biCompression || m_frame.GetImage() == 0 ||
vfmt0.biWidth != m_frame.GetImage()->width ||
vfmt0.biHeight != m_frame.GetImage()->height )
{
if( m_hic )
{
ICDecompressEnd( m_hic );
ICClose( m_hic );
}
m_hic = ICOpen( MAKEFOURCC('V','I','D','C'),
vfmt0.biCompression, ICMODE_DECOMPRESS ); if( m_hic && ICDecompressBegin( m_hic, &vfmt0, &vfmt1 ) == ICERR_OK )
{
m_frame.Create( vfmt0.biWidth, vfmt0.biHeight, 24 );
m_frame.GetImage()->origin = IPL_ORIGIN_BL; code = ICDecompress( m_hic, 0, &vfmt0, hdr->lpData,
&vfmt1, m_frame.GetImage()->imageData );
}
}
}
else
{
m_frame.Create( vfmt->bmiHeader.biWidth, vfmt->bmiHeader.biHeight, 24 );
memcpy( m_frame.GetImage()->imageData, hdr->lpData, m_frame.GetImage()->imageSize );
} /*m_frame.GetImage()->origin = IPL_ORIGIN_BL;*/
cvFlip( m_frame.GetImage(), m_frame.GetImage(), 0 ); OnFrame();
}
// Initialize camera input
bool CCamera::Initialize( int width, int height, int format, HWND parent, int wIndex )
{
char szDeviceName[80];
char szDeviceVersion[80];
HWND hWndC = 0;
if( (unsigned)wIndex < 10 )
goto init_camera; for( wIndex = 0; wIndex < 10; wIndex++ )
{
init_camera:
if( capGetDriverDescription( wIndex, szDeviceName,
sizeof (szDeviceName), szDeviceVersion,
sizeof (szDeviceVersion)))
{
hWndC = capCreateCaptureWindow ( "My Own Capture Window",
WS_CHILD | WS_VISIBLE , 0, 0, 160, 120, parent, 0); if( capDriverConnect (hWndC, wIndex))
{
/*BITMAPINFO bmi;
//capPreviewRate(hWndC, 66); // rate, in milliseconds
memset( &bmi.bmiHeader, 0, sizeof(bmi.bmiHeader));
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biBitCount = format == 0 ? 24 : 12;
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = format;
bmi.bmiHeader.biSizeImage = ((bmi.bmiHeader.biWidth*
bmi.bmiHeader.biBitCount/8 + 3)&-4)*
bmi.bmiHeader.biHeight;
if( format == -1 || capSetVideoFormat( hWndC, &bmi, sizeof(bmi)-4)) break;
capDriverDisconnect( hWndC );*/
break;
}
DestroyWindow( hWndC );
hWndC = 0;
}
}
if( hWndC )
{
m_capWnd = hWndC;
memset( &m_caps, 0, sizeof(m_caps));
capDriverGetCaps( hWndC, &m_caps, sizeof(m_caps));
capSetUserData( hWndC, (long)this );
capSetCallbackOnFrame( m_capWnd, FrameCallbackProc );
::MoveWindow( m_capWnd, 0, 0, 1, 1, TRUE );
CAPTUREPARMS p;
capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
p.dwRequestMicroSecPerFrame = 1;
capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
capPreviewScale(hWndC,FALSE);
capPreviewRate(hWndC,1);
}
return m_capWnd != 0;
}
// Uninitialize camera input
void CCamera::Uninitialize()
{
Stop();
capSetCallbackOnFrame( m_capWnd, NULL );
capDriverDisconnect( m_capWnd );
DestroyWindow( m_capWnd ); if( m_hic )
{
ICDecompressEnd( m_hic );
ICClose( m_hic );
m_hic = 0;
}
}// Start capture
void CCamera::Start()
{
if( m_capWnd )
{
m_isRunning = true;
}
}// Stop capture
void CCamera::Stop()
{
if( m_capWnd )
{
m_isRunning = false;
}
}
void CCamera::VideoFormatDlg()
{
if( m_capWnd && m_caps.fHasDlgVideoFormat )
{
capDlgVideoFormat( m_capWnd );
}
}
void CCamera::VideoSourceDlg()
{
if( m_capWnd && m_caps.fHasDlgVideoSource )
{
capDlgVideoSource( m_capWnd );
}
}
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/// Camera.h: interface for the CCamera class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_)
#define AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#include "highgui.h"
#include <vfw.h>class CCamera
{
public:
CImage& GetFrame() { return m_frame; }
CCamera();
virtual ~CCamera();
bool IsInitialized() { return m_capWnd != 0; };
bool IsRunning() { return m_isRunning; }; virtual void OnFrame();
bool Initialize( int width, int height, int format, HWND parent, int camIndex=-1 );
void Uninitialize(); void ClearRect();
void Start();
void Stop();
void VideoFormatDlg();
void VideoSourceDlg();
void UpdateParent( bool whole ); /* internal use only */
void PutCompressedFrame( VIDEOHDR* hdr, BITMAPINFO* vfmt );protected:
CImage m_frame;
HWND m_capWnd;
bool m_isRunning;
CAPDRIVERCAPS m_caps;
DWORD m_fourcc;
HIC m_hic;
};#endif // !defined(AFX_CAMERA_H__A499AE7A_13AB_4297_AE1E_25B23734F6FA__INCLUDED_)