⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 USB视频摄象头及拍照,代码.CBC6下编译通过。
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

#include "vfw.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
HWND hWndC,ghWndCap;
static int x1,y1,x2,y2;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{ //抓拍视频图片

  if (ghWndCap)
   {
       capFileSaveDIB(ghWndCap,"photo.bmp");
    }
  Image1->Picture ->LoadFromFile("photo.bmp");



    /*
    //将屏幕上100,100开始的一部分图抓到Form的Image1中
    HDC hdcSrc,hdcDst;
    hdcSrc=Image1->Canvas->Handle;
    hdcDst=Image2->Canvas->Handle;
    BitBlt(hdcDst,0,0,Image2->Width,Image2->Height,hdcSrc,100,60,SRCCOPY);
    Image2->Refresh();
    ReleaseDC(Handle,hdcSrc);
    ReleaseDC(Handle,hdcDst);

    Graphics::TBitmap *bitmap=new Graphics::TBitmap;
    bitmap->Assign(Image2->Picture);
    //bitmap->Assign(Image1->Picture);
    bitmap->PixelFormat=pf24bit;
    bitmap->SaveToFile("123.bmp");


    delete bitmap;

  Image3->Picture ->LoadFromFile("123.bmp");
 */
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{ //打开视频信号
   CAPDRIVERCAPS gCapDriverCaps;  //驱动
    CAPSTATUS gCapStatus;         //摄象机状态
    hWndC=Panel1->Handle ;
    //创建捕获窗,其中hWnd为主窗口句柄
    ghWndCap = capCreateCaptureWindow("Capture Window",WS_CHILD | WS_VISIBLE,
         0, 0, 300,240, hWndC,0);

    if(!capDriverConnect(ghWndCap,0))
    {
     BitBtn3->Enabled =true;  //启动视频
     ShowMessage("视频设备连接失败"); // 将捕获窗同驱动器连接
     return;
    }
    BitBtn4->Enabled =true;   //视频终止
    BitBtn3->Enabled =false;  //启动视频
    Panel1->Caption ="";

    capPreviewRate(ghWndCap, 1); // 设置Preview模式的显示速率
    capPreview(ghWndCap, TRUE); //启动Preview模式


}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  if (ghWndCap)
    {
    SendMessage(ghWndCap,WM_CAP_DRIVER_DISCONNECT,0,0);
    ghWndCap= 0;
    }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{  //终止视频
  if (ghWndCap)
    {
    SendMessage(ghWndCap,WM_CAP_DRIVER_DISCONNECT,0,0);
   ghWndCap= 0;
   BitBtn3->Enabled=true;
   BitBtn4->Enabled=false;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{ //显示
 

}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{//关闭
  if (ghWndCap)
    {
    SendMessage(ghWndCap,WM_CAP_DRIVER_DISCONNECT,0,0);
   ghWndCap= 0;
    }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
 AnsiString fname;
 if( this->SavePic->Execute( ) )
 {
   fname=this->SavePic->FileName ;

    if(1==this->SavePic->FilterIndex)
    {
     this->Image3->Picture->SaveToFile(fname+".bmp" ) ;
    }
    else
    {
     this->Image3->Picture->SaveToFile(fname+".jpg" ) ;
    }

 }
    
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{  //确定图象初始坐标
  // ShowMessage("x="+IntToStr(X)+" y="+IntToStr(Y));
  x1=X,y1=Y;
  oldx = X ; oldy = Y;
  Drawing = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{ //确定图象后坐标
    x2=X,y2=Y;
  //将屏幕上x1,y1开始到x2,y2的一部分图抓到Form的Image3中
    HDC hdcSrc,hdcDst;
    hdcSrc=Image1->Canvas->Handle;
    hdcDst=Image2->Canvas->Handle;
    BitBlt(hdcDst,x1,y1,x2-x1,y2-y1,hdcSrc,x1,y1,SRCCOPY);
    ReleaseDC(Handle,hdcSrc);
    ReleaseDC(Handle,hdcDst);
    Image2->Refresh();
    Graphics::TBitmap *bitmap=new Graphics::TBitmap;
    bitmap->Assign(Image2->Picture );
    bitmap->PixelFormat=pf24bit; 
    Image3->Picture->Assign(bitmap);
    delete bitmap;

    Drawing = false;

    

}
//---------------------------------------------------------------------------



void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
   if(Drawing)
   {
   Image1->Canvas->Brush->Style = bsClear;
   //Image1->Canvas->Pen->Color = clRed;
   Image1->Canvas->Pen->Style = psDot ;
   // clear old line
   //Image1->Canvas->CopyMode= cmPatInvert;
  // Image1->Canvas->Pen->Mode = pmNot	;
   Image1->Canvas->Pen->Mode = pmNot ;
   Image1->Canvas->Rectangle(x1,y1,oldx,oldy);
   //draw new line
   //Image1->Canvas->CopyMode = cmPatCopy ;
  // Image1->Canvas->Pen->Mode = pmNop;
   Image1->Canvas->Rectangle(x1,y1,X,Y);
   oldx = X;
   oldy = Y;
   }
}
//---------------------------------------------------------------------------



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -