This topic shows you how to use Content Moderation SDK for .NET to call the optical character recognition (OCR) operation and detect text in images.

Prerequisites

The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
Note You must use the required versions of the dependencies described in the Installation topic. Otherwise, subsequent operation calls fail.

Submit synchronous OCR tasks

Interface Description Supported region
ImageSyncScanRequest Submits synchronous OCR tasks to recognize text in images. Set the scenes parameter to ocr.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
  • ap-southeast-5: Indonesia (Jakarta)
Sample code
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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey 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";

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "ID of the image to be moderated");
            //task1.Add("url", "URL of the image to be moderated");
            task1.Add("url", "http://example.com/xx.jpg");

            // Example: If you moderate the image of the front side of an ID card, specify id-card-front. 
            Dictionary<string, object> cardExtras = new Dictionary<string, object>();
            cardExtras.Add("card", "id-card-front");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: the moderation scenarios. 
            httpBody.Add("scenes", new List<string> { "ocr" });
            httpBody.Add("bizType", "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("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}