温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何编写发一段开启摄像头的代码

发布时间:2021-10-14 15:46:22 来源:亿速云 阅读:126 作者:柒染 栏目:编程语言

本篇文章给大家分享的是有关如何编写发一段开启摄像头的代码,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。


//---------------------------------------------------------------------------

#pragma hdrstop

#include "Unit1.h"
#include "vfw.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HWND hWndC;
CAPDRIVERCAPS CapDrvCaps;
CAPSTATUS CapStatus;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
    char szDeviceName[80];
    char szDeviceVersion[80];

    for (int wIndex = 0; wIndex < 10; wIndex++)
    {
        if (capGetDriverDescription (wIndex, szDeviceName,
            sizeof (szDeviceName), szDeviceVersion,
            sizeof (szDeviceVersion)))
        {
            ComboBox1->Items->Add(szDeviceName);
        }
    }
    
    if(ComboBox1->Items->Count>0)
        ComboBox1->ItemIndex=0;
    else
    {
        ShowMessage("没有找到视频软件");
        Close();
    }
    Button1Click(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    hWndC = capCreateCaptureWindow (
        (LPSTR) "My Capture Window", // window name if pop-up
        WS_CHILD | WS_VISIBLE, // window style
        0, 0, 160, 120, // window position and dimensions
        (HWND)Panel2->Handle,//(HWND)Application->Handle,//(HWND) hwndParent,
        ComboBox1->ItemIndex+1);//(int) nID /* child ID */);

    //连接设备:
    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); // starts preview
        capDriverDisconnect (hWndC);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    // Video format dialog box.
    if (CapDrvCaps.fHasDlgVideoFormat)
    {
        capDlgVideoFormat(hWndC);
        // Are there new image dimensions?
        capGetStatus(hWndC, &CapStatus, sizeof (CAPSTATUS));
        SetWindowPos(hWndC, NULL,
            0,
            0,
            CapStatus.uiImageWidth,
            CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    // Video source dialog box.
    if (CapDrvCaps.fHasDlgVideoSource)
        capDlgVideoSource(hWndC);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
    //设置相关参数
    CAPTUREPARMS CaptureParms;
    float FramesPerSec = 30.0;

    capCaptureGetSetup(hWndC, &CaptureParms, sizeof(CAPTUREPARMS));

    CaptureParms.dwRequestMicroSecPerFrame = (DWORD) (1.0e6 /
        FramesPerSec);
    capCaptureSetSetup(hWndC, &CaptureParms, sizeof (CAPTUREPARMS));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
    //设置预览时的比例
    capPreviewScale(hWndC, 1);
    //设置预览时的帧频率
    capPreviewRate(hWndC,66);
    //如果要捕获视频流,则要使用函数指定不生成文件。否则将会自动生成AVI文件
    capCaptureSequenceNoFile(hWndC);
    //指定是否使用叠加模式,使用为1,否则为0
    capOverlay(hWndC, 1);
    
    //Video display dialog box.
    if (CapDrvCaps.fHasDlgVideoDisplay)
        capDlgVideoDisplay(hWndC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    if(SaveDialog2->Execute())
        capFileSaveDIB( hWndC, (SaveDialog2->FileName+".bmp").c_str() ); //截取当前帧
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{
    if(Button7->Caption == "开始捕获")
    {
        if(SaveDialog1->Execute())
        {
            // Set up the capture operation.
            capCaptureSequence(hWndC);
            // Capture.
            capFileSaveAs(hWndC, (SaveDialog1->FileName+".avi").c_str());//视频
        }
        Button7->Caption="停止捕获";
    }
    else
    {
        Button7->Caption="开始捕获";
        capCaptureStop(hWndC);
    }
}
//---------------------------------------------------------------------------

以上就是如何编写发一段开启摄像头的代码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI