FMX.CameraComponent Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample project shows how to use and manipulate the camera of a device. The sample demonstrates the use of the TCameraComponent.

Location

You can find the CameraComponent sample project at:

  • Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
    • CPP\Mobile Snippets\CameraComponent\
    • Object Pascal\Mobile Snippets\CameraComponent\
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This sample demonstrates the use of the TCameraComponent, which is the nonvisual component for a camera device.

The sample project shows how to manipulate the camera of a device, and how to set preset and custom quality capture settings for your camera. With this sample you can:

  • Turn on and off the camera of your device.
  • Select either the front facing camera or the back facing camera.
  • Set different resolution configurations for your camera.
  • Select whether to turn the flash on, off, or leave it in automatic mode.

How to Use the Sample

  1. Navigate to one of the locations given above and open:
    • Delphi: CameraComponent.dproj
    • C++: CameraComponent.cbproj
  2. In the Projects Window, select the target platform.
    Note: The device where you run the application must have a camera.
  3. Press F9 or choose Run > Run.
  4. The available Camera Resolutions are automatically filled in the cbResolutions combo box.
  5. To interact with the sample:
    • Use the Start Camera and Stop Camera buttons to enable and disable the camera component.
    • Use the Front and Back buttons of the Camera Type section to change between the front and back camera of your device.
    • Change the Camera Resolutions by:
    • Using the Low, Medium, High, or Photo preset resolutions.
    • Selecting a resolution from the cbResolutions combo box to get a custom resolution based on the available ones. Use the cbPriority combo box to set how the resolutions must be arranged, either prioritizing the resolution or the frame rate.
    • Use the On, Off, and Auto buttons available in the Camera Torch Type section to manage the camera torch.
    CameraComponent 1.png    CameraComponent 2.png

Files

File in Delphi File in C++ Contains

CameraComponent.dproj

CameraComponent.cbproj

The project itself.

uMain.fmx

uMain.fmx

The main form where the components are located.

uMain.pas

uMain.h, uMain.cpp

Delphi source file, C++ header file, and C++ implementation file respectively.

Information.txt

Information.txt

Basic information about this sample.

Implementation

The Active property is used to turn on and off the camera.

The TVideoCaptureSetting record is used to store a single camera frame rate with its corresponding resolution.

Viewing the Camera Captured Data

The SampleBufferToBitmap method is used to display the captured data on a bitmap.

The event handler written for the OnSampleBufferReady event, synchronizes the CurrentThread, that represents the main thread, with the method to capture and display the data. Synchronize is used to execute the SampleBufferToBitmap method within the main thread.

Ensuring the Camera is Released When Required

When the form is created, the sample checks whether the IFMXApplicationEventService platform service is supported, this service will allow you to enable or disable the camera when the application goes to background.

TApplicationEvent is used to check the different application events to ensure the camera is released when the app goes away in the following cases: WillBecomeInactive, EnteredBackground, and WillTerminate.

Warning: It is important that the camera is released as required; otherwise the camera will remain engaged by the app (even after going to background), and other apps will not have access to use the camera.

Changing the Camera Type

The Kind property is used to select the front camera or the back camera of the device: TCameraKind.FrontCamera and TCameraKind.BackCamera.

Since the front and back camera of a device tend to have different resolutions; when the camera changes, the available resolutions are updated.

Setting the Quality of the Captured Video Data

The Quality property is used to set the quality of the video capture. It is possible to set preset or custom camera quality:

Preset Camera Quality

The Quality property is set to one of the following TVideoCaptureQuality values: LowQuality, MediumQuality, HighQuality, and PhotoQuality, that represent the available preset camera quality values.

Custom Camera Quality

To set a custom camera quality, the sample sets the Quality property to CaptureSettings. Then, the AvailableCaptureSettings property is used to capture the settings available in the selected camera. These settings are then displayed in the cbResolutions TComboBox so the user can select the wanted custom camera quality. When combo box changes the CaptureSetting property is used to set the selected custom camera settings.

To gather the AvailableCaptureSettings, the sample determines the priority criterion to sort the results using the CaptureSettingPriority property. The two possible capture settings priorities are Resolution and FrameRate.

Using the Torch

The TorchMode property is used to set the torch to one of the possible torch modes: ModeOff, ModeOn, and ModeAuto.

Uses

See Also