win32用VFW预览和录像的代码

转载 2013年12月03日 15:37:05
#include <windows.h>
#include <vfw.h>
#include <commdlg.h>
#include "CapPicture.h"

LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
LRESULT WINAPI SelCapDrvProc( HWND, UINT, WPARAM, LPARAM );
int EnumCapDrv();
VOID APIENTRY HandlePopupMenu(HWND, POINT);
DWORD WINAPI videoThreadProc(LPVOID lParam);


HANDLE ghInstance;
HWND hwndMain;
HWND hwndVideo;
HWND hwndSelCapDrvDlg;
HWND hwndSelCapDrvDlg_LBox;
HWND hwndExit;
HWND hwndMin;
HWND hwndHelp;
HWND hwndRecord;
HANDLE hVideoThread;
HRGN hRegion1;
CAPDRIVERCAPS CapDrvCaps;
bool isRecordFileOpen = false;
char recordFile[260];
bool isPicFileOpen = false;
char pictureFile[260];
bool isRecording = false;
bool threadEnd = false;

int PASCAL WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,

    LPSTR lpszCmdLine,
    int nCmdShow )
{
   WNDCLASS wc;
   MSG msg;

   if( !hPrevInstance )
   {
      wc.lpszClassName = "GenericAppClass";
      wc.lpfnWndProc = MainWndProc;
      wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
      wc.hCursor = LoadCursor( NULL, IDC_ARROW );
      wc.hbrBackground = CreateSolidBrush (RGB(196, 200, 204));
      wc.lpszMenuName = "GenericAppMenu";

      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;

      RegisterClass( &wc );
   }

   ghInstance = hInstance;

   hwndMain = CreateWindow( "GenericAppClass",
      "Super Video",
      WS_POPUP,
      100,
      100,
      500,
      500,
      NULL,
      NULL,
      hInstance,
      NULL
   );

   ShowWindow( hwndMain, nCmdShow );
   SetWindowRgn(hwndMain,hRegion1,1);

   while( GetMessage( &msg, NULL, 0, 0 ) ) {
      TranslateMessage( &msg );

      DispatchMessage( &msg );
   }

   return msg.wParam;
}

