Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Analyze ID with Azure Form Recognizer
/// <summary>
/// Sends an image to Form Recognizer and returns back the URL that contains the result
/// </summary>
/// <returns></returns>
private async Task<string> StartAnalyzingImage()
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
var client = _httpClientFactory.CreateClient();
// set the key
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<Azure key>");
// set the endpoint
var formRecognizerEndpoint = "https://northeurope.api.cognitive.microsoft.com/";
queryString["includeTextDetails"] = "true";
var uri = $"{formRecognizerEndpoint}/formrecognizer/v2.1-preview.3/prebuilt/idDocument/analyze?{queryString}";
var image = System.IO.File.ReadAllBytes(@"d:\license.jpg");
using var content = new ByteArrayContent(image);
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
var response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
// if the response is successful, it means that Form Recognizer started processing our image
// in order to get the result, we have to make GET requests to an URL
// that URL is provided in a header named "Operation-Location"
// below we're going to retrieve that URL and send it back to the caller of the function
var location = response.Headers.FirstOrDefault(h => h.Key == "Operation-Location");
return location.Value.FirstOrDefault();
}
return string.Empty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment