【转】C++ builder 家用摄像头捕捉图像

作者: ldsea 分类: 程序生活 发布时间: 2009-09-17 19:16

我们来开发一个用家用摄像头捕捉图像的小程序。程序的主要代码如下,其中步骤说明将插入到代码当中。

步骤一:

在C++ builder中新建的Form上插入:2个button,一个panel,一个ComboBox和一个SaveDialg

步骤二:编写代码
[code]
//—————————————————————————-
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "vfw.h"         //双击Form后,在代码区先加入vfw头文件。      
//—————————————————————————-
#pragma resource "*.dfm"
#pragma package(smart_init)
TForm1 *Form1;      //加入自定义变量。
HWND hWndC;
CAPDRIVERCAPS CapDrvCaps;
CAPSTATUS CapStatus;
//—————————————————————————-
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
}
//—————————————————————————-
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Table1->Open();
}
//—————————————————————————-
void __fastcall TForm1::Button1Click(TObject *Sender)    // 加入启动camere的程序。
{
  char szDeviceName[80];
  char szDeviceVersion[80];
  for (int wIndex=0;wIndex<10;wIndex++)
  {
    if (capGetDriverDescription(wIndex,szDeviceName,sizeof(szDeviceName),szDeviceVersion,sizeof(szDeviceVersion)))
        {
            Camera->Items->Add(szDeviceName);
        }
    }
    if(Camera->Items->Count>0)
        Camera->ItemIndex=0;
    else
    {
        ShowMessage("There is no camera!");
        Close();
    }

//检查视频是否处于实时捕获状态
  if(CapStatus.fLiveWindow==1)
  //CapStatus.fLiveWindow=1表明当前初频已处于实时捕获状态,否则CapStatus.fLiveWindow=0
  {
    ShowMessage("You needn't do it again!");
    return;
  }
  hWndC=capCreateCaptureWindow((LPSTR)"My Capture Window",WS_CHILD | WS_VISIBLE,0,0,160,120,(HWND)Panel2->Handle,Camera->ItemIndex+1);
  //连接设备:
  capDriverConnect(hWndC,0);
  capPreviewRate(hWndC,50); // rate, in milliseconds
  capPreview(hWndC,TRUE); // starts preview
  //获取视频驱动相关性能
  capDriverGetCaps(hWndC,&CapDrvCaps,sizeof(CAPDRIVERCAPS));
  //获取捕获窗口状态
  capGetStatus(hWndC,&CapStatus,sizeof(CAPSTATUS));
  SetWindowPos(hWndC,NULL,0,0,CapStatus.uiImageWidth,CapStatus.uiImageHeight,SWP_NOZORDER | SWP_NOMOVE);
}
//—————————————————————————

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   if(hWndC)
  {
    capPreview(hWndC,FALSE);
    capDriverDisconnect(hWndC);
  }

}
//—————————————————————————

void __fastcall TForm1::Button2Click(TObject *Sender)    //加入图像保存程序。
{
if(SavePictureDialog1->Execute())
capFileSaveDIB(hWndC,(SavePictureDialog1->FileName+".bmp").c_str());
}
//—————————————————————————

[/code]
步骤三:

运行并测试。

一条评论
  • cloud

    2009年9月19日 23:27

    学习了

回复 cloud 取消回复

您的电子邮箱地址不会被公开。