/// <summary>Preprocess given image if it is needed.</summary> /// <param name="inputImage"> /// original input image /// <see cref="System.IO.FileInfo"/> /// </param> /// <param name="pageNumber">number of page to be OCRed</param> /// <returns> /// path to output image as /// <see cref="System.String"/> /// </returns> private String PreprocessImage(FileInfo inputImage, int pageNumber) { String tmpFileName = TesseractOcrUtil.GetTempFilePath(Guid.NewGuid().ToString(), GetExtension(inputImage)); String path = inputImage.FullName; try { if (GetTesseract4OcrEngineProperties().IsPreprocessingImages()) { Pix pix = ImagePreprocessingUtil.PreprocessImage(inputImage, pageNumber); TesseractOcrUtil.SavePixToTempPngFile(tmpFileName, pix); if (!File.Exists(System.IO.Path.Combine(tmpFileName))) { System.Drawing.Bitmap img = TesseractOcrUtil.ConvertPixToImage(pix); if (img != null) { TesseractOcrUtil.SaveImageToTempPngFile(tmpFileName, img); } } } if (!GetTesseract4OcrEngineProperties().IsPreprocessingImages() || !File.Exists(System.IO.Path.Combine(tmpFileName ))) { TesseractOcrUtil.CreateTempFileCopy(path, tmpFileName); } if (File.Exists(System.IO.Path.Combine(tmpFileName))) { path = tmpFileName; } } catch (System.IO.IOException e) { LogManager.GetLogger(GetType()).Error(MessageFormatUtil.Format(Tesseract4LogMessageConstant.CANNOT_READ_INPUT_IMAGE , e.Message)); } return(path); }
public virtual void TestImageSavingAsPngWithError() { String path = TEST_IMAGES_DIRECTORY + "numbers_01.jpg"; System.Drawing.Bitmap bi = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(new FileStream(path, FileMode.Open , FileAccess.Read)); TesseractOcrUtil.SaveImageToTempPngFile(null, bi); }
public virtual void TestNullSavingAsPng() { String tmpFileName = TesseractOcrUtil.GetTempFilePath(GetTargetDirectory() + "/testNullSavingAsPng", ".png" ); TesseractOcrUtil.SaveImageToTempPngFile(tmpFileName, null); NUnit.Framework.Assert.IsFalse(File.Exists(System.IO.Path.Combine(tmpFileName))); TesseractOcrUtil.SavePixToTempPngFile(tmpFileName, null); NUnit.Framework.Assert.IsFalse(File.Exists(System.IO.Path.Combine(tmpFileName))); }
public virtual void TestImageSavingAsPng() { String path = TEST_IMAGES_DIRECTORY + "numbers_01.jpg"; String tmpFileName = GetTargetDirectory() + "testImageSavingAsPng.png"; NUnit.Framework.Assert.IsFalse(File.Exists(System.IO.Path.Combine(tmpFileName))); System.Drawing.Bitmap bi = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(new FileStream(path, FileMode.Open , FileAccess.Read)); TesseractOcrUtil.SaveImageToTempPngFile(tmpFileName, bi); NUnit.Framework.Assert.IsTrue(File.Exists(System.IO.Path.Combine(tmpFileName))); TesseractHelper.DeleteFile(tmpFileName); NUnit.Framework.Assert.IsFalse(File.Exists(System.IO.Path.Combine(tmpFileName))); }