Exemplo n.º 1
0
        private static string CallOcrWebService(FileInfo pngFile, int topLeftX, int topLeftY, int bottomRightX, int bottomRightY)
        {
            var restClient = new RestServiceClient
            {
                Proxy = { Credentials = CredentialCache.DefaultCredentials },
                ApplicationId = ConfigurationManager.AppSettings["ApplicationId"].ToString(),
                Password = ConfigurationManager.AppSettings["Password"].ToString()
            };

            var textFieldProcessingSettings = new TextFieldProcessingSettings
            {
                CustomOptions = "region=" + topLeftX + "," + topLeftY + "," + bottomRightX + "," + bottomRightY,
                Language = "english"
            };

            if (pngFile.DirectoryName == null)
                throw new Exception("png file directory name is blank");

            var outputXmlFile = Path.Combine(pngFile.DirectoryName, GetFileNameWithoutExtension(pngFile) + ".xml");

            // call the REST service
            var task = restClient.ProcessTextField(pngFile.ToString(), textFieldProcessingSettings);
            System.Threading.Thread.Sleep(4000);
            task = restClient.GetTaskStatus(task.Id);
            //Console.WriteLine("Task status: {0}", task.Status);
            while (task.IsTaskActive())
            {
                // Note: it's recommended that your application waits
                // at least 2 seconds before making the first getTaskStatus request
                // and also between such requests for the same task.
                // Making requests more often will not improve your application performance.
                // Note: if your application queues several files and waits for them
                // it's recommended that you use listFinishedTasks instead (which is described
                // at http://ocrsdk.com/documentation/apireference/listFinishedTasks/).
                System.Threading.Thread.Sleep(4000);
                task = restClient.GetTaskStatus(task.Id);
                //Console.WriteLine("Task status: {0}", task.Status);
            }
            if (task.Status == TaskStatus.Completed)
            {
                //Console.WriteLine("Processing completed.");
                restClient.DownloadResult(task, outputXmlFile);
                //Console.WriteLine("Download completed.");
            }
            else
            {
                //Console.WriteLine("Error while processing the task");
                return outputXmlFile;
            }
            return outputXmlFile;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Perform text recognition of a field
        /// Throws an exception if something goes wrong
        /// </summary>
        /// <returns>Id of created task</returns>
        public Task ProcessTextField(string filePath, TextFieldProcessingSettings settings)
        {
            string url = String.Format("{0}/processTextField{1}", ServerUrl, settings.AsUrlParams);

            // Build post request
            WebRequest request = createPostRequest(url);

            writeFileToRequest(filePath, request);

            XDocument response = performRequest(request);
            Task      task     = ServerXml.GetTaskStatus(response);

            return(task);
        }
Exemplo n.º 3
0
        private void fieldSelected(object sender, RegionSelectedEventArgs e)
        {
            string tempFilePath = System.IO.Path.GetTempFileName();
            e.CroppedImage.Save(tempFilePath, System.Drawing.Imaging.ImageFormat.Tiff);

            string outputDir = getOutputDir();

            UserTask task = new UserTask(tempFilePath);
            task.TaskStatus = "Uploading";
            task.SourceIsTempFile = true;
            task.IsFieldLevel = true;

            // TODO: correct output name
            task.OutputFilePath = System.IO.Path.Combine(
                outputDir,
                "field-level" + System.IO.Path.GetRandomFileName() + ".xml");

            task.SourceImage = e.CroppedImage;

            _userTasks.Add(task);
            _fieldLevelTasks.Add(task);

            

            // Select mode: text, barcode, checkmark
            if (flModeText.IsChecked == true)
            {
                TextFieldProcessingSettings settings = new TextFieldProcessingSettings();
                restClientAsync.ProcessTextFieldAsync(tempFilePath, settings, task);
            }
            else if (flModeBarcode.IsChecked == true)
            {
                BarcodeFieldProcessingSettings settings = new BarcodeFieldProcessingSettings();
                restClientAsync.ProcessBarcodeFieldAsync(tempFilePath, settings, task);
            }
            else
            {
                CheckmarkFieldProcessingSettings settings = new CheckmarkFieldProcessingSettings();
                string userSettings = Properties.Settings.Default.CheckmarkOptions;
                if (!String.IsNullOrEmpty(userSettings))
                    settings.Params = userSettings;

                restClientAsync.ProcessCheckmarkFieldAsync(tempFilePath, settings, task);
            }

            // temp file will be deleted in ProcessingCompleted callback
        }
Exemplo n.º 4
0
 private static TextFieldProcessingSettings buildTextFieldSettings(string language, string customOptions)
 {
     TextFieldProcessingSettings settings = new TextFieldProcessingSettings();
     settings.Language = language;
     settings.CustomOptions = customOptions;
     return settings;
 }
Exemplo n.º 5
0
        public void ProcessTextField(string sourceFilePath, string outputFilePath, TextFieldProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessTextField(sourceFilePath, settings);

            waitAndDownload(task, outputFilePath);
        }
Exemplo n.º 6
0
        public void ProcessTextField(string sourceFilePath, string outputFilePath, TextFieldProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessTextField(sourceFilePath, settings);

            // For field-level
            /*
            var flSettings = new TextFieldProcessingSettings();
            TaskId taskId = restClient.ProcessTextField(sourceFilePath, flSettings);
             */

            TaskId taskId = task.Id;

            while (true)
            {
                task = restClient.GetTaskStatus(taskId);
                if (!Task.IsTaskActive(task.Status))
                    break;

                Console.WriteLine(String.Format("Task status: {0}", task.Status));
                System.Threading.Thread.Sleep(1000);
            }

            if (task.Status == TaskStatus.Completed)
            {
                Console.WriteLine("Processing completed.");
                restClient.DownloadResult(task, outputFilePath);
                Console.WriteLine("Download completed.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
 /// <summary>
 /// Call ProcessTextField asynchronously.
 /// Performs callbacks:
 ///   UploadFileCompleted
 ///   TaskProcessingCompleted
 /// </summary>
 public void ProcessTextFieldAsync(string filePath, TextFieldProcessingSettings settings, object taskId)
 {
     processFieldAsync(filePath, settings, taskId);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Call ProcessTextField asynchronously.
 /// Performs callbacks:
 ///   UploadFileCompleted
 ///   TaskProcessingCompleted
 /// </summary>
 public void ProcessTextFieldAsync(string filePath, TextFieldProcessingSettings settings, object taskId)
 {
     processFieldAsync(filePath, settings, taskId);
 }