问题描述
我使用标准代码初始化TVideoCaptureDevice并开始捕获。
I use the standard code to initialize TVideoCaptureDevice and start capturing.
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:='视频捕获设备不可用。'
正在运行。而且,在此之后,相机甚至不会从Button OnClick事件中初始化。
The code properly works when I dispatch a message from a button OnClick event but when the message is dispatched from TIdTCPServer OnExecute the camera does not start and Caption := 'Video capture devices not available.'
is run. Moreover, after this the camera does not initialize even from the Button OnClick event.
在直接调用
VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
VideoCamera.OnSampleBufferReady := CameraReady;
VideoCamera.StartCapture;
end;
。虽然从Button OnClick运行时效果很好。
使用TCameraComponent会导致相同的问题。
如果在Form OnCreate事件中处理了相机初始化,则可以解决此问题,但这不适合,因为两个或多个应用程序不允许同时使用相机。
from within Server OnExecute event. Though it works fine when run from the Button OnClick.Using of TCameraComponent cause the same problems.This issue could be reolved if camera initialization is handled in Form OnCreate event but this is not suitable as simultaneous usage of camera is not allowed by two or more applications.
推荐答案
似乎,捕获设备应该从主线程进行初始化和操作。尝试将捕获操作包装在TThread.Synchronize类过程中,如下所示:
It seems, capture device should be initialized and manipulated from the main thread. Try to wrap capture manipulating in TThread.Synchronize class procedure, smth like this:
procedure TMyForm.IdTCPServer1Execute(AContext: TIdContext);
...
begin
...
TThread.Synchronize(nil,
procedure
begin
DoSmthWithCamera();
end;
);
...
end;
这篇关于TCameraComponent和TVideoCaptureDevice不在Win32中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!