Tutorial: Implementing Font Identification with SDK
Learning Objectives
In this tutorial, you’ll learn:
- How to set up the Aspose.OCR Cloud SDK in your environment
- How to implement font identification using the SDK
- How to handle font identification results programmatically
- How to integrate font detection features into your applications
Prerequisites
- Completed the previous tutorials on font identification
- Basic programming knowledge in your chosen language
- Development environment set up for your programming language
- Active Aspose Cloud credentials (Client ID and Client Secret)
Why Use the SDK?
While you can interact directly with the Aspose.OCR Cloud REST API as shown in previous tutorials, using the SDK offers several advantages:
- Simplified authentication - The SDK handles token generation automatically
- Type safety - Strongly-typed methods and objects for your language
- Error handling - Built-in error handling and validation
- Convenience methods - Common operations wrapped in simple method calls
- Reduced development time - No need to manually construct API requests
Step 1: Install the SDK
Aspose.OCR Cloud provides SDKs for multiple programming languages. Let’s see how to install the SDK in different environments:
For .NET Developers
Install the NuGet package:
dotnet add package Aspose.OCR-Cloud
Or use the NuGet Package Manager in Visual Studio and search for “Aspose.OCR-Cloud”.
Step 2: Set Up Font Identification Client
Once you have the SDK installed, you need to initialize it with your credentials. Here’s how to do it:
In .NET
using Aspose.OCR.Cloud.SDK.Api;
using Aspose.OCR.Cloud.SDK.Model;
// Initialize the font identification API client
IdentifyFontApi fontIdentificationApi = new IdentifyFontApi("<Client Id>", "<Client Secret>");
Step 3: Prepare and Submit Image
With the SDK initialized, you can now prepare your image and submit it for font identification. The SDK handles all the complexity of encoding, sending the request, and parsing the response.
In .NET
// Read the image file
byte[] image = File.ReadAllBytes("sample-image.png");
// Configure recognition settings
OCRSettingsRecognizeFont recognitionSettings = new OCRSettingsRecognizeFont {
Language = "English",
MakeSkewCorrect = true,
MakeContrastCorrection = true,
ResultType = ResultType.Text
};
// Create the request body
OCRRecognizeFontBody requestBody = new OCRRecognizeFontBody(image, recognitionSettings);
// Send the image for font identification
string taskId = fontIdentificationApi.PostIdentifyFont(requestBody);
Console.WriteLine($"Font identification task submitted with ID: {taskId}");
Step 4: Retrieve and Process the Results
After submitting the image, you can use the SDK to fetch the results. The SDK handles the polling automatically, making it much simpler than the direct API approach.
In .NET
// Fetch the font identification results
OCRResponse result = fontIdentificationApi.GetIdentifyFont(taskId);
// Check if the task is completed
if (result.TaskStatus == TaskStatus.Completed)
{
// Decode the Base64 result
string base64Data = Encoding.UTF8.GetString(result.Results[0].Data);
Console.WriteLine("Font identification completed successfully.");
Console.WriteLine($"Result: {base64Data}");
// Further process or display the results
// Parse the JSON to get font information
// var fontInfo = JsonConvert.DeserializeObject(base64Data);
}
else if (result.TaskStatus == TaskStatus.Error)
{
Console.WriteLine($"Error during font identification: {result.Error.Messages}");
}
else
{
Console.WriteLine($"Task is in {result.TaskStatus} state.");
}
Step 5: Complete SDK Implementation Example
Let’s put everything together in a complete C# application:
using Aspose.OCR.Cloud.SDK.Api;
using Aspose.OCR.Cloud.SDK.Model;
using System;
using System.IO;
using System.Text;
namespace FontIdentificationDemo
{
internal class Program
{
static void Main(string[] args)
{
try
{
// Set your Aspose Cloud credentials
string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";
// Initialize the font identification API client
IdentifyFontApi fontIdentificationApi = new IdentifyFontApi(clientId, clientSecret);
// Path to the image file
string imagePath = "sample-image.png";
Console.WriteLine($"Processing image: {imagePath}");
// Read the image file
byte[] image = File.ReadAllBytes(imagePath);
// Configure recognition settings
OCRSettingsRecognizeFont recognitionSettings = new OCRSettingsRecognizeFont {
Language = "English",
MakeSkewCorrect = true,
MakeContrastCorrection = true,
ResultType = ResultType.Text
};
// Create the request body
OCRRecognizeFontBody requestBody = new OCRRecognizeFontBody(image, recognitionSettings);
// Send the image for font identification
Console.WriteLine("Submitting image for font identification...");
string taskId = fontIdentificationApi.PostIdentifyFont(requestBody);
Console.WriteLine($"Task ID: {taskId}");
// Wait for the task to complete
Console.WriteLine("Waiting for results...");
bool completed = false;
OCRResponse result = null;
int maxRetries = 10;
int retries = 0;
while (!completed && retries < maxRetries)
{
// Fetch the font identification results
result = fontIdentificationApi.GetIdentifyFont(taskId);
if (result.TaskStatus == TaskStatus.Completed ||
result.TaskStatus == TaskStatus.Error ||
result.TaskStatus == TaskStatus.NotExist)
{
completed = true;
}
else
{
Console.WriteLine($"Task status: {result.TaskStatus}. Waiting...");
retries++;
// Wait for 2 seconds before next retry
System.Threading.Thread.Sleep(2000);
}
}
// Process the results
if (result != null && result.TaskStatus == TaskStatus.Completed)
{
// Decode the Base64 result
string base64Data = Encoding.UTF8.GetString(result.Results[0].Data);
Console.WriteLine("Font identification completed successfully.");
Console.WriteLine($"Result: {base64Data}");
// You could parse JSON here to extract font information
// var fontInfo = JsonConvert.DeserializeObject(base64Data);
}
else if (result != null && result.TaskStatus == TaskStatus.Error)
{
Console.WriteLine($"Error during font identification: {result.Error.Messages}");
}
else
{
Console.WriteLine("Font identification timed out or failed.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
Advanced Use Cases
Now that you understand the basics, let’s explore some advanced scenarios for font identification using the SDK.
Processing Multiple Images
You can easily extend the code to process multiple images in batch:
// Directory containing images
string imageDirectory = "path/to/images";
string[] imageFiles = Directory.GetFiles(imageDirectory, "*.png");
foreach (string imagePath in imageFiles)
{
// Process each image (use the code from previous example)
// ...
}
Integrating Font Identification into Web Applications
For web applications, you can create an API endpoint that handles font identification:
[HttpPost("identify-font")]
public async Task<IActionResult> IdentifyFont(IFormFile imageFile)
{
if (imageFile == null || imageFile.Length == 0)
return BadRequest("No file uploaded");
try
{
// Read the image file
using var memoryStream = new MemoryStream();
await imageFile.CopyToAsync(memoryStream);
byte[] imageData = memoryStream.ToArray();
// Initialize the font identification API client
IdentifyFontApi fontIdentificationApi = new IdentifyFontApi("CLIENT_ID", "CLIENT_SECRET");
// Configure recognition settings
OCRSettingsRecognizeFont recognitionSettings = new OCRSettingsRecognizeFont {
ResultType = ResultType.Text
};
// Create request body and submit
OCRRecognizeFontBody requestBody = new OCRRecognizeFontBody(imageData, recognitionSettings);
string taskId = fontIdentificationApi.PostIdentifyFont(requestBody);
// Return the task ID to the client for polling
return Ok(new { taskId = taskId });
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
Troubleshooting Common Issues
When implementing font identification with the SDK, you might encounter these common issues:
Authentication Failures
If you’re getting authentication errors:
- Double-check your Client ID and Client Secret
- Ensure your subscription is active
- Verify your account has access to the OCR Cloud API
Task Status Never Completes
If your task seems stuck in the “Pending” or “Processing” state:
- Increase your polling timeout
- Verify the image size isn’t too large
- Check your internet connection
What You’ve Learned
In this tutorial, you’ve learned:
- How to set up and initialize the Aspose.OCR Cloud SDK
- How to submit images for font identification using the SDK
- How to retrieve and process font identification results
- How to implement polling for task completion
- How to integrate font identification into different application types
- How to troubleshoot common issues
Next Steps
Now that you’ve completed all tutorials in this series, you can:
- Review the API Reference for additional features
- Explore other capabilities of Aspose.OCR Cloud
- Integrate font identification into your production applications
- Combine font identification with other OCR features for comprehensive document analysis
Further Practice
- Create a simple UI for uploading images and displaying font identification results
- Build a tool that compares detected fonts with a font database
- Develop a batch processing utility for identifying fonts in multiple images
- Create a service that suggests similar fonts based on the detection results