VC中实现VFW的时时视频捕捉

Visual C++ Code
#include <AFX.h>
#include "StdAfx.h"
#include <VFW.H>
#include <stdio.h>
#pragma comment(lib,"VFW32.LIB")

HWND AVIhWnd = 0;
HWND hWnd = 0;
HDC hDC = 0;
HDC ScenehDC = 0;
DWORD AVIWidth = 640;
DWORD AVIHeight = 480;
DWORD DCColorBits = 32;
BYTE BmpByte[1920*480];

BITMAPINFO DCitmap;
HBITMAP SceneBitmap;
HGDIOBJ AboriginaalObject;
LPCSTR szClassName;
LPCSTR szWindowTitle;

long X;
long Y;
long OutWidth;
long InputWidth;
long cR;
long cG;
long cB;

static LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK FrameCallbackProc(HWND, LPVIDEOHDR);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    szClassName = "myClass";
    szWindowTitle = "MyWindow";

    WNDCLASS  wc;
    wc.style = CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc = MainWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_3DSHADOW);
    wc.lpszMenuName =  NULL;
    wc.lpszClassName = szClassName;
    RegisterClass(&wc);

    hWnd = CreateWindow(szClassName,
                        szWindowTitle,
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        AVIWidth,
                        AVIHeight,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);

    if (!hWnd){
        MessageBox(0,TEXT("创建窗口失败!/r/n"),"", MB_OK); 
        return (FALSE);
    }
    ShowWindow(hWnd, SW_SHOWNORMAL);
    UpdateWindow(hWnd);

    ScenehDC = CreateCompatibleDC(hDC);
    ZeroMemory(&DCitmap.bmiHeader, sizeof(BITMAPINFOHEADER));
    DCitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    DCitmap.bmiHeader.biBitCount = 24;
    DCitmap.bmiHeader.biWidth = AVIWidth;
    DCitmap.bmiHeader.biHeight = AVIHeight;
    DCitmap.bmiHeader.biPlanes = 1;
    DCitmap.bmiHeader.biClrUsed = 0;
    DCitmap.bmiHeader.biClrImportant = 0;
    DCitmap.bmiHeader.biSizeImage = AVIWidth * AVIHeight * (DCitmap.bmiHeader.biBitCount / 8);
    SceneBitmap = CreateDIBSection(ScenehDC, &DCitmap, DIB_RGB_COLORS, NULL ,0 , 0);

    AboriginaalObject = SelectObject(ScenehDC, (HGDIOBJ)SceneBitmap);

    OutWidth = AVIWidth * 3;
    InputWidth = AVIWidth * 3;


    int rd;
    BITMAPINFOHEADER bmpFormat;
    AVIhWnd = capCreateCaptureWindow((LPSTR)"视频追踪", 
                                    WS_CHILD  | WS_VISIBLE, 
                                    0, 0, AVIWidth, AVIHeight, 
                                    hWnd, 
                                    1);
    if (AVIhWnd==0)
    {
        MessageBox(hWnd, "创建AVI窗口失败","提示",64);
    }else{
        hDC = GetWindowDC(hWnd);

        capSetCallbackOnFrame(AVIhWnd,(FARPROC)FrameCallbackProc);
        rd = capDriverConnect(AVIhWnd,0);

        if(rd != 0){
            CAPDRIVERCAPS *Caps;
            capDriverGetCaps(AVIhWnd,sizeof(CAPDRIVERCAPS),&Caps);
            capPreviewRate(AVIhWnd, 30);
        }else{
            MessageBox(hWnd, "不能打开设备驱动。","提示",64);
        }
    }

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

    SelectObject(ScenehDC, AboriginaalObject);
    DeleteObject((HGDIOBJ)SceneBitmap);
    DeleteDC(ScenehDC);
    return 0;
}

LRESULT CALLBACK MainWndProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
    switch (Message) {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return (DefWindowProc(hWnd, Message, wParam, lParam));
    }
    return 0;
}

LRESULT CALLBACK FrameCallbackProc(HWND ghWnd,LPVIDEOHDR lpVData)
{
    for(Y=1; Y<=AVIHeight; Y++)
    {
        for(X=1; X<=AVIWidth; X++)
        {
            cB = lpVData->lpData[((AVIHeight-Y)*InputWidth)+(X*3)];
            cG = lpVData->lpData[((AVIHeight-Y)*InputWidth)+(X*3)+1];
            cR = lpVData->lpData[((AVIHeight-Y)*InputWidth)+(X*3)+2];
            BmpByte[((Y-1)*OutWidth)+(X*3)]=cB;
            BmpByte[((Y-1)*OutWidth)+(X*3)+1]=cG;
            BmpByte[((Y-1)*OutWidth)+(X*3)+2]=cR;
        }
    }
    SetBitmapBits(SceneBitmap,sizeof(BmpByte),BmpByte);
    BitBlt(hDC, 0, 0, AVIWidth, AVIHeight, ScenehDC, 0, 0, SRCCOPY);
    return 0;
}

你可能感兴趣的:(null,application,avi,colors)