LRESULT CALLBACK MainWndProc( HWND hwndMain, UINT msg, WPARAM wParam,
   LPARAM lParam )
{
   HDC hDC = GetDC(hwndMain);
   RECT rc;           
   POINT pt;  

   switch( msg ) {
      case WM_LBUTTONDBLCLK:
              SetFocus(hwndMain);
              break;
      case WM_RBUTTONDOWN:
            GetClientRect(hwndMain, (LPRECT) &rc);
            pt.x = LOWORD(lParam);
            pt.y = HIWORD(lParam);
            if (PtInRect((LPRECT) &rc, pt))
                HandlePopupMenu(hwndMain, pt);
            break;
      case WM_PAINT:
         FrameRgn(hDC,hRegion1,CreateSolidBrush(RGB(0,0,0)),2,2);
           BringWindowToTop(hwndSelCapDrvDlg);
         return( DefWindowProc( hwndMain, msg, wParam, lParam ));
      case WM_COMMAND:
           CAPSTATUS CapStatus;
           switch( wParam ) {
         
             case SOURCE:
                  if(CapDrvCaps.fHasDlgVideoSource)
                          capDlgVideoSource(hwndVideo);
                  break;
               case FORMAT:
                  if(CapDrvCaps.fHasDlgVideoFormat)
                  {        
                          capDlgVideoFormat(hwndMain);
                          capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
                          SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
                                      CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
                  }
                  break;
               case DISPLAY:
                   if (CapDrvCaps.fHasDlgVideoDisplay)
                       capDlgVideoDisplay(hwndVideo);
                   break;
            case EXIT:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_CLOSE, 0);
               break;
            case MINIMIZE:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
               break;
            case HELP:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_CONTEXTHELP, 0);
               break;
            case RECORDVIDEO:
            if(HIWORD(wParam) == BN_CLICKED && (HWND) lParam == hwndRecord)
            {
               if (!isRecordFileOpen)
               {
                       OPENFILENAME ofn;
                       ZeroMemory(&ofn, sizeof(OPENFILENAME));
                    ofn.lStructSize = sizeof(OPENFILENAME);
                    ofn.hwndOwner = hwndMain;
                    ofn.lpstrFile = recordFile;
                    ofn.nMaxFile = sizeof(recordFile);
                    ofn.lpstrFilter = "Video\0*.avi";
                    ofn.nFilterIndex = 1;
                    ofn.lpstrFileTitle = NULL;
                    ofn.nMaxFileTitle = 0;
                    ofn.lpstrInitialDir = NULL;
                    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
                    if(GetSaveFileName(&ofn) == TRUE)
                    {
                        strcpy(recordFile, ofn.lpstrFile);
                        strcat(recordFile, ".avi");
                        isRecordFileOpen = true;
                        DWORD id;
                        SECURITY_ATTRIBUTES sa;
                        sa.nLength = sizeof(SECURITY_ATTRIBUTES);
                        sa.lpSecurityDescriptor = NULL;
                        sa.bInheritHandle = TRUE;
                        hVideoThread = CreateThread(&sa, (ULONG)0,
                                            videoThreadProc, (LPVOID)(ULONG)0, (ULONG)0, &id);        
                        if(hVideoThread == NULL)
                            MessageBox(NULL, "Creation of Record Thread failed!", "Thread", MB_OK | MB_ICONEXCLAMATION);
                        
                        break;
                    }
                }

                if (isRecordFileOpen)
                {
                    if(isRecording)
                    {
                        threadEnd = true;
                        capFileSaveAs(hwndVideo, recordFile);
                        SetWindowText(hwndRecord, "Record Video");
                        isRecording = false;
                        break;
                    }
                    
                    if(!isRecording )
                    {
                        int a = 0;
                        
                        MessageBox(hwndMain, "Do you want to write over the open file?",
                                    "File warning", MB_YESNO | MB_ICONWARNING);
                        if (a != IDYES)
                        {
                            isRecordFileOpen = false;
                            SendMessage(hwndMain, WM_COMMAND, MAKEWPARAM(RECORDVIDEO, BN_CLICKED), (LPARAM) hwndRecord);
                        }
                        
                        if (a == IDYES)
                        {
                            capCaptureSequence(hwndVideo);
                            isRecording = true;
                        }
                        
                    break;
                    }
                }
            }    
             break;
         }
      break;
     case WM_CREATE:
      
          RECT helpRect, minRect, exitRect;
          HRGN helpRgn, minRgn, exitRgn;
        hRegion1 = CreateRoundRectRgn(100,100,500, 500,20,20);   
          hwndVideo = capCreateCaptureWindow(
                          (LPSTR) "My Capture Window",
                          WS_CHILD | WS_VISIBLE,
                          150, 150, 300, 280,
                          (HWND) hwndMain,
                          (int) 1);
        hwndExit = CreateWindow (
                "button",
                "x",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                470, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) EXIT,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);
        hwndMin = CreateWindow (
                "button",
                "-",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                450, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) MINIMIZE,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);          
        hwndHelp = CreateWindow (
                "button",
                "?",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                430, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) HELP,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);
        hwndRecord = CreateWindow (
                "button",
                "录像",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                250, 464, 90, 28,
                hwndMain,
                (HMENU) RECORDVIDEO,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);                
                
          GetClientRect(hwndHelp, &helpRect);
          GetClientRect(hwndMin, &minRect);
          GetClientRect(hwndExit, &exitRect);
                              
          helpRgn = CreateEllipticRgnIndirect(&helpRect);
        minRgn = CreateEllipticRgnIndirect(&minRect);
        exitRgn = CreateEllipticRgnIndirect(&exitRect);
                        
        SetWindowRgn(hwndExit,exitRgn,1);
        SetWindowRgn(hwndMin,minRgn,1);                                        
        SetWindowRgn(hwndHelp,helpRgn,1);                
    
        hwndSelCapDrvDlg = CreateDialog((HINSTANCE)ghInstance,
                                        MAKEINTRESOURCE( SELCAPDRVDLG  ),
                                        0, (DLGPROC)SelCapDrvProc);
        hwndSelCapDrvDlg_LBox = GetDlgItem(hwndSelCapDrvDlg,
                                              SELCAPDRVDLG_LSTBOX);
        EnumCapDrv();
      break;
      case WM_DESTROY:
           capPreview(hwndVideo, FALSE);
           capDriverDisconnect(hwndVideo);
         PostQuitMessage( 0 );
         break;
      default:
         return( DefWindowProc( hwndMain, msg, wParam, lParam ));

   }

   return 0;
}

LRESULT CALLBACK SelCapDrvProc( HWND hWnd, UINT msg,
                                WPARAM wParam, LPARAM lParam )
{

    switch(msg)
    {
        case WM_INITDIALOG:
            return TRUE;
        case WM_COMMAND:
            switch ( wParam )
            {
                case SELCAPDRVDLG_BUTTON:
                    int sel = 0;
                    SendMessage( hwndSelCapDrvDlg_LBox, LB_GETSELITEMS, 1, sel);
                    SendMessage( hwndVideo, WM_CAP_DRIVER_CONNECT, sel, 0L);
                    SendMessage( hwndSelCapDrvDlg, WM_CLOSE, 0, 0);
                    SendMessage( hwndVideo, WM_CAP_DRIVER_GET_CAPS,
                                 sizeof(CAPDRIVERCAPS), (LONG) (LPVOID) &CapDrvCaps);
                    capPreviewRate( hwndVideo, 66 );
                    capPreview( hwndVideo, TRUE );
            }
            return TRUE;
        case WM_CLOSE:
            DestroyWindow(hwndSelCapDrvDlg);
            return TRUE;
    }

    return( 0L );
}

