用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);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值