首页 > 解决方案 > FMX Delphi 10.2 网络摄像头访问冲突,但只是有时

问题描述

谁能帮我这个?我在 Delphi 10.2 中使用 FireMonkey 制作了一个应用程序,它在我的表单上显示来自网络摄像头的视频,并在一个操作中将其插入数据库。网络摄像头的代码是这样的:

https://www.youtube.com/watch?v=EDgCyE0MSVo

我使用TVideoCaptureDevice类和这段代码:

VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
if VideoCamera <> nil then
begin
  VideoCamera.OnSampleBufferReady := SampleBufferReady;
  VideoCamera.StartCapture;
end
else
begin
  // MessageDlg('.',  TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOK], 0);
end;

SampleBufferReady()过程中,我有这行代码:

VideoCamera.SampleBufferToBitmap(frame, true);

当我关闭应用程序时,我有以下代码:

if VideoCamera <> nil then
  VideoCamera.StopCapture;

应用程序可以正常工作数千次,但有时我会遇到访问冲突,我想在FMX.Media或 中的某个地方System,如您所见:

从调试器打印屏幕

或者这个

来自调试器的下一个打印屏幕

我怎样才能弄清楚会发生什么?

如下所示,我无法更改帧速率

具有帧速率变化的打印屏幕

当我列出 AvailableCaptureSettings 时,我只得到 30 fps

可用设置

现在我与调试器完全不同的打印屏幕

新的打印屏幕

正如你所说,我做了一个新的简单项目。这是完整的代码:

unit Unit3;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Media,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Objects, FMX.Layouts;

type
  TForm3 = class(TForm)
    layCamera1: TLayout;
    imgCamera1: TImage;
    Rectangle1: TRectangle;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    VideoCamera : TVideoCaptureDevice;
    procedure SampleBufferSync;
    procedure SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
  end;

var
  Form3: TForm3;
  frame: TBitmap;

implementation

{$R *.fmx}

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if VideoCamera <> nil then
    VideoCamera.StopCapture;

end;

procedure TForm3.FormShow(Sender: TObject);
begin
  VideoCamera:= TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
  if VideoCamera <> nil then
  begin
      VideoCamera.OnSampleBufferReady := SampleBufferReady;
      VideoCamera.StartCapture;
  end
  else
  begin

  end;
end;

procedure TForm3.SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
begin
  if frame = nil then
    frame := TBitmap.Create;

  VideoCamera.SampleBufferToBitmap(frame, true);


  imgCamera1.Bitmap.Assign(frame);

end;

procedure TForm3.SampleBufferSync;
begin
  try
     if frame = nil then
      frame := TBitmap.Create;
    VideoCamera.SampleBufferToBitmap(frame, true);
    imgCamera1.Bitmap.Assign(frame);
  except on E:Exception do
  begin
     MessageDlg(E.Message, TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOK], 0);
  end;
  end;
end;

end.

当我启动程序时,它工作了一段时间,但在这个调试窗口几分钟后意外崩溃(有时是 2 分钟,有时是 22 分钟)

调试器窗口

另一个问题是,有时,当我关闭一个程序时,它不会关闭,直到我按下 Delphi 中的停止按钮强制它

标签: delphifiremonkeywebcam

解决方案


如评论中所述,我已经构建了 OP 在他的问题中发布的“简单新项目”。

它运行了6个多小时没有任何问题,并且在我做其他事情时仍在运行。我有一个罗技 C310 网络摄像头。安装了所有更新的 Windows 10 Pro 20H2 版本 19042.685。Delphi 10.4.1 更新 1,版本 27.0.38860.1461。

我的结论是 OP 正在使用的 Delphi 10.2 中有一个错误。至少在我正在使用的 Delphi 10.4.1 中,该错误已得到修复。


推荐阅读