예제 #1
0
        protected override string DoScan(string filename, LoggingSection log)
        {
            DateTime beginning = DateTime.Now;

            try
            {
                log.Debug($"Extracting text from file");
                string text = File.ReadAllText(filename);
                log.Debug("Text extracted");
                return(text);
            }
            catch (Exception)
            {
                throw;
                //return new ScanResult(null, filename, this, beginning, DateTime.Now, false, e);
            }
        }
예제 #2
0
        public CommandHandler()
        {
            logging    = new LoggingSection(this);
            commands   = new List <ICommand>();
            hasControl = false;

            logging.Info("Initializing commands");
            foreach (Type type in AppDomain.CurrentDomain.GetAssemblies()
                     .SelectMany(s => s.GetTypes())
                     .Where(p => !p.IsInterface && typeof(ICommand).IsAssignableFrom(p))
                     .Where(t => t.GetConstructors().All(c => c.GetParameters().Length == 0)))
            {
                ICommand cmd = (ICommand)Activator.CreateInstance(type);
                logging.Debug($"Initialized command {cmd.Name}");
                commands.Add(cmd);
            }
            logging.Debug($"Initialized {commands.Count} commands");
        }
예제 #3
0
 public void TakeControl()
 {
     if (!hasControl)
     {
         logging.Debug($"Took control over Thread \"{Thread.CurrentThread.Name}\" and handling console input");
         hasControl = true;
         while (Program.IsRunning)
         {
             HandleInput(Console.ReadLine());
         }
         hasControl = false;
     }
 }
예제 #4
0
        protected override async Task <string> DoScanAsync(string filename, LoggingSection log)
        {
            DateTime beginning = DateTime.Now;

            try
            {
                string text;
                log.Debug($"Extracting text from file");
                using (StreamReader streamReader = File.OpenText(filename))
                {
                    text = await streamReader.ReadToEndAsync();

                    log.Debug("Text extracted");
                }
                return(text);
            }
            catch (Exception)
            {
                throw;
                //return new ScanResult(null, filename, this, beginning, DateTime.Now, false, e);
            }
        }
예제 #5
0
        public void Log_ReturnedLogMessage_ShouldHaveLogLevelDebug()
        {
            //Arrange
            LoggingSection loggingSection = new LoggingSection(this);
            LogMessage     logMessage;

            //Act
            logMessage = loggingSection.Debug("Test");

            //Assert
            Assert.NotNull(logMessage);
            Assert.Equal(LogLevel.DEBUG, logMessage.LogLevel);
        }
예제 #6
0
파일: Scanners.cs 프로젝트: lanicon/HawkEye
 public Scanners()
 {
     logging  = new LoggingSection(this);
     scanners = new List <Scanner>();
     logging.Info("Initializing scanners");
     foreach (Type type in AppDomain.CurrentDomain.GetAssemblies()
              .SelectMany(s => s.GetTypes())
              .Where(p => !p.IsInterface && p != typeof(Scanner) && typeof(Scanner).IsAssignableFrom(p))
              .Where(t => t.GetConstructors().All(c => c.GetParameters().Length == 0)))
     {
         Scanner scanner = (Scanner)Activator.CreateInstance(type);
         logging.Debug($"Created instance of {type.Name}");
         scanners.Add(scanner);
     }
 }
예제 #7
0
 public static void Disable(IDisposable service)
 {
     try
     {
         service.Dispose();
         servicesToBeDisposed.Remove(service);
         logging.Debug($"{service.GetType().Name + (service.GetType().GetTypeInfo().GenericTypeArguments.Length > 0 ? $"<{string.Join(", ", service.GetType().GenericTypeArguments.Select(type => type.Name))}>" : "")} disposed");
     }
     catch (Exception e)
     {
         logging.Error($"" +
                       $"Unhandled exception while disposing Service {service.GetType().Name}: {e.Message}" +
                       $"\nSource: {(e.Source != null ? e.Source : "Unknown")}" +
                       $"\nStackTrace: {e.StackTrace}");
         throw;
     }
 }
예제 #8
0
        protected override string DoScan(string filename, LoggingSection log)
        {
            PdfDocument   pdfDocument   = PdfReader.Open(filename);
            StringBuilder stringBuilder = new StringBuilder();

            for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)
            {
                log.Verbose($"Scanning page {pageIndex + 1} of {pdfDocument.PageCount}");
                PdfPage pdfPage = pdfDocument.Pages[pageIndex];
                //Extract text from text elements
                stringBuilder.Append($"{ExtractTextFromPdfPage(pdfPage)}{Environment.NewLine}");

                //Extract text from image elements with Tesseract OCR - awesome! :)
                PdfDictionary resources = pdfPage.Elements.GetDictionary("/Resources");
                if (resources != null)
                {
                    PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
                    if (xObjects != null)
                    {
                        ICollection <PdfItem> items = xObjects.Elements.Values;
                        foreach (PdfItem item in items)
                        {
                            PdfReference reference = item as PdfReference;
                            if (reference != null)
                            {
                                PdfDictionary xObject = reference.Value as PdfDictionary;
                                if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
                                {
                                    Bitmap bitmap = PdfImageToBitmap(xObject);
                                    if (bitmap == null)
                                    {
                                        log.Error("Could not extract bitmap from PDF image element. Seems like the PDF image filter type is not supported. Skipping element!");
                                        continue;
                                    }
                                    log.Debug("Rotating image");
                                    bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                    log.Debug("Upscaling image 2x");
                                    BitmapUtils.Scale(ref bitmap, 2);
                                    log.Debug("Grayscaling image");
                                    BitmapUtils.GrayscaleWithLockBits(bitmap);
                                    log.Debug("Denoising image");
                                    BitmapUtils.DenoiseWithLockBits(bitmap);
                                    log.Debug("Applying OCR on image");
                                    Pix             pix             = PixConverter.ToPix(bitmap);
                                    TesseractEngine tesseractEngine = Services.OCRProvider.AwaitResource();
                                    Page            tesseractPage   = tesseractEngine.Process(pix);
                                    try
                                    {
                                        string text = tesseractPage.GetText();
                                        log.Debug($"Text is {text.Length} characters long");
                                        if (!string.IsNullOrWhiteSpace(text) && text != "\n")
                                        {
                                            stringBuilder.Append(text.Replace("\n", " "));
                                        }
                                    }
                                    catch (InvalidOperationException e)
                                    {
                                        log.Error($"OCR failed on Page {pageIndex} of file {filename}:\n{e.StackTrace}");
                                    }
                                    Services.OCRProvider.Feed(tesseractEngine);
                                    pix.Dispose();
                                }
                            }
                        }
                    }
                }
                stringBuilder.Append("\n");
            }

            log.Debug("Trimming text");
            string documentText = stringBuilder.ToString();

            documentText = documentText.Trim();
            while (documentText.Contains("  "))
            {
                documentText = documentText.Replace("  ", " ");
            }
            while (documentText.Contains("\n\n"))
            {
                documentText = documentText.Replace("\n\n", "\n");
            }
            return(stringBuilder.ToString());
        }