即時的反向圖像搜尋與校正傾斜圖片是開發視覺搜尋功能的開發者常見的挑戰。Aspose.CAD Cloud SDK for .NET 提供強大的函式庫,簡化了在 C# 中執行反向圖像搜尋與圖像去斜的流程。在本指南中,您將看到如何對 PNG 進行去斜、呼叫 Bing Visual Search 進行反向查詢,並使用簡潔的程式碼與 cURL 範例整合整個工作流程。

開始之前:先決條件與安裝

要遵循本教程,您需要:

  • .NET 6.0 或更新版本,以及類似 Visual Studio 2022 的 IDE。
  • 具有 API 金鑰和客戶端密鑰的 Aspose Cloud 帳戶。
  • 存取 Bing Visual Search API(需要訂閱金鑰)。
  • 將輸入圖像檔案(input.png)放置於專案資料夾中。

透過 NuGet 安裝程式庫:

dotnet add package Aspose.cad-Cloud

從下載頁面下載最新套件。安裝完成後,您即可開始編寫工作流程的程式碼。

逐步構建:反向圖像搜索與圖像去斜在 .NET 中

實作分為五個邏輯步驟。每個步驟僅顯示從完整範例中提取的相關程式碼。

步驟 1:載入來源圖像

將 PNG 檔案讀取為位元組陣列,並建立可供庫使用的記憶體串流。

string inputImagePath = "input.png";
byte[] inputBytes = File.ReadAllBytes(inputImagePath);
using var inputStream = new MemoryStream(inputBytes);

第 2 步:設定 Aspose.CAD 雲端函式庫

使用您的 API 金鑰和基礎 URL 建立配置物件,然後實例化 ImageApi。

var config = new Configuration
{
    ApiKey = "YOUR_ASPOSE_CAD_CLOUD_API_KEY",
    BasePath = "https://api.aspose.cloud"
};
var imageApi = new ImageApi(config);

欲了解更多詳情,請參閱 API 參考。

第 3 步:使用庫校正圖像

使用圖像流建立 DeskewImageRequest,並呼叫去斜操作。

var deskewRequest = new DeskewImageRequest
{
    ImageData = inputStream
    // Optional: Angle, BackgroundColor can be set here
};
byte[] deskewedBytes = await imageApi.PostImageDeskewAsync(deskewRequest);

步驟 4:儲存去斜影像

將返回的位元組陣列寫入新檔案。

string deskewedImagePath = "deskewed.png";
File.WriteAllBytes(deskewedImagePath, deskewedBytes);
Console.WriteLine($"Deskewed image saved to {deskewedImagePath}");

步驟 5:透過 Bing Visual Search API 執行反向圖像搜尋

建立一個 HttpClient,加入訂閱金鑰標頭,並將原始圖像發佈至 Bing。

string bingSubscriptionKey = "YOUR_BING_VISUAL_SEARCH_SUBSCRIPTION_KEY";
string bingEndpoint = "https://api.bing.microsoft.com/v7.0/images/visualsearch";

using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingSubscriptionKey);

using var multipartContent = new MultipartFormDataContent();
var imageContent = new ByteArrayContent(inputBytes);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
multipartContent.Add(imageContent, "image", Path.GetFileName(inputImagePath));

HttpResponseMessage response = await httpClient.PostAsync(bingEndpoint, multipartContent);
string jsonResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("Reverse Image Search Result:");
Console.WriteLine(jsonResult);

按照這些步驟,您就可以在 C# 中完成完整的反向圖像搜索和圖像校正工作流程。

完整的逆向影像搜尋與影像校正自動化範例

以下範例示範如何將所有組件結合成一個單一的控制台應用程式。

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Aspose.CAD.Cloud.Sdk.Api;
using Aspose.CAD.Cloud.Sdk.Client;
using Aspose.CAD.Cloud.Sdk.Model;

namespace ReverseImageSearchDeskew
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Aspose.CAD Cloud configuration
            var config = new Configuration
            {
                ApiKey = "YOUR_ASPOSE_CAD_CLOUD_API_KEY",
                BasePath = "https://api.aspose.cloud"
            };
            var imageApi = new ImageApi(config);

// File paths
            string inputImagePath = "input.png";
            string deskewedImagePath = "deskewed.png";

// Load input image
            byte[] inputBytes = File.ReadAllBytes(inputImagePath);
            using var inputStream = new MemoryStream(inputBytes);