int EnumCapDrv()
{
    char szDeviceName[80];
    char szDeviceVersion[80];
    char item[161];
    int i;

    for (i=0; i<10; i++)
    {
        if ( capGetDriverDescription(i, szDeviceName, sizeof(szDeviceName),
                                    szDeviceVersion, sizeof(szDeviceVersion)) )
        {
                strcpy(item, szDeviceName);
                strcat(item, " ");
                strcat(item, szDeviceVersion);
                SendMessage(hwndSelCapDrvDlg_LBox, LB_ADDSTRING, 0,
                           (LPARAM) item);
                SendMessage(hwndSelCapDrvDlg_LBox, LB_SETITEMDATA, i, (LPARAM) i);
        }
    }
    
    return 0;
}

VOID APIENTRY HandlePopupMenu(HWND hwnd, POINT pt)
{
    HMENU hmenu;    
    HMENU hmenuTrackPopup;
    hmenu = LoadMenu((HINSTANCE)ghInstance, "PopupMenu");
    if (hmenu == NULL)
        return;
    hmenuTrackPopup = GetSubMenu(hmenu, 0);
    ClientToScreen(hwnd, (LPPOINT) &pt);
    TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_LEFTBUTTON,
        pt.x, pt.y, 0, hwnd, NULL);
    DestroyMenu(hmenu);
}

DWORD WINAPI videoThreadProc(LPVOID lParam)
{
    SetWindowText(hwndRecord, "停止");
    capCaptureSequence(hwndVideo);
    isRecording = true;
    while (!threadEnd)
        ;
    MessageBox(NULL, "Leaving Thread", "thread", NULL);
    return 0;
}



原帖出自点击打开链接

Win7下VFW控制摄像头

开发环境: Win7 + VS2008 + VFW 一. 最基础的调用 1.1  初始化 BOOL CMyDlg::Init() { // HWND m_hWndVideo; // 创建捕捉窗口...
  • cay22
  • cay22
  • 2015年04月25日 15:35
  • 2068

VFW视频采集方案

2.2 VFW视频采集方案 VFW是Microsoft于1992年推出的数字视频软件包,它不依赖于专用的硬件设备,提供了通用的数字视频开发方案。VFW主要由AVICap.dll、MSVideo.dl...
  • Andy20081251
  • Andy20081251
  • 2014年03月05日 14:31
  • 967

windows下视频捕捉VFW和DirectShow

windows下视频捕捉VFW和DirectShow
  • chenlycly
  • chenlycly
  • 2016年09月26日 16:23
  • 1310

基于vfw的摄像头视频浏览和捕获的程序

使用基于VFW的方式进行摄像头视频浏览和捕获,优点是编码简单,缺点是功能受限 方法:新建一个基于对话框的工程,引用头文件 ,导入 vfw32.lib 库,库文件可以在vc存在路径中找到;如:D:/Pr...
  • vinilarain
  • vinilarain
  • 2006年05月11日 10:18
  • 4154

Android 实现预览,录像,拍照

package com.demo;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutput...
  • yiranhaiziqi
  • yiranhaiziqi
  • 2016年02月18日 16:03
  • 460

同时获取Camera预览和录像视频流

介绍除了默认的Preview数据流之外,增加录像数据流。要求同时获取,并能够支持不同的分辨率。 1. 描述高通平台支持同时获取Preview和录像,要获取双路视频流的话,除了Android系统默认支持...
  • laisse
  • laisse
  • 2015年11月13日 09:53
  • 2205

趁热打铁之---VS2010基于VFW的视频控件编写,实现预览、拍照功能

一、VFW简介 VFW()
  • wu_tongyu
  • wu_tongyu
  • 2014年09月28日 15:50
  • 1646

VFW vs WDM

VFW技术受到的很多批评,因它捕获的数据保存到磁盘上会占用大量磁盘空间,每秒数据量超过20M,(本人试验640*480窗口1s大约需要10M),同时需要大量的客户端支撑软件,VFW体系架构上的不足在视...
  • hamsky
  • hamsky
  • 2006年12月16日 11:24
  • 3307

windows下使用vfw方式生成AVI视频的实现

生成视频文件的方式很多,你可以使用libx264这个开源的264编解码库来编码视频,生成视频文件;但是这样很麻烦,还需要另外找一个包装器(比如mp4)来包装这个视频流,否则播放器一般无法识别和播放。 ...
  • shenyi0106
  • shenyi0106
  • 2015年08月28日 16:14
  • 1794

视频捕捉全教程(MFC+vfw)

视频捕捉全教程(vc+vfw) 目 录 一. 视频捕获快速入门 二.基本的捕获设置            1.设置捕获速度:            2.设置终止捕...
  • u010002704
  • u010002704
  • 2014年09月04日 23:03
  • 902
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:win32用VFW预览和录像的代码
举报原因:
原因补充:

(最多只允许输入30个字)