Beispiel #1
0
        /// <summary>
        /// Upload a file to service synchronously and start processing
        /// </summary>
        /// <param name="filePath">Path to an image to process</param>
        /// <param name="settings">Language and output format</param>
        /// <returns>Id of the task. Check task status to see if you have enough units to process the task</returns>
        /// <exception cref="ProcessingErrorException">thrown when something goes wrong</exception>
        public OcrSdkTask ProcessImage(string filePath, ProcessingSettings settings)
        {
            string url = String.Format("{0}/processImage?{1}", ServerUrl, settings.AsUrlParams);

            if (!String.IsNullOrEmpty(settings.Description))
            {
                url = url + "&description=" + Uri.EscapeDataString(settings.Description);
            }

            try
            {
                // Build post request
                WebRequest request = WebRequest.Create(url);
                setupPostRequest(url, request);
                writeFileToRequest(filePath, request);

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

                return(task);
            }
            catch (System.Net.WebException e)
            {
                throw new ProcessingErrorException("Cannot upload file: " + e.Message + ". Please make sure that application Id and password are correct.", e);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Upload a file to service synchronously and start processing
        /// </summary>
        /// <param name="input">Byte array to process</param>
        /// <param name="settings">Language and output format</param>
        /// <returns>Id of the task. Check task status to see if you have enough units to process the task</returns>
        /// <exception cref="ProcessingErrorException">thrown when something goes wrong</exception>
        public Task ProcessImage(byte[] input, ProcessingSettings settings)
        {
            string url = String.Format("{0}/processImage?{1}", ServerUrl, settings.AsUrlParams);

            if (!String.IsNullOrEmpty(settings.Description))
            {
                url = url + "&description=" + Uri.EscapeDataString(settings.Description);
            }

            try
            {
                // Build post request
                WebRequest request = createPostRequest(url);
                writeFileToRequest(input, request);

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

                return(task);
            }
            catch (System.Net.WebException e)
            {
                String friendlyMessage = retrieveFriendlyMessage(e);
                if (friendlyMessage != null)
                {
                    throw new ProcessingErrorException(friendlyMessage, e);
                }
                throw new ProcessingErrorException("Cannot upload file", e);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Upload a file to service synchronously and start processing
 /// </summary>
 /// <param name="filePath">Path to an image to process</param>
 /// <param name="settings">Language and output format</param>
 /// <returns>Id of the task. Check task status to see if you have enough units to process the task</returns>
 /// <exception cref="ProcessingErrorException">thrown when something goes wrong</exception>
 public Task ProcessImage(string filePath, ProcessingSettings settings)
 {
     using (var ms = new MemoryStream())
     {
         var bytes = File.ReadAllBytes(filePath);
         ms.Write(bytes, 0, bytes.Length);
         return(ProcessImage(ms, settings));
     }
 }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Stream imageStream = AppData.Instance.Image;
            if (imageStream == null)
                return;

            string localPath = "image.jpg";
            saveImageToFile(imageStream, localPath);

            ProcessingSettings settings = new ProcessingSettings();
            settings.SetLanguage("English,Russian");
            settings.OutputFormat = OutputFormat.txt;

            displayMessage("Uploading..");
            abbyyClient.ProcessImageAsync(localPath, settings, settings);
        }
Beispiel #5
0
        /// <summary>
        /// Process directory or file with given path
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="outputFilePath">Path to directory to store results
        /// Will be created if it doesn't exist
        /// </param>
        /// <param name="processAsDocument">If true, all images are processed as a single document</param>
        public void ProcessPath(string sourcePath, string outputPath, ProcessingSettings settings,
            bool processAsDocument)
        {
            List<string> sourceFiles = new List<string>();
            if (Directory.Exists(sourcePath))
            {
                sourceFiles.AddRange(Directory.GetFiles(sourcePath));
                sourceFiles.Sort();
            }
            else if (File.Exists(sourcePath))
            {
                sourceFiles.Add(sourcePath);
            }
            else
            {
                Console.WriteLine("Invalid source path");
                return;
            }

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            if (!processAsDocument || sourceFiles.Count == 1)
            {
                foreach (string filePath in sourceFiles)
                {
                    string outputFileName = Path.GetFileNameWithoutExtension(filePath);
                    string ext = settings.OutputFileExt;
                    string outputFilePath = Path.Combine(outputPath, outputFileName + ext);

                    Console.WriteLine("Processing " + Path.GetFileName(filePath));

                    ProcessFile(filePath, outputFilePath, settings);
                }
            }
            else
            {
                string outputFileName = "document";
                string ext = settings.OutputFileExt;
                string outputFilePath = Path.Combine(outputPath, outputFileName + ext);

                ProcessDocument(sourceFiles, outputFilePath, settings);
            }
        }
Beispiel #6
0
        public Task StartProcessingTask(TaskId taskId, ProcessingSettings settings)
        {
            string url = String.Format("{0}/processDocument?taskId={1}&{2}", ServerUrl,
                                       Uri.EscapeDataString(taskId.ToString()),
                                       settings.AsUrlParams);

            if (!String.IsNullOrEmpty(settings.Description))
            {
                url = url + "&description=" + Uri.EscapeDataString(settings.Description);
            }

            // Build get request
            WebRequest request    = createGetRequest(url);
            XDocument  response   = performRequest(request);
            Task       serverTask = ServerXml.GetTaskStatus(response);

            return(serverTask);
        }
        /// <summary>
        /// Upload a file to service synchronously and start processing
        /// </summary>
        /// <param name="filePath">Path to an image to process</param>
        /// <param name="settings">Language and output format</param>
        /// <returns>Id of the task. Check task status to see if you have enough units to process the task</returns>
        /// <exception cref="ProcessingErrorException">thrown when something goes wrong</exception>
        public OcrSdkTask ProcessImage(string filePath, ProcessingSettings settings)
        {
            string url = String.Format("{0}/processImage?{1}", ServerUrl, settings.AsUrlParams);

            if (!String.IsNullOrEmpty(settings.Description))
            {
                url = url + "&description=" + Uri.EscapeDataString(settings.Description);
            }

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

            writeFileToRequest(filePath, request);

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

            return(task);
        }
Beispiel #8
0
        /// <summary>
        /// Submit and process image asynchronously.
        /// Performs callbacks:
        ///   UploadFileCompleted
        ///   TaskProcessingCompleted
        /// </summary>
        /// <param name="filePath">Path to file in isolated storage</param>
        /// <param name="settings"></param>
        /// <param name="userState"></param>
        public void ProcessImageAsync(string filePath, ProcessingSettings settings, object userState)
        {
            BackgroundWorker w = new BackgroundWorker();

            w.DoWork += new DoWorkEventHandler((sender, e) =>
            {
                OcrSdkTask task = null;
                try
                {
                    task = _syncClient.ProcessImage(filePath, settings);
                    UploadCompletedEventArgs uploadArgs = new UploadCompletedEventArgs(task, userState);
                    onUploadFileCompleted(sender, uploadArgs);

                    // Wait until task finishes
                    while (true)
                    {
                        task = _syncClient.GetTaskStatus(task.Id);
                        if (!task.IsTaskActive())
                        {
                            break;
                        }
                        Thread.Sleep(1000);
                    }

                    if (task.Status == TaskStatus.NotEnoughCredits)
                    {
                        throw new Exception("Not enough credits to process image. Please add more pages to your application's account.");
                    }

                    TaskEventArgs taskArgs = new TaskEventArgs(task, null, false, userState);

                    onProcessingCompleted(sender, taskArgs);
                }
                catch (Exception ex)
                {
                    TaskEventArgs taskArgs = new TaskEventArgs(task, ex, false, userState);
                    onProcessingCompleted(sender, taskArgs);
                }
            }
                                               );

            w.RunWorkerAsync();
        }
        /// <summary>
        /// Submit and process image asynchronously. 
        /// Performs callbacks:
        ///   UploadFileCompleted
        ///   TaskProcessingCompleted
        /// </summary>
        /// <param name="filePath">Path to file in isolated storage</param>
        /// <param name="settings"></param>
        /// <param name="userState"></param>
        public void ProcessImageAsync(string filePath, ProcessingSettings settings, object userState)
        {
            BackgroundWorker w = new BackgroundWorker();
            w.DoWork += new DoWorkEventHandler((sender, e) =>
                {
                    Task task = null;
                    try
                    {
                        task = _syncClient.ProcessImage(filePath, settings);
                        UploadCompletedEventArgs uploadArgs = new UploadCompletedEventArgs(task, userState);
                        onUploadFileCompleted(sender, uploadArgs);

                        // Wait until task finishes
                        while (true)
                        {
                            task = _syncClient.GetTaskStatus(task.Id);
                            if (!task.IsTaskActive())
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                        }

                        if (task.Status == TaskStatus.NotEnoughCredits)
                        {
                            throw new Exception("Not enough credits to process image. Please add more pages to your application's account.");
                        }

                        TaskEventArgs taskArgs = new TaskEventArgs(task, null, false, userState);

                        onProcessingCompleted(sender, taskArgs);
                    }
                    catch (Exception ex)
                    {
                        TaskEventArgs taskArgs = new TaskEventArgs(task, ex, false, userState);
                        onProcessingCompleted(sender, taskArgs);
                    }
                }
            );

            w.RunWorkerAsync();
        }
Beispiel #10
0
        private static ProcessingSettings buildSettings(string language, string outputFormat)
        {
            ProcessingSettings settings = new ProcessingSettings();
            settings.SetLanguage( language );
            switch (outputFormat.ToLower())
            {
                case "txt": settings.OutputFormat = OutputFormat.txt; break;
                case "rtf": settings.OutputFormat = OutputFormat.rtf; break;
                case "docx": settings.OutputFormat = OutputFormat.docx; break;
                case "xlsx": settings.OutputFormat = OutputFormat.xlsx; break;
                case "pptx": settings.OutputFormat = OutputFormat.pptx; break;
                case "pdfsearchable": settings.OutputFormat = OutputFormat.pdfSearchable; break;
                case "pdftextandimages": settings.OutputFormat = OutputFormat.pdfTextAndImages; break;
                case "xml": settings.OutputFormat = OutputFormat.xml; break;
                default:
                    throw new ArgumentException("Invalid output format");
            }

            return settings;
        }
Beispiel #11
0
        public void ProcessFile(string sourceFilePath, string outputFileBase, ProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessImage(sourceFilePath, settings);

            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.");
                for (int i = 0; i < settings.OutputFormats.Count; i++)
                {
                    var outputFormat = settings.OutputFormats[i];
                    string ext = settings.GetOutputFileExt(outputFormat);
                    restClient.DownloadUrl(task.DownloadUrls[i], outputFileBase + ext);
                }
                Console.WriteLine("Download completed.");
            }
            else if (task.Status == TaskStatus.NotEnoughCredits)
            {
                Console.WriteLine("Not enough credits to process the file. Please add more pages to your application balance.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
Beispiel #12
0
 ProcessingSettings GetProcessingSettings()
 {
     ProcessingSettings result = new ProcessingSettings();
     result.Language = getLanguage();
     result.OutputFormat = getOutputFormat();
     return result;
 }
Beispiel #13
0
 /// <summary>
 /// Upload a file to service synchronously and start processing
 /// </summary>
 /// <param name="inputStream">Stream to process</param>
 /// <param name="settings">Language and output format</param>
 /// <returns>Id of the task. Check task status to see if you have enough units to process the task</returns>
 /// <exception cref="ProcessingErrorException">thrown when something goes wrong</exception>
 public Task ProcessImage(Stream inputStream, ProcessingSettings settings)
 {
     return(ProcessImage(toByteArray(inputStream), settings));
 }
Beispiel #14
0
 ProcessingSettings GetProcessingSettings()
 {
     ProcessingSettings result = new ProcessingSettings();
     result.SetLanguage(getLanguages());
     result.SetOutputFormat( getOutputFormat() );
     return result;
 }
Beispiel #15
0
        public void ProcessDocument(IEnumerable<string> _sourceFiles, string outputFileBase,
            ProcessingSettings settings)
        {
            string[] sourceFiles = _sourceFiles.ToArray();
            Console.WriteLine(String.Format("Recognizing {0} images as a document",
                sourceFiles.Length));

            Task task = null;
            for (int fileIndex = 0; fileIndex < sourceFiles.Length; fileIndex++)
            {
                string filePath = sourceFiles[fileIndex];
                Console.WriteLine("{0}: uploading {1}", fileIndex + 1, Path.GetFileName(filePath));

                task = restClient.UploadAndAddFileToTask(filePath, task == null ? null : task.Id);
            }

            // Start task
            Console.WriteLine("Starting task..");
            task = restClient.StartProcessingTask(task.Id, settings);

            task = waitForTask(task);

            if (task.Status == TaskStatus.Completed)
            {
                Console.WriteLine("Processing completed.");
                for( int i = 0; i < settings.OutputFormats.Count; i++ ) 
                {
                    var outputFormat = settings.OutputFormats[i];
                    string ext = settings.GetOutputFileExt(outputFormat);
                    restClient.DownloadUrl(task.DownloadUrls[i], outputFileBase + ext);
                }
                Console.WriteLine("Download completed.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
Beispiel #16
0
        public void ProcessFile(string sourceFilePath, string outputFileBase, ProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessImage(sourceFilePath, settings);

            task = waitForTask(task);

            if (task.Status == TaskStatus.Completed)
            {
                Console.WriteLine("Processing completed.");
                for (int i = 0; i < settings.OutputFormats.Count; i++)
                {
                    var outputFormat = settings.OutputFormats[i];
                    string ext = settings.GetOutputFileExt(outputFormat);
                    restClient.DownloadUrl(task.DownloadUrls[i], outputFileBase + ext);
                }
                Console.WriteLine("Download completed.");
            }
            else if (task.Status == TaskStatus.NotEnoughCredits)
            {
                Console.WriteLine("Not enough credits to process the file. Please add more pages to your application balance.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
Beispiel #17
0
        public void ProcessDocument(IEnumerable<string> _sourceFiles, string outputFilePath,
            ProcessingSettings settings)
        {
            string[] sourceFiles = _sourceFiles.ToArray();
            Console.WriteLine(String.Format("Recognizing {0} images as a document",
                sourceFiles.Length));

            Task task = null;
            for (int fileIndex = 0; fileIndex < sourceFiles.Length; fileIndex++)
            {
                string filePath = sourceFiles[fileIndex];
                Console.WriteLine("{0}: uploading {1}", fileIndex + 1, Path.GetFileName(filePath));

                task = restClient.UploadAndAddFileToTask(filePath, task == null ? null : task.Id);
            }

            // Start task
            Console.WriteLine("Starting task..");
            restClient.StartProcessingTask(task.Id, settings);

            while (true)
            {
                task = restClient.GetTaskStatus(task.Id);
                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");
            }
        }
Beispiel #18
0
        public void ProcessFile(string sourceFilePath, string outputFilePath, ProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessImage(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 if (task.Status == TaskStatus.NotEnoughCredits)
            {
                Console.WriteLine("Not enough credits to process the file. Please add more pages to your application balance.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
 public void ProcessImageAsync(string filePath, ProcessingSettings settings, object taskId)
 {
     processFileAsync(filePath, settings, taskId);
 }
Beispiel #20
0
 public void UploadFileAsync(string filePath, ProcessingSettings settings, object taskId)
 {
     processFileAsync(filePath, settings, taskId);
 }
Beispiel #21
0
        private static ProcessingSettings buildSettings(string language,
            string outputFormat, string profile)
        {
            ProcessingSettings settings = new ProcessingSettings();
            settings.SetLanguage( language );
            switch (outputFormat.ToLower())
            {
                case "txt": settings.SetOutputFormat(OutputFormat.txt); break;
                case "rtf": settings.SetOutputFormat( OutputFormat.rtf); break;
                case "docx": settings.SetOutputFormat( OutputFormat.docx); break;
                case "xlsx": settings.SetOutputFormat( OutputFormat.xlsx); break;
                case "pptx": settings.SetOutputFormat( OutputFormat.pptx); break;
                case "pdfsearchable": settings.SetOutputFormat( OutputFormat.pdfSearchable); break;
                case "pdftextandimages": settings.SetOutputFormat( OutputFormat.pdfTextAndImages); break;
                case "xml": settings.SetOutputFormat( OutputFormat.xml); break;
                default:
                    throw new ArgumentException("Invalid output format");
            }
            if (profile != null)
            {
                switch (profile.ToLower())
                {
                    case "documentconversion":
                        settings.Profile = Profile.documentConversion;
                        break;
                    case "documentarchiving":
                        settings.Profile = Profile.documentArchiving;
                        break;
                    case "textextraction":
                        settings.Profile = Profile.textExtraction;
                        break;
                    default:
                        throw new ArgumentException("Invalid profile");
                }
            }

            return settings;
        }