Skip to content

TTMSFNCWXCamera

The TTMSFNCWXCamera is a component to access and show a stream of the selected camera.

Requirements

macOS requires internet connection.

The minimum required iOS version is 14.6.

Control the camera

On macOS an internet connection is required as the camera file needs to be loaded from an online source. So as a first step, set the public FileLocation property to cflOnline.

The camera stream needs to be started manually:

procedure TForm1.Button1Click(Sender: TObject);
begin
 TTMSFNCWXCamera1.Start;
end;

The camera feed can be paused or stopped by calling TTMSFNCWXCamera.Pause and TTMSFNCWXCamera.Stop.

List of available camera devices

The TTMSFNCWXCamera picks a default camera automatically. It’s possible to change this behaviour by requesting a list of available camera devices, but keep in mind this functionality requires internet connection in native applications. For WEB it’s expected that the application will be hosted in a secure context.

Change the TTMSFNCWXCamera.CameraType property to wctSelected to enable camera device selection. After that, call TTMSFNCWXCamera.RequestDevices to request a list of the available devices. This list of devices can be accessed once the OnCameraDevicesInitialized event is triggered.

procedure TForm2.FormCreate(Sender: TObject);
begin
 TMSFNCWXCamera1.CameraType := wctSelected;
 TMSFNCWXCamera1.RequestDevices;
end;
procedure TForm2.TMSFNCWXCamera1CameraDevicesInitialized(Sender: TObject);
begin
 if TMSFNCWXCamera1.Devices.Count > 0 then
 begin
 TMSFNCWXCamera1.SelectDevice(TMSFNCWXCamera1.Devices.Items[0]);
 TMSFNCWXCamera1.Start;
 end;
end;

Take a snapshot

Taking a snapshot of the current camera feed is as simple as calling TTMSFNCWXCamera.TakeSnapshot and implementing the TTMSFNCWXCamera.OnSnapshot event. The TakeSnapshot method has an AFormat parameter which will determine if the snapshould should be created as PNG or JPEG. By default it is created as PNG.

procedure TForm1.FormCreate(Sender: TObject);
begin
 TMSFNCWXCamera1.TakeSnapshot;
end;
procedure TForm1.TMSFNCWXCamera1Snapshot(Sender: TObject;
 ABitmap: TTMSFNCBitmap);
begin
 ABitmap.SaveToFile('path\to\picture.png');
end;

Apply additional constraints

TTMSFNCWXCamera has basic support for browser supported constraints.

When the OnGetSupportedConstraints event triggers, the supported constraints are available via the SupportedConstraints TStringList property. This event is triggered as soon as the initialization finishes.

To apply the constraints, the ApplyConstraints(TJSONObject) method can be used. Simply construct the equivalent constraints object of what the browser expects as a TJSONObject. The constraints will be applied to the currently running video track. For mor information on constraints consult the relevant MDN docs.

Example: Apply maximum brightness

procedure TForm3.Button1Click(Sender: TObject);
begin
  TMSFNCWXCamera1.Start;
end;

procedure TForm3.Button2Click(Sender: TObject);
var
  obj: TJSONObject;
begin
  obj := TJSONObject.Create;
  try
    obj.AddPair('brightness', 255);
    TMSFNCWXCamera1.ApplyConstraints(obj);
  finally
    obj.Free;
  end;
end;

procedure TForm3.TMSFNCWXCamera1Start(Sender: TObject);
begin
  //Enable the button that allows setting the maximum brightness
  if TMSFNCWXCamera1.SupportedConstraints.IndexOf('brightness') <> -1 then
    Button2.Enabled := True;
end;