真如客 發表於 2020-12-18 23:51:00

Delphi 控制摄像头操作

<p>DELPHI控制摄像头操作可以使用TVideoCap控件,或直接使用MS的AVICAP32.DLL就可轻松的实现对摄像头编程。</p>
<p>首先常量定义和函数定义:</p>
<p>implementation<br>const WM_CAP_START = WM_USER;<br>const WM_CAP_STOP = WM_CAP_START + 68;<br>const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;<br>const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;<br>const WM_CAP_SAVEDIB = WM_CAP_START + 25;<br>const WM_CAP_GRAB_FRAME = WM_CAP_START + 60;<br>const WM_CAP_SEQUENCE = WM_CAP_START + 62;<br>const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;<br>const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+&nbsp; 63 ;<br>const WM_CAP_SET_OVERLAY =WM_CAP_START+&nbsp; 51 ;<br>const WM_CAP_SET_PREVIEW =WM_CAP_START+&nbsp; 50 ;<br>const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;<br>const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;<br>const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;<br>const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;<br>const WM_CAP_SET_SCALE=WM_CAP_START+&nbsp; 53 ;<br>const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+&nbsp; 52 ;<br>function capCreateCaptureWindowA(lpszWindowName : PCHAR;<br>dwStyle : longint;<br>x : integer;<br>y : integer;<br>nWidth : integer;<br>nHeight : integer;<br>ParentWin : HWND;<br>nId : integer): HWND;<br>STDCALL EXTERNAL 'AVICAP32.DLL';<br>{$R *.dfm}</p>
<p>打开Delphi,添加Panel1到Form1上,定义一个全局变量,var hWndC : THandle;&nbsp;添加button1 ,caption为激活摄像头:</p>
<p>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>hWndC := capCreateCaptureWindowA('My Own Capture Window',<br>WS_CHILD or WS_VISIBLE ,<br>Panel1.Left,<br>Panel1.Top,<br>Panel1.Width,<br>Panel1.Height,<br>Form1.Handle,<br>0);<br>if hWndC &lt;&gt; 0 then<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);<br>SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);<br>//SendMessage(hWndC, WM_CAP_SEQUENCE_NOFILE, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);<br>end;</p>
<p>添加button2 ,caption为关闭摄像头:</p>
<p>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);<br>hWndC := 0;<br>end;<br>end;&nbsp;</p>
<p>添加button3 ,caption为保存为BMP图像:</p>
<p>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC,WM_CAP_SAVEDIB,0,longint(pchar('c:\\test.bmp')));<br>end;<br>end;</p>
<p>添加button4 ,caption为开始录像:</p>
<p>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then<br>begin<br>SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0, Longint(pchar('c:\\test.avi')));<br>SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);<br>end;<br>end;&nbsp;</p>
<p>添加button5 ,caption为停止录像:</p>
<p>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC, WM_CAP_STOP, 0, 0);<br>end;<br>end;</p>
<p>添加button6,caption为退出:</p>
<p>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>close;<br>end;&nbsp;</p>
<p>可以添加MediaPlayer和opendialog控件</p>
<p>添加button7,caption为加载视频:</p>
<p>procedure TForm1.Button7Click(Sender: TObject);<br>begin<br>openDialog1.DefaultExt := 'avi';<br>openDialog1.Filter := 'avi files (*.avi)|*.avi';</p>
<p>if OpenDialog1.Execute&nbsp; then<br>begin<br>&nbsp;if (MediaPlayer1.DeviceID&lt;&gt;0) then<br>&nbsp; begin<br>&nbsp;&nbsp;&nbsp; if (MediaPlayer1.Mode=mpplaying) then MediaPlayer1.Stop;<br>&nbsp; end;</p>
<p>&nbsp;MediaPlayer1.FileName:=openDialog1.FileName;<br>&nbsp;//MediaPlayer1.DisplayRect.Top:=panel2.Top;<br>&nbsp;//MediaPlayer1.DisplayRect.Left:=panel2.left;<br>&nbsp;//MediaPlayer1.DisplayRect.Right:=panel2.Height;<br>&nbsp;//MediaPlayer1.DeviceType&nbsp;&nbsp; :=dtAutoSelect;<br>&nbsp;Mediaplayer1.Open;<br>&nbsp;MediaPlayer1.Play;<br>&nbsp;end;<br>end;</p>
<p>如果电脑没有摄像头,panel就会黑黑的,可以尝试安装SoftCam虚拟摄像头。</p>
<p>关于摄像头编程,大家也可以看看这组VCL组件:DSPack,DSPack是一套使用微软Direct Show和DirectX技术的类和组件,设计工作于DirectX 9,支持系统Win9X, ME, 2000和Windows XP。</p>
<p>视屏聊天 按数据压缩传输给对方,显示出来,不是那么简单。</p>
<div>
<table border="0"><colgroup><col></colgroup>
<tbody valign="top">
<tr>
<td valign="middle">
<p>unit Unit1;</p>
<p>interface</p>
<p>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, ExtCtrls, MPlayer;</p>
<p>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp;&nbsp;&nbsp; Button1: TButton;<br>&nbsp;&nbsp;&nbsp; Panel1: TPanel;<br>&nbsp;&nbsp;&nbsp; Button2: TButton;<br>&nbsp;&nbsp;&nbsp; Button3: TButton;<br>&nbsp;&nbsp;&nbsp; Button4: TButton;<br>&nbsp;&nbsp;&nbsp; Button5: TButton;<br>&nbsp;&nbsp;&nbsp; Button6: TButton;<br>&nbsp;&nbsp;&nbsp; Button7: TButton;<br>&nbsp;&nbsp;&nbsp; OpenDialog1: TOpenDialog;<br>&nbsp;&nbsp;&nbsp; MediaPlayer1: TMediaPlayer;<br>&nbsp;&nbsp;&nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button5Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button7Click(Sender: TObject);<br>&nbsp;&nbsp;&nbsp; procedure Button6Click(Sender: TObject);<br>&nbsp; private</p>
<p>&nbsp;&nbsp;&nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp;&nbsp;&nbsp; { Public declarations }<br>&nbsp; end;</p>
<p>var<br>&nbsp; Form1: TForm1;<br>&nbsp;&nbsp; hWndC : THandle;</p>
<p>implementation<br>const WM_CAP_START = WM_USER;<br>const WM_CAP_STOP = WM_CAP_START + 68;<br>const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;<br>const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;<br>const WM_CAP_SAVEDIB = WM_CAP_START + 25;<br>const WM_CAP_GRAB_FRAME = WM_CAP_START + 60;<br>const WM_CAP_SEQUENCE = WM_CAP_START + 62;<br>const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;<br>const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+&nbsp; 63 ;<br>const WM_CAP_SET_OVERLAY =WM_CAP_START+&nbsp; 51 ;<br>const WM_CAP_SET_PREVIEW =WM_CAP_START+&nbsp; 50 ;<br>const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;<br>const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;<br>const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;<br>const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;<br>const WM_CAP_SET_SCALE=WM_CAP_START+&nbsp; 53 ;<br>const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+&nbsp; 52 ;<br>function capCreateCaptureWindowA(lpszWindowName : PCHAR;<br>dwStyle : longint;<br>x : integer;<br>y : integer;<br>nWidth : integer;<br>nHeight : integer;<br>ParentWin : HWND;<br>nId : integer): HWND;<br>STDCALL EXTERNAL 'AVICAP32.DLL';<br>{$R *.dfm}</p>
<p>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>hWndC := capCreateCaptureWindowA('My Own Capture Window',<br>WS_CHILD or WS_VISIBLE ,<br>Panel1.Left,<br>Panel1.Top,<br>Panel1.Width,<br>Panel1.Height,<br>Form1.Handle,<br>0);<br>if hWndC &lt;&gt; 0 then<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);<br>SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);<br>SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);<br>//SendMessage(hWndC, WM_CAP_SEQUENCE_NOFILE, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);<br>SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);<br>end;</p>
<p>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);<br>hWndC := 0;<br>end;<br>end;</p>
<p>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC,WM_CAP_SAVEDIB,0,longint(pchar('c:\\test.bmp')));<br>end;<br>end;</p>
<p>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then<br>begin<br>SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0, Longint(pchar('c:\\test.avi')));<br>SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);<br>end;<br>end;</p>
<p>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br>if hWndC &lt;&gt; 0 then begin<br>SendMessage(hWndC, WM_CAP_STOP, 0, 0);<br>end;<br>end;</p>
<p>procedure TForm1.Button7Click(Sender: TObject);<br>begin<br>openDialog1.DefaultExt := 'avi';<br>openDialog1.Filter := 'avi files (*.avi)|*.avi';</p>
<p>if OpenDialog1.Execute&nbsp; then<br>begin<br>&nbsp;if (MediaPlayer1.DeviceID&lt;&gt;0) then<br>&nbsp; begin<br>&nbsp;&nbsp;&nbsp; if (MediaPlayer1.Mode=mpplaying) then MediaPlayer1.Stop;<br>&nbsp; end;</p>
<p>&nbsp;MediaPlayer1.FileName:=openDialog1.FileName;<br>&nbsp;//MediaPlayer1.DisplayRect.Top:=panel2.Top;<br>&nbsp;//MediaPlayer1.DisplayRect.Left:=panel2.left;<br>&nbsp;//MediaPlayer1.DisplayRect.Right:=panel2.Height;<br>&nbsp;//MediaPlayer1.DeviceType&nbsp;&nbsp; :=dtAutoSelect;<br>&nbsp;Mediaplayer1.Open;<br>&nbsp;MediaPlayer1.Play;<br>&nbsp;end;<br>end;</p>
<p>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>close;<br>end;</p>
<p>end.</p>
<p>//= == =====================</p>
<p>object Form1: TForm1<br>&nbsp; Left = 192<br>&nbsp; Top = 114<br>&nbsp; Width = 658<br>&nbsp; Height = 422<br>&nbsp; Caption = '摄像头操作'<br>&nbsp; Color = clBtnFace<br>&nbsp; Font.Charset = DEFAULT_CHARSET<br>&nbsp; Font.Color = clWindowText<br>&nbsp; Font.Height = -11<br>&nbsp; Font.Name = 'MS Sans Serif'<br>&nbsp; Font.Style = []<br>&nbsp; OldCreateOrder = False<br>&nbsp; PixelsPerInch = 96<br>&nbsp; TextHeight = 13<br>&nbsp; object Button1: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 8<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '激活摄像头'<br>&nbsp;&nbsp;&nbsp; TabOrder = 0<br>&nbsp;&nbsp;&nbsp; OnClick = Button1Click<br>&nbsp; end<br>&nbsp; object Panel1: TPanel<br>&nbsp;&nbsp;&nbsp; Left = 0<br>&nbsp;&nbsp;&nbsp; Top = 0<br>&nbsp;&nbsp;&nbsp; Width = 521<br>&nbsp;&nbsp;&nbsp; Height = 385<br>&nbsp;&nbsp;&nbsp; Caption = '摄像头尚未激活中'<br>&nbsp;&nbsp;&nbsp; TabOrder = 1<br>&nbsp;&nbsp;&nbsp; object MediaPlayer1: TMediaPlayer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Left = 200<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Top = 16<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Width = 253<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Height = 33<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TabOrder = 0<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end<br>&nbsp; object Button2: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 56<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '关闭摄像头'<br>&nbsp;&nbsp;&nbsp; TabOrder = 2<br>&nbsp;&nbsp;&nbsp; OnClick = Button2Click<br>&nbsp; end<br>&nbsp; object Button3: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 112<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '保存为BMP图片'<br>&nbsp;&nbsp;&nbsp; TabOrder = 3<br>&nbsp;&nbsp;&nbsp; OnClick = Button3Click<br>&nbsp; end<br>&nbsp; object Button4: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 160<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '开始录像'<br>&nbsp;&nbsp;&nbsp; TabOrder = 4<br>&nbsp;&nbsp;&nbsp; OnClick = Button4Click<br>&nbsp; end<br>&nbsp; object Button5: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 208<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '停止录像'<br>&nbsp;&nbsp;&nbsp; TabOrder = 5<br>&nbsp;&nbsp;&nbsp; OnClick = Button5Click<br>&nbsp; end<br>&nbsp; object Button6: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 320<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 41<br>&nbsp;&nbsp;&nbsp; Caption = '退出'<br>&nbsp;&nbsp;&nbsp; TabOrder = 6<br>&nbsp;&nbsp;&nbsp; OnClick = Button6Click<br>&nbsp; end<br>&nbsp; object Button7: TButton<br>&nbsp;&nbsp;&nbsp; Left = 536<br>&nbsp;&nbsp;&nbsp; Top = 256<br>&nbsp;&nbsp;&nbsp; Width = 97<br>&nbsp;&nbsp;&nbsp; Height = 49<br>&nbsp;&nbsp;&nbsp; Caption = '加载视频'<br>&nbsp;&nbsp;&nbsp; TabOrder = 7<br>&nbsp;&nbsp;&nbsp; OnClick = Button7Click<br>&nbsp; end<br>&nbsp; object OpenDialog1: TOpenDialog<br>&nbsp;&nbsp;&nbsp; Left = 464<br>&nbsp;&nbsp;&nbsp; Top = 24<br>&nbsp; end<br>end</p>

