How to Draw Text onto an Image using Java

Cloudmersive
2 min readSep 14, 2022

--

Within most major photo editing applications, adding customized/formatted text onto any given image is only a few button-clicks away. When we want to avoid paying for expensive applications, however, and accomplish the same operations at scale for free, we can instead rely on different programmatic means — this time by using an API solution to solve the problem. With our Draw Text API, you can draw custom text onto any input image, specifying its font type & size, color, location, height & width. All you need to do is follow instructions below to install the API client & structure your API call using Java code examples from our API console. Other than that, you’ll just need to register a free account on our website to get your API key to authenticate the service (this account comes with 800 API calls per month).

The below request parameter example (JSON) shows how your Draw Text argument should be structured. We’ll pass this through the function later on:

{
"BaseImageBytes": "string",
"BaseImageUrl": "string",
"TextToDraw": [
{
"Text": "string",
"FontFamilyName": "string",
"FontSize": 0,
"Color": "string",
"X": 0,
"Y": 0,
"Width": 0,
"Height": 0
}
]
}

Before we get to that, though, let’s start by installing the API client. First add a reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then add one to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation finished, include the imports at the top of your file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditApi;

Lastly, call the function, passing through our structured argument from the first step & authenticating our API key:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditApi apiInstance = new EditApi();
DrawTextRequest request = new DrawTextRequest(); // DrawTextRequest | Draw text parameters
try {
byte[] result = apiInstance.editDrawText(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditApi#editDrawText");
e.printStackTrace();
}

With all your arguments properly configured, there’s nothing left to do but run it & test it. Simple as that!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet