// Matt Content Moderator Part 7 public static void CallContentModeratorAPIOCR() { try { // Get the file name Console.WriteLine("Please enter a file name:"); var imagePath = Console.ReadLine(); Console.WriteLine("Please enter a content type: (image/jpeg) is default"); var contentType = "image/jpeg"; var line = Console.ReadLine(); if (!String.IsNullOrEmpty(line)) { contentType = line; } if (File.Exists(imagePath)) { var fileName = new FileInfo(imagePath).Name; Console.WriteLine("Getting image Insights for image: " + fileName); Console.WriteLine(); var query = new ContentModeratorOCRQuery(); query.ImagePath = imagePath; query.ContentType = contentType; query.Endpoint = Constants.CONTENT_MODERATOR_IMAGE_URL; query.SubscriptionKey = Constants.CONTENT_MODERATOR_KEY; var result = ContentModeratorService.CallContentModeratorOCRAPI(query).Result; ExportJSON(JsonConvert.SerializeObject(result)); Console.WriteLine("\nPress Enter to exit "); Console.ReadLine(); } else { Console.WriteLine("Invalid Image Path"); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Console.ReadLine(); throw; } }
public static async Task <OCR> CallContentModeratorOCRAPI(ContentModeratorOCRQuery query) { var url = $"{query.Endpoint + "/OCR"}{query.ToQueryString()}"; using (var bingClient = new BingHttpClient(query.SubscriptionKey)) { var cancellationToken = default(CancellationToken); HttpRequestMessage _httpRequest = CreateImageRequest(query, url); var httpResponseMessage = await bingClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); if (httpResponseMessage.IsSuccessStatusCode) { var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <OCR>(responseContent)); } else { throw new InvalidOperationException("An error occurred fetching the results from the service"); } } }