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); } }
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"); }
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; } }
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); } }
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); }
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); } }
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; } }
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()); }