あなたのコンピュータはカメラを持っていますか?他の人がQQで遊んでいるのを見るとき、あなたはそれを達成する方法について考えますか?
ここでは、Delphiを使用してMS AVICAP32.DLLを使用してカメラプログラミングを簡単に実装することを紹介します。
ネットワークプログラミングレベルを追加すると、ビデオチャットの実装は問題になりません。
以下のコードをご覧ください。
const wm_cap_start = wm_user;
const wm_cap_stop = wm_cap_start + 68;
const wm_cap_driver_connect = wm_cap_start + 10;
const wm_cap_driver_disconnect = wm_cap_start + 11;
const wm_cap_savedib = wm_cap_start + 25;
const wm_cap_grab_frame = wm_cap_start + 60;
const wm_cap_sequence = wm_cap_start + 62;
const wm_cap_file_set_capture_filea = wm_cap_start + 20;
const wm_cap_sequence_nofile = wm_cap_start+ 63
const wm_cap_set_overlay = wm_cap_start+ 51
const wm_cap_set_preview = wm_cap_start+ 50
const wm_cap_set_callback_videostream = wm_cap_start +6;
const wm_cap_set_callback_error = wm_cap_start +2;
const wm_cap_set_callback_statusa = wm_cap_start +3;
const wm_cap_set_callback_frame = wm_cap_start +5;
const wm_cap_set_scale = wm_cap_start+ 53
const wm_cap_set_previewrate = wm_cap_start+ 52
function capcreatecapturewindowa(lpszwindowname:pchar; dwstyle:longint; x:integer;
y:integer; nwidth:integer; nheight:integer; parentwin:hwnd;
nid:integer):hwnd; stdcall external 'avicap32.dll';
上記のコードは、主に使用する関数と定数の定義です。
わかりました、Delphiを開き、新しいプロジェクトを作成し、上記の定義を追加します。
新しいウィンドウを作成し、パネルを置き、ボタンを追加し、キャプションを「開始」に設定します
ここでは、グローバル変数、var hwndc:thandleを定義する必要があります。
開始ボタンのコードは次のとおりです。
始める
hwndc:= capcreatecapturewindowa(「私自身のキャプチャウィンドウ」、ws_childまたはws_visible、panel1.left、panel1.top、panel1.width、panel1.height、form.handle、0);
hwndc:= capcreatecapturewindowa(「私自身のキャプチャウィンドウ」、ws_childまたはws_visible、panel1.left、panel1.top、panel1.width、panel1.height、form.handle、0);
hwndc <> 0の場合
始める
sendmessage(hwndc、wm_cap_set_callback_videostream、0、0);
sendMessage(hwndc、wm_cap_set_callback_error、0、0);
sendMessage(hwndc、wm_cap_set_callback_statusa、0、0);
sendmessage(hwndc、wm_cap_driver_connect、0、0);
sendMessage(hwndc、wm_cap_set_scale、1、0);
sendMessage(hwndc、wm_cap_set_previewrate、66、0);
sendmessage(hwndc、wm_cap_set_overlay、1、0);
sendmessage(hwndc、wm_cap_set_preview、1、0);
終わり;
F9を押して、カメラのビデオ画面が表示されますか?
次に、停止する方法は?別のボタンのキャプションを追加して「停止」
コードは次のとおりです。
hwndc <> 0の場合、開始します
sendmessage(hwndc、wm_cap_driver_disconnect、0、0);
hwndc:= 0;
終わり;
ビデオ画面がスクリーニングされましたが、どうすれば保存できますか?
以下は2つの方法で保存されます。1つはBMP静的図、もう1つはAVIアニメーションです。
フォームに3つのボタンを置き、キャプションを「BMPの保存」、「ビデオ録画を開始」、「停止ビデオ録画」にそれぞれ設定します。
3つのボタンのコードは次のとおりです。
// BMPを保存します
hwndc <> 0の場合、開始します
sendMessage(hwndc、wm_cap_savedib、0、longint(pchar( 'c://test.bmp')));
終わり;
//ビデオ録画を開始します
hwndc <> 0の場合
始める
sendmessage(hwndc、wm_cap_file_set_capture_filea、0、longint(pchar( 'c://test.avi')));
sendMessage(hwndc、wm_cap_sequence、0、0);
終わり;
//録音を停止します
hwndc <> 0の場合、開始します
sendMessage(hwndc、wm_cap_stop、0、0);
終わり;
もう一度実行して見てください。 。いくつかの写真を保存して見ることができます。または、それらをAVIに録音してゆっくり楽しむことができます。
プログラムの実行効果:[関連マップ]
完全なプログラムコードは次のとおりです。
ユニットユニット1;
インタフェース
用途
窓、メッセージ、sysutils、バリアント、クラス、グラフィック、コントロール、フォーム、
ダイアログ、stdctrls、extctrls;
タイプ
tform1 = class(tform)
パネル:Tpanel;
Button1:Tbutton;
Button2:tbutton;
Button3:Tbutton;
Button4:Tbutton;
Button5:Tbutton;
手順button1click(sender:tobject);
手順button2click(sender:tobject);
手順button3click(sender:tobject);
手順button4click(sender:tobject);
手順button5click(sender:tobject);
手順formclose(sender:tobject; var action:tcloseaction);
プライベート
hwndc:thandle;
公共
{公開宣言}
終わり;
var
form1:tform1;
const wm_cap_start = wm_user;
const wm_cap_stop = wm_cap_start + 68;
const wm_cap_driver_connect = wm_cap_start + 10;
const wm_cap_driver_disconnect = wm_cap_start + 11;
const wm_cap_savedib = wm_cap_start + 25;
const wm_cap_grab_frame = wm_cap_start + 60;
const wm_cap_sequence = wm_cap_start + 62;
const wm_cap_file_set_capture_filea = wm_cap_start + 20;
const wm_cap_sequence_nofile = wm_cap_start+ 63
const wm_cap_set_overlay = wm_cap_start+ 51
const wm_cap_set_preview = wm_cap_start+ 50
const wm_cap_set_callback_videostream = wm_cap_start +6;
const wm_cap_set_callback_error = wm_cap_start +2;
const wm_cap_set_callback_statusa = wm_cap_start +3;
const wm_cap_set_callback_frame = wm_cap_start +5;
const wm_cap_set_scale = wm_cap_start+ 53
const wm_cap_set_previewrate = wm_cap_start+ 52
function capcreatecapturewindowa(lpszwindowname:pchar;
dwstyle:longint; x:integer; y:integer; nwidth:integer;
nheight:integer; parentwin:hwnd; nid:integer):hwnd;
stdcall外部 'avicap32.dll';
実装
{$ r *.dfm}
手順tform1.button1click(sender:tobject);
始める
hwndc:= capcreatecapturewindowa(「私自身のキャプチャウィンドウ」、ws_childまたはws_visible、panel1.left、panel1.top、panel1.width、panel1.height、form.handle、0);
hwndc:= capcreatecapturewindowa(「私自身のキャプチャウィンドウ」、ws_childまたはws_visible、panel1.left、panel1.top、panel1.width、panel1.height、form.handle、0);
hwndc <> 0の場合
始める
sendmessage(hwndc、wm_cap_set_callback_videostream、0、0);
sendMessage(hwndc、wm_cap_set_callback_error、0、0);
sendMessage(hwndc、wm_cap_set_callback_statusa、0、0);
sendmessage(hwndc、wm_cap_driver_connect、0、0);
sendMessage(hwndc、wm_cap_set_scale、1、0);
sendMessage(hwndc、wm_cap_set_previewrate、66、0);
sendmessage(hwndc、wm_cap_set_overlay、1、0);
sendmessage(hwndc、wm_cap_set_preview、1、0);
終わり;
終わり;
手順tform1.button2click(sender:tobject);
始める
hwndc <> 0の場合、開始します
sendmessage(hwndc、wm_cap_driver_disconnect、0、0);
hwndc:= 0;
終わり;
終わり;
手順tform1.button3click(sender:tobject);
始める
hwndc <> 0の場合、開始します
sendMessage(hwndc、wm_cap_savedib、0、longint(pchar( 'c://test.bmp')));
終わり;
終わり;
手順tform1.button4click(sender:tobject);
始める
hwndc <> 0の場合
始める
sendmessage(hwndc、wm_cap_file_set_capture_filea、0、longint(pchar( 'c://test.avi')));
sendMessage(hwndc、wm_cap_sequence、0、0);
終わり;
終わり;
手順tform1.button5click(sender:tobject);
始める
hwndc <> 0の場合、開始します
sendMessage(hwndc、wm_cap_stop、0、0);
終わり;
終わり;
手順tform1.formclose(sender:tobject; var action:tcloseaction);
始める
hwndc <> 0の場合、開始します
sendmessage(hwndc、wm_cap_driver_disconnect、0、0);
終わり;
終わり;
終わり。
コンピューターにカメラがないが、プログラムの効果を確認したい場合、それは大丈夫ですか?
もちろん、仮想カメラを見つけて、それを行うことができます。
「実際の」カメラとしてシミュレートできます。このことを使用して、QQ、MSNなどのチャットソフトウェアを欺かないでください。
カメラのプログラミングについては、VCLコンポーネントのセットをご覧ください。DSPACKは、Microsoft Direct ShowおよびDirectX 9で設計および機能するクラスとコンポーネントのセットです。 、2000およびWindows XP。
さて、これらを紹介してください。実装する。