釋放 AI 的強大力量

100 萬免費代幣

88% 價格優惠

立即啟用
本文由簡體中文內容自動轉碼而成。阿里雲不保證此自動轉碼的準確性、完整性及時效性。本文内容請以簡體中文版本為準。

圖片OCR識別

更新時間:2024-07-06 00:11

本文介紹了如何使用.NET SDK圖片OCR介面,識別圖片中的文字資訊。

前提條件

已安裝.NET依賴。關於安裝.NET依賴的具體操作,請參見安裝.NET依賴

說明

請一定按照安裝.NET依賴頁面中的版本安裝,否則會導致調用失敗。

提交圖片同步檢測任務

介面

描述

支援的Region

介面

描述

支援的Region

ImageSyncScanRequest

提交圖片OCR同步識別任務,對圖片中的文字進行識別(scene=ocr)。

  • 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)
        {
            /**
             * 常見擷取環境變數方式:
             *     擷取RAM使用者AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     擷取RAM使用者AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建議從環境變數中擷取RAM使用者AccessKey ID",
                    "建議從環境變數中擷取RAM使用者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");
            //task1.Add("url", "待檢測圖片連結地址");
            task1.Add("url", "http://example.com/xx.jpg");

            // 樣本:身份證正面識別。
            Dictionary<string, object> cardExtras = new Dictionary<string, object>();
            cardExtras.Add("card", "id-card-front");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes:檢測情境。
            httpBody.Add("scenes", new List<string> { "ocr" });
            httpBody.Add("bizType", "業務情境");
            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);
            }
        }
    }
}
  • 本頁導讀 (0, M)
  • 前提條件
  • 提交圖片同步檢測任務
文檔反饋
phone 聯絡我們

立即和Alibaba Cloud在線服務人員進行交談,獲取您想了解的產品信息以及最新折扣。

alicare alicarealicarealicare