欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#編寫一個接受圖片流的OCR識別接口

 更新時間:2024年03月08日 11:41:59   作者:搬磚的詩人Z  
這篇文章主要為大家詳細介紹了如何使用C#寫一個接受圖片流的OCR識別接口,以及測試用例調(diào)用接口,感興趣的小伙伴可以跟隨小編一起學習一下

示例代碼

using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;

namespace MyAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OCRController : ControllerBase
    {
        [HttpPost("OCR")]
        public async Task<IActionResult> OCRTextAsync()
        {
            try
            {
                // 檢查是否有文件被上傳
                if (Request.Form.Files.Count > 0)
                {
                    // 獲取上傳的文件
                    var file = Request.Form.Files[0];

                    // 將文件轉(zhuǎn)換為字節(jié)數(shù)組
                    using (var memoryStream = new MemoryStream())
                    {
                        await file.CopyToAsync(memoryStream);
                        byte[] imageData = memoryStream.ToArray();

                        return Ok("OCR result");
                    }
                }
                else
                {
                    return BadRequest("No file uploaded");
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Internal server error: {ex}");
            }
        }
    }
}

調(diào)用示例代碼:

    private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "請選擇圖片";
            // fileDialog.Filter = "所有文件(*pdf*)|*.jpg*"; //設(shè)置要選擇的文件的類型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;//返回文件的完整路徑     
                                                  // textBox1.Text = file;
                pictureBox1.Image = Image.FromFile(file);

                MemoryStream stream = new MemoryStream();
                pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imagedata = stream.GetBuffer();
                await TestApiAsync(imagedata);
            }

        }
        catch (Exception ex)
        {

            MessageBox.Show("請選擇正確的圖片");
        }
    }

    private async Task TestApiAsync(byte[] imageData)
    {
        string url = "http://localhost:5000/api/OCR/OCR";

        // 創(chuàng)建 HttpClient 實例
        using (var httpClient = new HttpClient())
        using (var formData = new MultipartFormDataContent())
        {
            try
            {
                formData.Add(new ByteArrayContent(imageData), "file", "image.jpg");

                // 發(fā)送 POST 請求到接口
                HttpResponseMessage response = await httpClient.PostAsync(url, formData);

                // 檢查響應(yīng)是否成功
                if (response.IsSuccessStatusCode)
                {
                    // 讀取響應(yīng)內(nèi)容
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("OCR 結(jié)果:" + result);
                }
                else
                {
                    Console.WriteLine("請求失敗,狀態(tài)碼:" + response.StatusCode);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("發(fā)生異常:" + ex.Message);
            }
        }
    }

知識拓展

下面小編為大家整理了使用C#實現(xiàn)OCR本地圖片識別文字的示例代碼,希望對大家有所幫助

文字識別

 var apiKey = "F--------------------X"; //自己申請的key
  var secretKey = "H----------------------"; //自己申請的key
  
  Ocr client = new Ocr(apiKey, secretKey)
    {
            Timeout = 30000//延時時間
   };
//本地圖片識別文字
var image = File.ReadAllBytes("圖片文件路徑");
	// 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片,可能會拋出網(wǎng)絡(luò)等異常,請使用try/catch捕獲
	var result = client.GeneralBasic(image);
	Console.WriteLine(result);
	// 如果有可選參數(shù)
	var options = new Dictionary<string, object>{
	    {"language_type", "CHN_ENG"},
	    {"detect_direction", "true"},
	    {"detect_language", "true"},
	    {"probability", "true"}
	};
	// 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為本地圖片
	result = client.GeneralBasic(image, options);

網(wǎng)絡(luò)圖片識別

//網(wǎng)絡(luò)圖片識別
 var url = "https//www.x.com/sample.jpg";
	// 調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片,可能會拋出網(wǎng)絡(luò)等異常,請使用try/catch捕獲
	var result = client.GeneralBasicUrl(url);
	Console.WriteLine(result);
	// 如果有可選參數(shù)
	var options = new Dictionary<string, object>{
	    {"language_type", "CHN_ENG"},
	    {"detect_direction", "true"},
	    {"detect_language", "true"},
	    {"probability", "true"}
	};
	// 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片
	result = client.GeneralBasicUrl(url, options);

到此這篇關(guān)于基于C#編寫一個接受圖片流的OCR識別接口的文章就介紹到這了,更多相關(guān)C# OCR識別接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#中 paint()與Onpaint()的區(qū)別

    C#中 paint()與Onpaint()的區(qū)別

    paint是事件onpaint方法onpaint方法是調(diào)用paint事件的,用哪一個,效果是一樣,就看那一個方便了內(nèi)部是這樣實現(xiàn)的:
    2013-04-04
  • C#基本概念列舉詳解

    C#基本概念列舉詳解

    這篇文章主要介紹了C#基本概念列舉,需要的朋友可以參考下
    2014-02-02
  • VB.NET中Caching的使用方法

    VB.NET中Caching的使用方法

    Caching緩存,就是將一些生成代價比較大的常用數(shù)據(jù),保存起來重用。一般數(shù)據(jù)都保存在內(nèi)存中,因為從內(nèi)存中讀取數(shù)據(jù)比從數(shù)據(jù)庫等其他地方要快。
    2013-04-04
  • C#學習筆記——基本語法

    C#學習筆記——基本語法

    本文給大家詳細介紹了C#的基本語法知識以及一些基礎(chǔ)知識的匯總,非常的簡單基礎(chǔ),有需要的小伙伴可以參考下
    2017-02-02
  • C#中String和StringBuilder的簡介與區(qū)別

    C#中String和StringBuilder的簡介與區(qū)別

    今天小編就為大家分享一篇關(guān)于C#中String和StringBuilder的簡介與區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • C#中的TemplateMethod模式問題分析

    C#中的TemplateMethod模式問題分析

    這篇文章主要介紹了C#中的TemplateMethod模式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • c#操作xml文件示例

    c#操作xml文件示例

    對于XML讀寫操作,項目中經(jīng)常要用到,之前木有好好總結(jié)過,例如LINQ TO XML也用過,這次無意發(fā)現(xiàn)XPATH對于XML的查詢極為方便,索性把XML的操作總結(jié)以便后續(xù)方便使用
    2014-03-03
  • C#泛型集合類System.Collections.Generic

    C#泛型集合類System.Collections.Generic

    這篇文章介紹了C#中的泛型集合類System.Collections.Generic,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • c#中多線程間的同步示例詳解

    c#中多線程間的同步示例詳解

    使用線程時最頭痛的就是共享資源的同步問題,處理不好會得到錯誤的結(jié)果,所以下面這篇文章主要給大家介紹了關(guān)于c#中多線程間同步的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • C#在LINQ中使用GroupBy

    C#在LINQ中使用GroupBy

    這篇文章主要介紹了C#在LINQ中如何使用GroupBy,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下
    2020-08-08

最新評論