开发者

How to take snapshot and save to JPEG from webcam using DSPack?

开发者 https://www.devze.com 2023-03-22 11:08 出处:网络
Usin开发者_如何学运维g DSPack, Delphi XE I need to take a snapshot from a webcam and allow a preview before which the user is allowed to save to JPEG file. How can this be done (code)?Maybe this will

Usin开发者_如何学运维g DSPack, Delphi XE I need to take a snapshot from a webcam and allow a preview before which the user is allowed to save to JPEG file. How can this be done (code)?


Maybe this will work, but I have not tested it. You should give it a try.

unit main;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, DSPack, DSUtil, DirectShow9;

type
 TMainForm = class(TForm)
   CaptureGraph: TFilterGraph;
   VideoWindow: TVideoWindow;
   ListBox1: TListBox;
   VideoSourceFilter: TFilter;
   StartButton: TButton;
   StopButton: TButton;
   Label1: TLabel;
   ListBox2: TListBox;
   Label3: TLabel;
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure ListBox1Click(Sender: TObject);
   procedure StartButtonClick(Sender: TObject);
   procedure StopButtonClick(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 MainForm: TMainForm;
 VideoDevice: TSysDevEnum;
 VideoMediaTypes: TEnumMediaType;
implementation

{$R *.dfm}

{ TMainForm }

procedure TMainForm.FormCreate(Sender: TObject);
var i: integer;
begin
 VideoDevice := TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
 for i := 0 to VideoDevice.CountFilters - 1 do
   ListBox1.Items.Add(VideoDevice.Filters[i].FriendlyName);

 VideoMediaTypes := TEnumMediaType.Create;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
 VideoDevice.Free;
 VideoMediaTypes.Free;
end;

// Selecting of the video source
procedure TMainForm.ListBox1Click(Sender: TObject);
var
 PinList: TPinList;
 i: integer;
begin
 VideoDevice.SelectGUIDCategory(CLSID_VideoInputDeviceCategory);
 if ListBox1.ItemIndex <> -1 then
 begin
   // Set the device which we work with
   VideoSourceFilter.BaseFilter.Moniker := VideoDevice.GetMoniker(ListBox1.ItemIndex);
   VideoSourceFilter.FilterGraph := CaptureGraph;
   CaptureGraph.Active := true;
   PinList := TPinList.Create(VideoSourceFilter as IBaseFilter);
   ListBox2.Clear;
   VideoMediaTypes.Assign(PinList.First);
   // Adding permission to ListBox2, which supports device
   for i := 0 to VideoMediaTypes.Count - 1 do
     ListBox2.Items.Add(VideoMediaTypes.MediaDescription[i]);
   CaptureGraph.Active := false;
   PinList.Free;
   StartButton.Enabled := true;
 end;
end;

procedure TMainForm.StartButtonClick(Sender: TObject);
var
 PinList: TPinList;
begin

 // Activating graph filter, at this stage the source filter is added to the graph
 CaptureGraph.Active := true;

 // The configuration of the output device
 if VideoSourceFilter.FilterGraph <> nil then
 begin
   PinList := TPinList.Create(VideoSourceFilter as IBaseFilter);
   if ListBox2.ItemIndex <> -1 then
     with (PinList.First as IAMStreamConfig) do
       SetFormat(VideoMediaTypes.Items[ListBox2.ItemIndex].AMMediaType^);
   PinList.Free;
 end;

 // now render streams
 with CaptureGraph as IcaptureGraphBuilder2 do
 begin
   // Hooking up a preview video (VideoWindow)
   if VideoSourceFilter.BaseFilter.DataLength > 0 then
     RenderStream(@PIN_CATEGORY_PREVIEW, nil, VideoSourceFilter as IBaseFilter,
       nil , VideoWindow as IBaseFilter);

 end;
// Launch video
 CaptureGraph.Play;
 StopButton.Enabled := true;
 StartButton.Enabled := false;
 ListBox2.Enabled := false;
 ListBox1.Enabled := false;
end;

// Stop video
procedure TMainForm.StopButtonClick(Sender: TObject);
begin
 StopButton.Enabled := false;
 StartButton.Enabled := true;
 CaptureGraph.Stop;
 CaptureGraph.Active := False;
 ListBox2.Enabled := true;
 ListBox1.Enabled := true;
end;

end.
0

精彩评论

暂无评论...
验证码 换一张
取 消