// Deskew the image using Aspose.CAD Cloud
            var deskewRequest = new DeskewImageRequest
            {
                ImageData = inputStream
                // Additional optional parameters (e.g., Angle, BackgroundColor) can be set here
            };
            byte[] deskewedBytes = await imageApi.PostImageDeskewAsync(deskewRequest);
            File.WriteAllBytes(deskewedImagePath, deskewedBytes);
            Console.WriteLine($"Deskewed image saved to {deskewedImagePath}");

// Reverse image search using Bing Visual Search API
            string bingSubscriptionKey = "YOUR_BING_VISUAL_SEARCH_SUBSCRIPTION_KEY";
            string bingEndpoint = "https://api.bing.microsoft.com/v7.0/images/visualsearch";

using var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingSubscriptionKey);

using var multipartContent = new MultipartFormDataContent();
            var imageContent = new ByteArrayContent(inputBytes);
            imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
            multipartContent.Add(imageContent, "image", Path.GetFileName(inputImagePath));

HttpResponseMessage response = await httpClient.PostAsync(bingEndpoint, multipartContent);
            string jsonResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("Reverse Image Search Result:");
            Console.WriteLine(jsonResult);
        }
    }

// Request model for deskew operation (matches Aspose.CAD Cloud SDK expectations)
    public class DeskewImageRequest
    {
        public Stream ImageData { get; set; }
        // Optional: public double? Angle { get; set; }
        // Optional: public string BackgroundColor { get; set; }
    }
}

注意: 此程式碼範例示範了核心功能。在您的專案中使用之前,請確保更新任何檔案路徑和設定值以符合實際環境,驗證所有必要的相依項目已正確安裝,並在開發環境中徹底測試。若遇到任何問題,請參閱官方文件或聯繫支援團隊以獲得協助。

使用 cURL 透過 REST API 執行相同操作

如果您偏好純粹的 REST 方法,則可以使用 cURL 命令執行相同的工作流程。

1. 驗證並取得存取權杖

curl -X POST "https://api.aspose.cloud/connect/token" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"

2. 上傳來源 PNG 並請求去斜

curl -X PUT "https://api.aspose.cloud/v3.0/cad/deskew" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary @input.png \
     -o deskewed.png

3. 呼叫 Bing Visual Search 進行反向圖像搜尋

curl -X POST "https://api.bing.microsoft.com/v7.0/images/visualsearch" \
     -H "Ocp-Apim-Subscription-Key: YOUR_BING_VISUAL_SEARCH_SUBSCRIPTION_KEY" \
     -F "image=@input.png;type=application/octet-stream"

這些指令執行與 C# 程式碼中顯示的相同步驟,讓您能將此功能整合到任何能執行 shell 腳本的環境中。

如需其他參數和回應格式,請參閱官方 API 文件。

結論

在 C# 中實作反向圖像搜尋和圖像去斜變得相當簡單,只需使用 Aspose.CAD Cloud SDK for .NET。該函式庫負責圖像前處理,而 Bing Visual Search 提供強大的反向查詢功能。遵循上述步驟,即可建立一個穩健的視覺搜尋管線,在查詢前校正傾斜的輸入。請記得取得有效的授權以供正式環境使用;產品頁面上提供價格資訊,您亦可從 臨時授權頁面 取得臨時授權。有了程式碼和 cURL 範例,您即可將此功能整合到您的應用程式中。

常見問題

  • 在 C# 應用程式中,反向圖像搜索和圖像去斜能達成什麼目的?
    它允許自動校正上傳圖片的方向,然後使用外部搜索服務找到視覺上相似的項目,提升使用者體驗與搜索精確度。

  • 我可以在 PNG 之外使用其他圖像格式嗎?
    是的,該庫支援常見的光柵格式,例如 JPG 和 BMP。只需在程式碼中更改檔案副檔名。

  • 我需要自行托管任何服務來執行此程式碼嗎?
    不,Aspose.CAD Cloud SDK for .NET 是基於雲端的函式庫,所有處理都在 Aspose 伺服器上完成。您只需要網際網路連線和有效的憑證。

  • 如何有效處理大量圖像批次?
    以非同步方式處理圖像,快取去斜結果,並在多個 API 呼叫中重複使用相同的存取權杖。這可降低延遲與 API 開銷。

閱讀更多