首页 > 解决方案 > Azure OCR 识别PrintedTextInStreamAsync 无效图像

问题描述

我正在尝试在 Xamarin Forms 中使用 Azure OCR API。这是代码的重要部分。我正在使用 ComputerVisionClient .net 客户端

await CrossMedia.Current.Initialize();

  if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  {
    return;
  }

  var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
  {
    SaveToAlbum = false
  });

  Img = new Image
  {
    Source = ImageSource.FromStream(() =>
    {
      var stream = file.GetStream();
      return stream;
    }),
  };

  using (var photoStream = file.GetStream())
  {
    var text = await _client.RecognizePrintedTextInStreamAsync(true, photoStream);
  }

Img 绑定到视图上的图像,当我拍照时它会显示出来,所以肯定有图像。但是,RecognizePrintedTextInStreamAsync 返回“System.IO.FileNotFoundException:'Invalid Image'”,我不知道为什么。有任何想法吗?

编辑。添加了 _client 代码

public static ComputerVisionClient Authenticate(string endpoint, string key)
{
    ComputerVisionClient client =
        new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
        { Endpoint = endpoint };
    return client;
}

从我的构造函数调用并设置为全局变量

_client = Authenticate(endpoint, subscriptionKey);

谢谢

标签: xamarinazure-cognitive-services

解决方案


推荐阅读