static void Main(string[] args) { string testFile = null; try { // ** Make sure an api key has been entered if (API_KEY == string.Empty) { Console.WriteLine("[ERROR] Please update the sample code and enter the API Key that came with your subscription."); return; } // ** Was a 'file to OCR' specified on the command line? if (args.Count() == 0) { Console.WriteLine("[INFO] No file to OCR specified, using default file."); testFile = Directory.GetFiles(".", "*.tif")[0]; } else { testFile = args[0]; } // ** Specify the API key associated with your subscription. Configuration.Default.AddApiKey("api_key", API_KEY); // ** Accept all SSL Certificates, this makes life under mono a lot easier. This line is not needed on Windows ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; // ** The service's host name is already set, but for debugging purposes you may want to switch between 'http' and 'https'.' Configuration.Default.ApiClient.RestClient.BaseUrl = new Uri("https://api.muhimbi.com/api"); // ** We are dealing with OCR, so instantiate the relevant class OCRApi ocrApi = new OCRApi(); // ** Read the file we wish to OCR byte[] sourceFile = File.ReadAllBytes(testFile); // ** Fill out the data for the OCR operation. OcrTextData inputData = new OcrTextData( SourceFileName: testFile, // ** The name of the file to OCR. Always include the correct extension SourceFileContent: sourceFile, // ** The content of the file to OCR Language: OcrTextData.LanguageEnum.English, // ** The document's primary language Performance: OcrTextData.PerformanceEnum.Slowbutaccurate, // ** Unless you have a good reason not to, always use the 'Slow' option. CharactersOption: OcrTextData.CharactersOptionEnum.Blacklist, // ** Any characters to black list or white list (e.g. 1234567890 to deal with numerical data) Characters: "|", // ** In this example we blacklist the '|' (pipe) as it looks similar to l (L) 1 (one) and I (capital I) Paginate: false, // ** Only 'paginate' when your documents have images that span multiple pages. X: "36", // ** By default the entire page is OCRed, optionally specify an area (in pt., 1/72nd of an inch) Y: "72", Width: "400", Height: "30", PageNumber: "1" ); // ** Carry out the OCR operation Console.WriteLine("[INFO] Running OCR..."); var response = ocrApi.OcrText(inputData); // ** Writing the recognised text to the console Console.OutputEncoding = System.Text.Encoding.UTF8; Console.Write("[INFO] Extracted text: "); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(response.OutText); Console.ResetColor(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
/// <summary> /// Extract text using OCR Extract text from a file using OCR. /// </summary> /// <exception cref="Muhimbi.PDF.Online.Client.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="inputData"></param> /// <returns>Task of OcrOperationResponse</returns> public async System.Threading.Tasks.Task <OcrOperationResponse> OcrTextAsync(OcrTextData inputData) { ApiResponse <OcrOperationResponse> localVarResponse = await OcrTextAsyncWithHttpInfo(inputData); return(localVarResponse.Data); }
/// <summary> /// Extract text using OCR Extract text from a file using OCR. /// </summary> /// <exception cref="Muhimbi.PDF.Online.Client.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="inputData"></param> /// <returns>Task of ApiResponse (OcrOperationResponse)</returns> public async System.Threading.Tasks.Task <ApiResponse <OcrOperationResponse> > OcrTextAsyncWithHttpInfo(OcrTextData inputData) { // verify the required parameter 'inputData' is set if (inputData == null) { throw new ApiException(400, "Missing required parameter 'inputData' when calling OCRApi->OcrText"); } var localVarPath = "/v1/operations/ocr_text"; var localVarPathParams = new Dictionary <String, String>(); var localVarQueryParams = new Dictionary <String, String>(); var localVarHeaderParams = new Dictionary <String, String>(Configuration.DefaultHeader); var localVarFormParams = new Dictionary <String, String>(); var localVarFileParams = new Dictionary <String, FileParameter>(); Object localVarPostBody = null; // to determine the Content-Type header String[] localVarHttpContentTypes = new String[] { "application/json" }; String localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); // to determine the Accept header String[] localVarHttpHeaderAccepts = new String[] { "application/json" }; String localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); if (localVarHttpHeaderAccept != null) { localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); } // set "format" to json by default // e.g. /pet/{petId}.{format} becomes /pet/{petId}.json localVarPathParams.Add("format", "json"); if (inputData != null && inputData.GetType() != typeof(byte[])) { localVarPostBody = Configuration.ApiClient.Serialize(inputData); // http body (model) parameter } else { localVarPostBody = inputData; // byte array } // authentication (oauth2_auth) required // oauth required if (!String.IsNullOrEmpty(Configuration.AccessToken)) { localVarHeaderParams["Authorization"] = "Bearer " + Configuration.AccessToken; } // authentication (api_key) required if (!String.IsNullOrEmpty(Configuration.GetApiKeyWithPrefix("api_key"))) { localVarHeaderParams["api_key"] = Configuration.GetApiKeyWithPrefix("api_key"); } // make the HTTP request IRestResponse localVarResponse = (IRestResponse)await Configuration.ApiClient.CallApiAsync(localVarPath, Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarPathParams, localVarHttpContentType); int localVarStatusCode = (int)localVarResponse.StatusCode; if (ExceptionFactory != null) { Exception exception = ExceptionFactory("OcrText", localVarResponse); if (exception != null) { throw exception; } } return(new ApiResponse <OcrOperationResponse>(localVarStatusCode, localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), (OcrOperationResponse)Configuration.ApiClient.Deserialize(localVarResponse, typeof(OcrOperationResponse)))); }
/// <summary> /// Extract text using OCR Extract text from a file using OCR. /// </summary> /// <exception cref="Muhimbi.PDF.Online.Client.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="inputData"></param> /// <returns>OcrOperationResponse</returns> public OcrOperationResponse OcrText(OcrTextData inputData) { ApiResponse <OcrOperationResponse> localVarResponse = OcrTextWithHttpInfo(inputData); return(localVarResponse.Data); }