</td>

</tr>

</tbody>

</table>

</div>
<p>摘录自:Delphi摄像头操作&nbsp;http://yxmhero1989.blog.163.com/blog/static/112157956201102703433980/</p>
<p>试写的Delphi摄像头照相(抓图)小软件&nbsp;&nbsp;http://www.codefans.net/soft/8385.shtml<br>Delphi 激活摄像头&nbsp;&nbsp;http://hi.baidu.com/warrially/blog<br>利用Delphi编程控制摄像头&nbsp;http://www.haoxiai.net/bianchengyuyan/Delphi/84272.html<br>http://blog.csdn.net/walkershrek/archive/2007/12/04/1915440.aspx<br>关与Usb摄像头问题&nbsp;&nbsp;http://group.gimoo.net/review/142849<br>MTPlay V1.0 摄像头视频捕捉程序&nbsp;&nbsp;www.hackcode.com<br>用Delphi开发视频捕获(摄像头拍照)程序&nbsp;http://www.delphibbs.com/keylife/iblog_show.asp?xid=15554<br>Delphi实现摄像头拍照能&nbsp;http://www.xuedelphi.cn/wenzhang/mrjq/gjyy/2008/11/200811232621.htm<br>用Delphi控制摄像头编程(转载)http://blog.ednchina.com/999wjc/4230/message.aspx<br>http://blog.csdn.net/walkershrek/archive/2007/12/04/1915429.aspx<br>VB编写控制摄像头程序&nbsp;&nbsp;http://down.bbs156.cn/exef1226.html<br>一个用VC写的视屏聊天工具(源代码)&nbsp;http://www.trydone.com/posts/list/301.page<br>GraghDialog.rar&nbsp;&nbsp;&nbsp;http://www.hackchina.com/cont/182020</p>

</div>
<div id="MySignature" role="contentinfo">
    好的代码像粥一样,都是用时间熬出来的<br><br>
来源:https://www.cnblogs.com/jijm123/p/14157698.html
頁: [1]
查看完整版本: Delphi 控制摄像头操作