標準コードを使用してTVideoCaptureDeviceを初期化し、キャプチャを開始します。
const M_LAUNCH_CAMERA = WM_APP + 450;
type
TCamSF1 = class(TForm)
...
protected
procedure LaunchCamera(var Message: TMessage); message M_LAUNCH_CAMERA;
...
end;
...
procedure TCamSF1.LaunchCamera(var Message: TMessage);
begin
if VideoCamera = nil then
begin
VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
VideoCamera.OnSampleBufferReady := CameraReady;
VideoCamera.StartCapture;
end
else
begin
Caption := 'Video capture devices not available.';
end;
end
else
begin
VideoCamera.StartCapture;
end;
end;
procedure TCamSF1.IdTCPServer1Execute(AContext: TIdContext);
var
S: AnsiString;
Command: TAnsiStrings;
Msg: TMessage;
begin
if (AContext <> nil) and (AContext.Connection.Socket.Connected) and
(not AContext.Connection.Socket.InputBufferIsEmpty) then
S := AContext.Connection.Socket.ReadLn;
if S = '' then
exit;
Memo1.Lines.Add(S);
Command := ParseCommandString(S, '#');
if Command[0] = 'camresol' then
begin
CamShotParams := Command;
Msg.Msg := M_LAUNCH_CAMERA;
Dispatch(Msg);
end;
end;
ボタンのOnClickイベントからメッセージをディスパッチするとコードは正しく機能しますが、メッセージがTIdTCPServer OnExecuteからディスパッチされると、カメラが起動せずにCaption := 'Video capture devices not available.'
実行されます。さらに、この後、カメラはButtonOnClickイベントからでも初期化されません。
を直接呼び出す場合もコードは機能しません
VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
VideoCamera.OnSampleBufferReady := CameraReady;
VideoCamera.StartCapture;
end;
ServerOnExecuteイベント内から。ButtonOnClickから実行すると正常に動作しますが。TCameraComponentを使用すると、同じ問題が発生します。この問題は、カメラの初期化がForm OnCreateイベントで処理される場合に解決される可能性がありますが、2つ以上のアプリケーションでカメラの同時使用が許可されていないため、これは適切ではありません。
キャプチャデバイスは、メインスレッドから初期化して操作する必要があるようです。TThread.Synchronizeクラスプロシージャで操作するキャプチャをラップしてみてください。次のようになります。
procedure TMyForm.IdTCPServer1Execute(AContext: TIdContext);
...
begin
...
TThread.Synchronize(nil,
procedure
begin
DoSmthWithCamera();
end;
);
...
end;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加