Jump to content
DavidJr.

TVideoCaptureDevice will not run in (Delphi 10.2.3)

Recommended Posts

Hi,  I am following this example for setting up a VideoCaptureDevice app: https://docwiki.embarcadero.com/RADStudio/Sydney/en/Video_Capturing

 

I could not get the VideoCaptureDevice to start.  I tried 4 known good cameras and get the same thing,   I modified the code a bit to give me an indicator that I am indeed setting up the app right,  still nothing.  The app compiles just fine,  just won't start capture.

 

My version of Delphi is 10.2.3 and the following is my current code:

 

unit FMXCamera_Main;

interface

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

type
  TForm1 = class(TForm)
    Layout1: TLayout;
    btnStart: TButton;
    ComboBox1: TComboBox;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    VideoCamera: TVideoCaptureDevice;

    procedure SampleBufferSync;
    procedure SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
  end;

var
  Form1: TForm1;

implementation

// Delphi version of the implementation


{$R *.fmx}

procedure TForm1.btnStartClick(Sender: TObject);
begin
  if(VideoCamera <> nil) then
    begin
      if(VideoCamera.State = TCaptureDeviceState.Stopped) then
        begin
          VideoCamera.OnSampleBufferReady := SampleBufferReady;
          VideoCamera.CaptureSettingPriority := TVideoCaptureSettingPriority.FrameRate;
          VideoCamera.StartCapture;
          if(VideoCamera.State = TCaptureDeviceState.Capturing) then
            begin
              btnStart.Text := 'Stop';
            end
          else
            begin
              VideoCamera.StopCapture;
              btnStart.Text := 'Start';
            end;
        end
      else
        begin
          VideoCamera.StopCapture;
          btnStart.Text := 'Start';
        end;
    end
  else
    begin
      Caption := 'Video capture devices not available.';
    end;

end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  VideoCamera := TVideoCaptureDevice(TCaptureDeviceManager.Current.GetDevicesByName(ComboBox1.Selected.Text));

  if (VideoCamera <> nil) then
    begin
      btnstart.Text := 'Start '+VideoCamera.Name;
      btnStart.Enabled := true;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  DeviceList: TCaptureDeviceList;
  i: integer;

begin
  DeviceList := TCaptureDeviceManager.Current.GetDevicesByMediaType(TMediaType.Video);
  for i := 0 to DeviceList.Count - 1 do
    begin
      ComboBox1.Items.Add(DeviceList[i].Name);
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if(VideoCamera <> nil) then
    begin
      VideoCamera.StopCapture;
    end;
end;

procedure TForm1.SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
begin
  TThread.Synchronize(TThread.CurrentThread, SampleBufferSync);
  //Resize the image so that the video is buffered in its original size
  Image1.Width := Image1.Bitmap.Width;
  Image1.Height := Image1.Bitmap.Height;
end;

procedure TForm1.SampleBufferSync;
begin
  VideoCamera.SampleBufferToBitmap(Image1.Bitmap, true);
end;

end.

 

Share this post


Link to post

Ah, in that case - ensure there is no other software that might grab the camera instead of your app?

Share this post


Link to post
1 minute ago, Lars Fosdal said:

Ah, in that case - ensure there is no other software that might grab the camera instead of your app?

I have absolutely done all of that.  I am not even getting any errors in the IDE.

Share this post


Link to post

And there is nothing in the code that eats exceptions instead of exposing them?

What about stills? Can you get the example to work?

Share this post


Link to post
1 minute ago, Lars Fosdal said:

And there is nothing in the code that eats exceptions instead of exposing them?

What about stills? Can you get the example to work?

The example did not work,  The Camera is in a state of "Stopped" after calling "StartCapture"

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×