Recognize text in images using the Content Moderation .NET SDK. The ImageSyncScanRequest operation supports both general images and structured cards such as ID cards.
Use cases
Content filtering: Detect and filter text embedded in user-uploaded images before publishing
ID verification: Extract text from structured cards such as ID cards for identity workflows
Visual search: Index text in images to enable search across image libraries
Prerequisites
Before you begin, make sure you have:
The Content Moderation SDK for .NET installed. For installation instructions, see Installation
The required .NET version specified in the Installation topic — using an unsupported version causes operation calls to fail
How it works
Submit a synchronous scan request with the scenes parameter set to ocr. The operation returns recognized text synchronously.
Input: Public HTTP or HTTPS image URLs only. Local files and binary data are not supported. URLs must not exceed 2,048 characters.
For the full parameter reference, see Image OCR detection API.
Recognize text in an image
The following example submits a synchronous OCR task for a single image URL. It also shows how to pass the extras parameter to recognize text on a structured card (in this case, the front of an ID card).
Supported regions: cn-shanghai, cn-beijing, cn-shenzhen, ap-southeast-1
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Load credentials from environment variables.
// Store your AccessKey ID in ALIBABA_CLOUD_ACCESS_KEY_ID
// and your AccessKey secret in ALIBABA_CLOUD_ACCESS_KEY_SECRET.
// Never hardcode credentials in source code.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest request = new ImageSyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// Define the image to scan.
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "unique-id-for-this-image");
task1.Add("url", "http://example.com/xx.jpg");
// Optional: specify a structured card type to extract card-specific fields.
// Use "id-card-front" to recognize the front of an ID card.
Dictionary<string, object> cardExtras = new Dictionary<string, object>();
cardExtras.Add("card", "id-card-front");
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "ocr" });
httpBody.Add("bizType", "<your-business-scenario>");
httpBody.Add("extras", cardExtras);
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
"utf-8",
FormatType.JSON);
try
{
ImageSyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}Replace the following placeholders before running the code:
| Placeholder | Description |
|---|---|
ALIBABA_CLOUD_ACCESS_KEY_ID | Environment variable holding the AccessKey ID of a Resource Access Management (RAM) user |
ALIBABA_CLOUD_ACCESS_KEY_SECRET | Environment variable holding the AccessKey secret of the RAM user |
unique-id-for-this-image | A unique identifier for the image in this request |
http://example.com/xx.jpg | The public HTTP or HTTPS URL of the image to scan |
<your-business-scenario> | Your business scenario identifier, configured in the Content Moderation console |
Store credentials in environment variables, not in source code. Never commit AccessKey values to version control.
What's next
Image OCR detection API — full parameter reference and response schema
Installation — SDK installation and version requirements