基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口
更新時(shí)間:2024年03月08日 11:41:59 作者:搬磚的詩人Z
這篇文章主要為大家詳細(xì)介紹了如何使用C#寫一個(gè)接受圖片流的OCR識(shí)別接口,以及測(cè)試用例調(diào)用接口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
示例代碼
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 = "請(qǐng)選擇圖片";
// 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("請(qǐng)選擇正確的圖片");
}
}
private async Task TestApiAsync(byte[] imageData)
{
string url = "http://localhost:5000/api/OCR/OCR";
// 創(chuàng)建 HttpClient 實(shí)例
using (var httpClient = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
try
{
formData.Add(new ByteArrayContent(imageData), "file", "image.jpg");
// 發(fā)送 POST 請(qǐng)求到接口
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("請(qǐng)求失敗,狀態(tài)碼:" + response.StatusCode);
}
}
catch (Exception ex)
{
Console.WriteLine("發(fā)生異常:" + ex.Message);
}
}
}
知識(shí)拓展
下面小編為大家整理了使用C#實(shí)現(xiàn)OCR本地圖片識(shí)別文字的示例代碼,希望對(duì)大家有所幫助
文字識(shí)別
var apiKey = "F--------------------X"; //自己申請(qǐng)的key
var secretKey = "H----------------------"; //自己申請(qǐng)的key
Ocr client = new Ocr(apiKey, secretKey)
{
Timeout = 30000//延時(shí)時(shí)間
};
//本地圖片識(shí)別文字
var image = File.ReadAllBytes("圖片文件路徑");
// 調(diào)用通用文字識(shí)別, 圖片參數(shù)為本地圖片,可能會(huì)拋出網(wǎng)絡(luò)等異常,請(qǐng)使用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í)別, 圖片參數(shù)為本地圖片
result = client.GeneralBasic(image, options);
網(wǎng)絡(luò)圖片識(shí)別
//網(wǎng)絡(luò)圖片識(shí)別
var url = "https//www.x.com/sample.jpg";
// 調(diào)用通用文字識(shí)別, 圖片參數(shù)為遠(yuǎn)程url圖片,可能會(huì)拋出網(wǎng)絡(luò)等異常,請(qǐng)使用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í)別, 圖片參數(shù)為遠(yuǎn)程url圖片
result = client.GeneralBasicUrl(url, options);
到此這篇關(guān)于基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口的文章就介紹到這了,更多相關(guān)C# OCR識(shí)別接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中String和StringBuilder的簡(jiǎn)介與區(qū)別
今天小編就為大家分享一篇關(guān)于C#中String和StringBuilder的簡(jiǎn)介與區(qū)別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
C#泛型集合類System.Collections.Generic
這篇文章介紹了C#中的泛型集合類System.Collections.Generic,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

