Exemple #1
0
 public OcrApiServiceForm()
 {
     _formManager = new OcrApiServiceManager();
     _formModel = _formManager.LoadLastRequest();
     InitializeComponent();
     SetControlsNames();
     _apiCodeTextBox.Text = _formModel.ApiKey;
     _languageCodeTextBox.Text = _formModel.LanguageCode;
     _selectFileTextBox.Text = _formManager.GetFilePath(_formModel);
 }
 public OcrApiServiceForm()
 {
     _formManager = new OcrApiServiceManager();
     _formModel = _formManager.LoadLastRequest();
     InitializeComponent();
     _errorProvider.SetIconPadding(_selectFileTextBox, -20);
     // Sets control text from the resources
     SetControlsNames();
     // Adds existing language codes to combobox
     _languageCodeComboBox.Items.AddRange(_formManager.GetCodes().ToArray());
     // Fills form from model
     _apiCodeTextBox.Text = _formModel.ApiKey;
     _languageCodeComboBox.Text = _formModel.LanguageCode;
     _selectFileTextBox.Text = _formManager.GetFilePath(_formModel);
 }
        /// <summary>
        /// Gets file path from model
        /// </summary>
        /// <param name="model">Current form model</param>
        /// <returns>Path from the current model or empty string if file does not exist or is of invalid type</returns>
        public string GetFilePath(PostImageModel model)
        {
            string pathValidationResult = ValidateFilePath(model);
            return string.IsNullOrEmpty(pathValidationResult)
                    ? model.PicturePath
                    : string.Empty;

        }
        private void ToXML(PostImageModel model)
        {
            try
            {
                string directory = Path.GetDirectoryName(_lastPostedRequest);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                File.Delete(_lastPostedRequest);
                using (StreamWriter streamWriter = new StreamWriter(_lastPostedRequest))
                {
                    _uiModelSerializer.Serialize(streamWriter, model);
                }
            }
            catch
            { }
        }
        /// <summary>
        /// Validates file path from model
        /// </summary>
        /// <param name="model">Current form model</param>
        /// <returns> Error message if any or empty string</returns>
        public string ValidateFilePath(PostImageModel model)
        {
            if (string.IsNullOrEmpty(model.PicturePath) || !File.Exists(model.PicturePath))
            {
                return string.Format(ControlsResource.FILE_NOT_FOUND, model.PicturePath);
            }

            if (!GetAllowedFileExtentions().Contains(Path.GetExtension(model.PicturePath)))
            {
                return ControlsResource.INVALID_FILE;
            }

            return string.Empty;
        }
        /// <summary>
        /// Displays file browsing window
        /// </summary>
        /// <param name="model">Current form model</param>
        /// <returns>Path to the selected file</returns>
        public string BrowseFile(PostImageModel model)
        {
            string fileToOpen = string.Empty;
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                //Sets initial browsing folder
                if (!string.IsNullOrEmpty(model.PicturePath))
                {
                    string directoryName = Path.GetDirectoryName(model.PicturePath);
                    if (Directory.Exists(directoryName))
                    {
                        fileDialog.InitialDirectory = directoryName;
                    }
                }

                // Adds allowed file types filter
                if (_allowedFileTypes != null && _allowedFileTypes.Count() > 0)
                {
                    fileDialog.Filter = BuildFileFilter();
                }


                //Displays dialog
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    fileToOpen = fileDialog.FileName;
                }
            }

            return fileToOpen;
        }
        /// <summary>
        /// Sends image to ocr service
        /// </summary>
        /// <param name="model">Model that contains values if the request properties</param>
        /// <param name="callback">Action to be executed after response is received</param>
        public void SendImage(PostImageModel model, Action<string> callback)
        {
            Task task = taskFactory.StartNew(() =>
                {
                    string result = string.Empty;
                    try
                    {
                        // Trys to save current model state
                        ToXML(model);
                        // Creating parts of the request to be sent
                        IList<IFormPart> parts = new List<IFormPart>();
                        parts.Add(new FileFormPartModel("image", model.PicturePath, new Dictionary<string, string>() { { "Content-Type", GetContentType(model.PicturePath) } }));
                        parts.Add(new StringFormPartModel("language", model.LanguageCode));
                        parts.Add(new StringFormPartModel("apikey", model.ApiKey));
                        // Creats communicator and sending request
                        ServiceCommunicator communicator = new ServiceCommunicator();
                        ServiceResponse response = communicator.PostRequest(_ocrApiServiceUrl, parts, 10000);
                        result = response.ToString();
                    }
                    catch
                    { }

                    return result;
                }
            ).ContinueWith(cTask => { if (callback != null) callback(cTask.Result); }, TaskScheduler.FromCurrentSynchronizationContext());
        }