예제 #1
0
        /// <summary>
        /// Gets file types engine.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        /// <exception cref="Exception">Unsupported format</exception>
        public static IFileTypeEngine GetFileTypesEngine(string filePath)
        {
            IFileTypeEngine engine = null;

            FileType fileType = FilesHelper.GetFileType(filePath);

            switch (fileType)
            {
            case FileType.Bmp:
            case FileType.Gif:
            case FileType.Jpeg:
            case FileType.Png:
                engine = new SinglePageImageEngine();
                break;

            case FileType.Tiff:
                engine = new TiffEngine();
                break;

            case FileType.Pdf:
                engine = new PdfEngine();
                break;

            case FileType.Unsupported:
                throw new Exception("Unsupported format");
            }

            return(engine);
        }
예제 #2
0
파일: Gallery.cs 프로젝트: ak-git1/Tira
        /// <summary>
        /// Adds file to gallery
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <param name="maxNumberOfVerticalLines">The maximum number of vertical lines.</param>
        /// <returns></returns>
        private ActionResult AddFile(string filePath, int maxNumberOfVerticalLines = 1)
        {
            if (Images == null)
            {
                Images = new List <GalleryImage>();
            }

            try
            {
                IFileTypeEngine engine      = FileTypesEngineFactory.GetFileTypesEngine(filePath);
                int             totalPages  = engine.GetTotalPages(filePath);
                int             orderNumber = Images.Any() ? Images.Max(x => x.OrderNumber) + 1 : 1;

                for (int i = 1; i <= totalPages; i++)
                {
                    Guid   imageGuid         = Guid.NewGuid();
                    string imageFileName     = $"{imageGuid}.jpg";
                    string imagePath         = Path.Combine(GalleryFolderPath, imageFileName);
                    string thumbnailFileName = $"{imageGuid}_thumb.jpg";
                    string thumbnailPath     = Path.Combine(GalleryFolderPath, thumbnailFileName);

                    engine.SavePageToJpeg(filePath, imagePath, i);
                    new ImagesConverter(imagePath).CreateThumbnail(thumbnailPath, CommonSettings.ThumbnailWidth, CommonSettings.ThumbnailHeight);

                    GalleryImage galleryImage = new GalleryImage
                    {
                        Uid                  = Guid.NewGuid(),
                        OrderNumber          = orderNumber,
                        DisplayedName        = orderNumber.ToString(ImageNameMask),
                        ImageFolderPath      = GalleryFolderPath,
                        ImageFileName        = imageFileName,
                        ThumbnailFileName    = thumbnailFileName,
                        RecognitionCompleted = false,
                        MarkupObjects        = new MarkupObjects(maxNumberOfVerticalLines)
                    };
                    galleryImage.RecognizableElementOcrCompleted += Image_RecognizableElementOcrCompleted;

                    Images.Add(galleryImage);
                    orderNumber++;
                }

                return(new ActionResult());
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error(e, $"Unable to add {filePath} to gallery");
                return(new ActionResult(ActionResultType.Error, $"Error while trying to add {filePath} to gallery"));
            }
        }