A few years ago I started a project for native camera support for Android using the Camera 2 API, partly because the built-in camera support for Delphi was underperforming greatly. I have resurrected the project, added support for iOS, and it is now included in Kastri.

Existing camera support in Delphi

As many of you are probably aware, the existing camera support in Delphi is not very performant, especially on Android. Erik Van Bilsen of Grijjy has provided some hints and workarounds to increase performance, however it still has a way to go.

Camera support in Kastri

Camera support in Kastri builds on the original standalone camera project, and adds support for iOS. On both platforms, instead of using the built-in camera app on devices, native APIs are used. Native controls are also used for displaying the camera preview, so the result is an huge improvement over the existing support in Delphi.

Some caveats

Please be aware that this is a “first release” of camera support. It provides basic turn on/off of the preview, capturing of a still image, and inclusion of location metadata. Other features will be added over time, including:

  • Face detection – some of the code is already present; it just needs to be finished/tested
  • Providing a list of supported resolutions
  • Flash mode on/off
  • Focus control

etc. Developers are welcome to contribute to these enhancements, and sponsorship of Delphi Worlds on Github helps make new features a reality.

Also be aware that the Android support requires Android 6.0 or greater, and  iOS support requires iOS 10.0 or greater.

Demo

The demo for camera support is here, and is dependent on the Kastri library. When you run the demo you may note that when it is in preview mode, there are controls that appear above the preview. These are image controls that use native APIs to display the images, which makes it possible for them to overlay the native preview control. At present, these controls are created at runtime, however the plan is to change them into components that can install into Delphi.

Switching the Include Location switch starts the location sensor, and will prompt for permissions. Tapping the Start button starts the camera preview. Tapping the Camera button in the middle at the bottom captures an image. Tapping the Swap Camera button in the bottom right changes between front and back cameras.

Once an image is captured, tapping the green Accept button in the top right takes you back to the start, and fills the image control with the captured image. Tapping the red Cancel button in the top right takes you back to the start without filling the image control.

Capturing an image, and Including location metadata

When capturing a still image, the image data is returned in a stream, rather than a bitmap. This is so that metadata can be included, because bitmaps do not have metadata. If you wish to include location data in the image, set the IncludeLocation property of the TCamera instance to True, and before the still is captured, set the Location property to the current coordinates.

You can use Delphi’s TLocationSensor to obtain the users location. Remember that it takes a few seconds for the first location event to fire, start the sensor well before the user is able to capture an image. Please use the demo as a guide.

Remember also that when assigning the image stream to a bitmap, the location data will be lost in the bitmap, as it does not store it.

Note: For Delphi 10.4 and 10.4.1 users, if you wish to use location services on iOS, you will need to patch the System.iOS.Sensors unit as per this report, in order for it to work properly.