我将Azure Vision API用于OCR。 MVC中的示例代码可以正常工作,但是当我在按钮上单击时在Asp.net中使用相同的代码时,它无法正常工作。没有给出任何回应或给出错误。
响应=等待client.PostAsync(uri,content); //没有回应
响应=等待客户端。PostAsync(uri,content).ConfigureAwait(false); //错误:找不到资源
事件:
protected void btnScanCheque_Click(object sender, EventArgs e)
{
try
{
Task<string> task = imgScan.GetOCRDetails();
}
catch (Exception ex)
{
}
}
功能:
public async Task<string> GetOCRDetails()
{
string imageFilePath = @"C:\Projects\OCR Test\ReadImage\Uploads\Cheque_1.JPG";
var errors = new List<string>();
string extractedResult = "";
ImageInfoViewModel responeData = new ImageInfoViewModel();
try
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters.
string requestParameters = "language=unk&detectOrientation=true";
// Assemble the URI for the REST API Call.
string uri = endPoint + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Make the REST API call.
response = await client.PostAsync(uri, content);
}
// Get the JSON response.
string result = await response.Content.ReadAsStringAsync();
//If it is success it will execute further process.
if (response.IsSuccessStatusCode)
{
// The JSON response mapped into respective view model.
responeData = JsonConvert.DeserializeObject<ImageInfoViewModel>(result,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg)
{
errors.Add(earg.ErrorContext.Member.ToString());
earg.ErrorContext.Handled = true;
}
}
);
var linesCount = responeData.regions[0].lines.Count;
for (int i = 0; i < linesCount; i++)
{
var wordsCount = responeData.regions[0].lines[i].words.Count;
for (int j = 0; j < wordsCount; j++)
{
//Appending all the lines content into one.
extractedResult += responeData.regions[0].lines[i].words[j].text + " ";
}
extractedResult += Environment.NewLine;
}
}
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
return extractedResult;
}
答案 0 :(得分:1)
将事件更改为与GetOCRDetails方法上的await保持同步应该有用。