基于C#编写一个接受图片流的OCR识别接口
作者:搬砖的诗人Z
这篇文章主要为大家详细介绍了如何使用C#写一个接受图片流的OCR识别接口,以及测试用例调用接口,感兴趣的小伙伴可以跟随小编一起学习一下
示例代码
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]; // 将文件转换为字节数组 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}"); } } } }
调用示例代码:
private async void button1_Click(object sender, EventArgs e) { try { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择图片"; // fileDialog.Filter = "所有文件(*pdf*)|*.jpg*"; //设置要选择的文件的类型 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"; // 创建 HttpClient 实例 using (var httpClient = new HttpClient()) using (var formData = new MultipartFormDataContent()) { try { formData.Add(new ByteArrayContent(imageData), "file", "image.jpg"); // 发送 POST 请求到接口 HttpResponseMessage response = await httpClient.PostAsync(url, formData); // 检查响应是否成功 if (response.IsSuccessStatusCode) { // 读取响应内容 string result = await response.Content.ReadAsStringAsync(); Console.WriteLine("OCR 结果:" + result); } else { Console.WriteLine("请求失败,状态码:" + response.StatusCode); } } catch (Exception ex) { Console.WriteLine("发生异常:" + ex.Message); } } }
知识拓展
下面小编为大家整理了使用C#实现OCR本地图片识别文字的示例代码,希望对大家有所帮助
文字识别
var apiKey = "F--------------------X"; //自己申请的key var secretKey = "H----------------------"; //自己申请的key Ocr client = new Ocr(apiKey, secretKey) { Timeout = 30000//延时时间 }; //本地图片识别文字 var image = File.ReadAllBytes("图片文件路径"); // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 var result = client.GeneralBasic(image); Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary<string, object>{ {"language_type", "CHN_ENG"}, {"detect_direction", "true"}, {"detect_language", "true"}, {"probability", "true"} }; // 带参数调用通用文字识别, 图片参数为本地图片 result = client.GeneralBasic(image, options);
网络图片识别
//网络图片识别 var url = "https//www.x.com/sample.jpg"; // 调用通用文字识别, 图片参数为远程url图片,可能会抛出网络等异常,请使用try/catch捕获 var result = client.GeneralBasicUrl(url); Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary<string, object>{ {"language_type", "CHN_ENG"}, {"detect_direction", "true"}, {"detect_language", "true"}, {"probability", "true"} }; // 带参数调用通用文字识别, 图片参数为远程url图片 result = client.GeneralBasicUrl(url, options);
到此这篇关于基于C#编写一个接受图片流的OCR识别接口的文章就介绍到这了,更多相关C# OCR识别接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!