Tag Archives: Delphi

How to use Azure Cloud Cognitive Services?

What is Azure Cognitive Services?

According to Microsoft, the Cognitive Services in the Microsoft Azure Cloud bring AI within reach of every developer, without requiring machine learning expertise.

Using the Azure Cognitive Services API you can use API’s to embed the ability to hear, speak, translate and understand directly into your applications.

How are Azure Services configured?

Inside the Azure Cloud Services, different functionality is grouped together and made available to developers as online “Resources” that link to a subscription. Cognitive Services is a grouping of a number of AI-based services, with each service offered as a Resource (that you create to make active against your subscription).

The Azure Resource and Subscription model allow each resource to be billed individually and provided from any one of the specific data centers around the world. This makes it possible to develop functionality that fulfills local legal restrictions around the movement of data. It also makes it possible to provide customer-specific configurations when deploying inside an app.

For many of the Azure services, a free tier is available which is ideal for small deployments and testing.

Code Example for using Azure Cognitive Services

I originally looked at Using Azure Translator Services with Delphi in 2015. Since then, the original XML-based API has been deprecated in favour of a JSON-centric API, as part of the re-branding and re-organisation of the Azure cloud with the launch of Cognitive Services. The following code has been re-built from the ground up to take advantage of the latest language features in Delphi.

UPDATED CODE: Download from Github https://github.com/DelphiABall/Azure-Cognitive-Services

The classes in the GitHub repo make it simple to use the following Microsoft Azure Cloud services:


Each service uses the same authentication class (TAzureToken) to convert the access key for the resource into a session token. Note: Translator and Speech Services are different resources and as such use different instances of the authentication class.

unit Azure.API3.Constants provide a list of Regions based on the current Azure Regions, however, if these change, each class has overridden constructors that allow you to pass in the region as a string.

When you create a TAzureToken, use the constructor with the region for the resource, and then set the SubscriptionKey. This will automatically set the correct endpoints for the security to work. At the point the token is requested from the object, the API will be called, and the result cached. When the token times out, a new token will automatically be collected for you.

The following service and features are available to use: Each feature is in the corresponding unit following the naming conversion of Azure.API3.<Service>.<Feature>


  • Text Translation (converting text from one to multiple languages)
  • Dictionary Look up (looking up a words between languages and reporting on its usage)


  • Text To Speech (converting text using one, or multiple voices into audio file)
  • Speech to Text (converting .wav or .ogg files to text)

To see how to use these services, watch the videos below.

Azure Cloud – Translator Services

Azure Cloud – Text to Speech

Azure Cloud – Speech to Text

Getting Started with Delphi

The code sample is written to work in Delphi 10.4 and above. This provides the advantage of being able to work on Windows, macOS, iOS, Android and Linux.

You can download the latest Trial of Delphi from Embarcadero www.embarcadero.com/products/delphi/start-for-free.

For students or open-source projects, check out the latest Delphi community edition

Delphi 64bit Code

Updating 32bit code to Delphi 64bit

Ever since Delphi XE2, it has been possible to generate Delphi 64bit applications from the same code base as your traditional Windows 32bit Delphi code. The business case for 64-bit for business is covered in this tech paper The Impact of 64-bit Applications to your Company’s Bottom Line.

On the whole moving to 64bit (on iOS or Windows) is beautifully simple to achieve! It can be just as simple as adding the Delphi 64bit Windows target platform in the project manager and rebuilding the project.

My experience from talking to many developers who have moved up is that normally there are a few things to check in your code but typically its not a massive task to get compiling and ready to test.

A lot has been recorded on moving from Windows 32bit to Windows 64bit Delphi and this should be a useful summary if your just planning now moving up from older versions of Delphi to Delphi 10. If you are building iOS applications, then you will need to use the 64bit build now to get into the AppStore. Thankfully, Delphi has made the task of using 64bit very simple across all platforms and protected us from the headaches non Delphi Developers have had on the whole.

Lets start with this short video from David I who covers some the foundations in 7 minutes!

Continue reading Delphi 64bit Code

Bitmap to PNG/JPEG/JPG in Delphi / C++Builder

In FireMonkey, Bitmap is the common currency for working with images. If you want to send an image via Stream using LiveBindings or want to save an Image to disk or into a database on a device with limited storage however, they are quite large.

Thankfully FireMonkey has a really easy to use CodecManager that allows you to SaveToFile or SaveToStream and convert to PNG or JPG across all platforms (which are vastly smaller in size).


Creating a PNG, JPG/JPEG is as simple as calling the Bitmap.SaveToFile with the correct file extension. This works really well with even more extensions supported.

Personally I would use PNG as the JPG formats are around the same size on some test I’ve run, but produce a better quality of image, especially when grabbing screenshots.

SaveToStream and TBitmapCodecManager

Bitmap.SaveToStream doesn’t expose the CodexManager in the same way as it is available automatically with the SaveToFile option so takes a little more work to get a compressed image into the memory stream.

This is done using the TBitmapCodecManager class. A simple screen test has gone from over 1mb as a bitmap to around 6kb when saved to PNG using this approach.

The following code is an example using two image viewers (and a few save to files to help demonstrate the difference in file size). The key part is the TBitmapSurface and TBitmapCodecManager.

procedure TForm1.Button1Click(Sender: TObject);
  Stream: TMemoryStream;
  Surf: TBitmapSurface;
  aFile : string;
  ImageViewer1.Bitmap :=

  // Comparison save file as BMP 
  aFile := 'c:\test\myImage';
  Stream := TMemoryStream.Create;
    Stream.Position := 0;
    Surf := TBitmapSurface.Create;
      // use the codec to save Surface to stream
      if not TBitmapCodecManager.SaveToStream( 
                       '.png') then
        raise EBitmapSavingFailed.Create(
              'Error saving Bitmap to png');
    // do something with the png stream image
    Stream.Position := 0;

    // comparison output as PNG from the stream
    Stream.Position := 0;

Now, you can obviously save as a ‘PNG’ from the bitmap, but the above code shows how to avoid saving a file  to disk unless you really need to.

Happy coding!

Setting the default XML DOM in Delphi XE7

Managing the default XML DOM

Scanning the whats new in RAD Studio XE7 (Delphi, C++ Builder) it covers the introduction of a new XML Dom (OmniXML) meaning there are now 3 XML DOMS to choose from when setting the default XML DOM.

  • ADOM
  • OmniXML

The XML Dom defines which engine in essence is used to work with your XML documents.

Should I read on? Well if your using SOAP or XML in your projects, then this could make a difference to your speed and performance of the applications, and it takes seconds to implement.

Continue reading Setting the default XML DOM in Delphi XE7

Castalia IDE Plugin Free with XE7 (ends soon)

One present I’m going to be playing with over Christmas is the Castalia IDE plug in. If you want to know why, check out the 10 minute demo here of some of the cool refactoring and IDE tools that Castalia provides.

Don’t forget if your latest version is XE2 your last chance to qualify for upgrade pricing is now.  http://www.embarcadero.com/radoffer

The Embarcadero sales teams will be in between Christmas and new year so there is still time to code smarter in the new year.