Skip to content

TTMSFNCWXCamera

The TTMSFNCWXCamera is a component that accesses a camera and displays a live video stream. It supports front/back/default camera selection, snapshot capture in PNG or JPEG format, device enumeration, and browser-level media constraints. It can be used as an input source for TTMSFNCWXBarcodeDecoder and TTMSFNCWXQRDecoder

Requirements

  • macOS: An internet connection is required. Set FileLocation to cflOnline.
  • iOS: Minimum version 14.6.
  • WEB: The application must be hosted in a secure context (HTTPS) for device enumeration.

Starting and stopping the camera

The camera does not start automatically. Call Start when ready.

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

procedure TForm1.Button2Click(Sender: TObject);
begin
  TMSFNCWXCamera1.Pause;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  TMSFNCWXCamera1.Stop;
end;

Selecting a camera type

The CameraType property selects which camera to use:

Value Description
wctDefault The browser's default camera selection (default).
wctFront Front-facing camera (selfie).
wctBack Back-facing camera.
wctSelected A specific device chosen via SelectDevice.
TMSFNCWXCamera1.CameraType := wctFront;
TMSFNCWXCamera1.Start;

List of available camera devices

To let the user choose a specific device, set CameraType to wctSelected, then call RequestDevices. When OnCameraDevicesInitialized fires, loop the Devices collection and call SelectDevice.

Important

Device enumeration requires an internet connection on native platforms. On WEB the application must run in a secure context.

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCWXCamera1.CameraType := wctSelected;
  TMSFNCWXCamera1.RequestDevices;
end;

procedure TForm1.TMSFNCWXCamera1CameraDevicesInitialized(Sender: TObject);
var
  I: Integer;
begin
  ComboBox1.Clear;
  for I := 0 to TMSFNCWXCamera1.Devices.Count - 1 do
    ComboBox1.Items.Add(TMSFNCWXCamera1.Devices.Items[I].Name);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  TMSFNCWXCamera1.SelectDevice(
    TMSFNCWXCamera1.Devices.Items[ComboBox1.ItemIndex]);
  TMSFNCWXCamera1.Start;
end;

Setting the camera resolution

The Resolution sub-property expresses an ideal (not guaranteed) resolution. The browser uses the closest supported resolution.

TMSFNCWXCamera1.Resolution.IdealWidth  := 1280;
TMSFNCWXCamera1.Resolution.IdealHeight := 720;

Change the resolution while the camera is running:

TMSFNCWXCamera1.ChangeCameraResolution(1920, 1080);

Taking a snapshot

Call TakeSnapshot to capture the current frame. The result is delivered via OnSnapshot (as a TTMSFNCBitmap) and OnSnapshotBase64 (as a Base64 string).

procedure TForm1.Button4Click(Sender: TObject);
begin
  TMSFNCWXCamera1.TakeSnapshot(ifJPEG);  // ifPNG is the default
end;

procedure TForm1.TMSFNCWXCamera1Snapshot(Sender: TObject; ABitmap: TTMSFNCBitmap);
begin
  ABitmap.SaveToFile('snapshot.png');
end;

procedure TForm1.TMSFNCWXCamera1SnapshotBase64(Sender: TObject; ABase64: string);
begin
  // Use ABase64 as a data URI or send to a server
end;

Applying media 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.

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;

macOS setup

On macOS, the camera component file must be loaded from an online source. Set FileLocation to cflOnline before starting:

TMSFNCWXCamera1.FileLocation := cflOnline;
TMSFNCWXCamera1.Start;

Properties

Property Type Default Description
CameraType TTMSFNCWXCameraType wctDefault Which camera to use: wctDefault, wctFront, wctBack, or wctSelected.
Devices TTMSFNCWXCameraDeviceList Read-only. List of available camera devices populated by RequestDevices.
FileLocation TTMSFNCWXCameraFileLocation cflOffline How the camera component file is loaded: cflOffline (embedded) or cflOnline (CDN). Required for macOS.
KeepRatio Boolean True Maintains the camera's aspect ratio within the component bounds.
Resolution.IdealWidth Integer 640 Ideal capture width in pixels.
Resolution.IdealHeight Integer 480 Ideal capture height in pixels.
SupportedConstraints TStringList Read-only. List of supported media constraint names. Available after OnGetSupportedConstraints.

Methods

Method Description
ApplyConstraints(AConstraints: TJSONObject) Applies media constraints to the current video track.
ChangeCameraResolution(AWidth, AHeight: Integer) Changes the capture resolution while the camera is running.
Pause Pauses the camera stream without releasing the device.
RequestDevices Enumerates available camera devices. Results arrive via OnCameraDevicesInitialized.
Resume Resumes a paused camera stream.
SelectDevice(ADevice: TTMSFNCWXCameraDevice) Selects a specific camera device from the Devices list.
Start Starts the camera stream.
Stop Stops the camera stream and releases the device.
TakeSnapshot(AFormat: TTMSFNCWXCameraImageFormat) Captures the current frame. Default format is ifPNG. Results via OnSnapshot and OnSnapshotBase64.

Events

Event Description
OnCameraDevicesInitialized Fires when device enumeration completes. Access results via Devices.
OnGetSupportedConstraints Fires after initialization. Access supported constraint names via SupportedConstraints.
OnPause Fires when the camera is paused.
OnSnapshot Fires after TakeSnapshot. Receives the captured frame as a TTMSFNCBitmap.
OnSnapshotBase64 Fires after TakeSnapshot. Receives the captured frame as a Base64 string.
OnStart Fires when the camera stream starts.
OnStop Fires when the camera stream stops.
OnSwitchCamera Fires when the active camera device changes.