Пример #1
0
        private async Task <ScannedImage> ExportRawPdfPage(PdfPage page, ImportParams importParams)
        {
            string pdfPath  = Path.Combine(Paths.Temp, Path.GetRandomFileName());
            var    document = new PdfDocument();

            document.Pages.Add(page);
            document.Save(pdfPath);

            var image = ScannedImage.FromSinglePagePdf(pdfPath, false);

            if (!importParams.NoThumbnails || importParams.DetectPatchCodes)
            {
                using (var bitmap = await scannedImageRenderer.Render(image))
                {
                    if (!importParams.NoThumbnails)
                    {
                        image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
                    }
                    if (importParams.DetectPatchCodes)
                    {
                        image.PatchCode = PatchCodeDetector.Detect(bitmap);
                    }
                }
            }
            return(image);
        }
Пример #2
0
 public void PostProcessStep2(ScannedImage image, Bitmap bitmap, ScanProfile profile, ScanParams scanParams, int pageNumber)
 {
     if (!profile.UseNativeUI && profile.BrightnessContrastAfterScan)
     {
         if (profile.Brightness != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new BrightnessTransform {
                 Brightness = profile.Brightness
             });
         }
         if (profile.Contrast != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new TrueContrastTransform {
                 Contrast = profile.Contrast
             });
         }
     }
     if (profile.FlipDuplexedPages && pageNumber % 2 == 0)
     {
         AddTransformAndUpdateThumbnail(image, ref bitmap, new RotationTransform(RotateFlipType.Rotate180FlipNone));
     }
     if (profile.AutoDeskew)
     {
         var op = operationFactory.Create <DeskewOperation>();
         if (op.Start(new[] { image }))
         {
             operationProgress.ShowProgress(op);
         }
     }
     if (scanParams.DetectPatchCodes && image.PatchCode == PatchCode.None)
     {
         image.PatchCode = PatchCodeDetector.Detect(bitmap);
     }
 }
Пример #3
0
        private ScannedImage ExportG4(PdfPage page, PdfDictionary imageObject, byte[] imageBytes, ImportParams importParams)
        {
            int width            = imageObject.Elements.GetInteger(PdfImage.Keys.Width);
            int height           = imageObject.Elements.GetInteger(PdfImage.Keys.Height);
            int bitsPerComponent = imageObject.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

            // We don't have easy access to a standalone CCITT G4 decoder, so we'll make use of the .NET TIFF decoder
            // by constructing a valid TIFF file "manually" and directly injecting the bytestream
            var stream = new MemoryStream();

            Write(stream, TiffBeforeDataLen);
            // The bytestream is 2-padded, so we may need to append an extra zero byte
            if (imageBytes.Length % 2 == 1)
            {
                Write(stream, imageBytes.Length + 0x11);
            }
            else
            {
                Write(stream, imageBytes.Length + 0x10);
            }
            Write(stream, TiffBeforeData);
            Write(stream, imageBytes);
            if (imageBytes.Length % 2 == 1)
            {
                Write(stream, new byte[] { 0x00 });
            }
            Write(stream, TiffBeforeWidth);
            Write(stream, width);
            Write(stream, TiffBeforeHeight);
            Write(stream, height);
            Write(stream, TiffBeforeBits);
            Write(stream, bitsPerComponent);
            Write(stream, TiffBeforeRealLen);
            Write(stream, imageBytes.Length);
            Write(stream, TiffTrailer);
            stream.Seek(0, SeekOrigin.Begin);

            using (Bitmap bitmap = (Bitmap)Image.FromStream(stream))
            {
                bitmap.SafeSetResolution(bitmap.Width / (float)page.Width.Inch, bitmap.Height / (float)page.Height.Inch);

                var image = new ScannedImage(bitmap, ScanBitDepth.BlackWhite, true, -1);
                if (!importParams.NoThumbnails)
                {
                    image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
                }
                if (importParams.DetectPatchCodes)
                {
                    image.PatchCode = PatchCodeDetector.Detect(bitmap);
                }
                return(image);
            }
        }
Пример #4
0
 public void PostProcessStep2(ScannedImage image, Bitmap bitmap, ScanProfile profile, ScanParams scanParams, int pageNumber, bool supportsNativeUI = true)
 {
     if (!scanParams.NoThumbnails)
     {
         image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
     }
     if (scanParams.SkipPostProcessing)
     {
         return;
     }
     if (profile.StretchHistogram && !profile.HistogramStretchConfig.IsNull)
     {
         AddTransformAndUpdateThumbnail(image, ref bitmap, new StretchHistogramTransform {
             Parameters = profile.HistogramStretchConfig
         });
     }
     if ((!profile.UseNativeUI || !supportsNativeUI) && profile.BrightnessContrastAfterScan)
     {
         if (profile.Brightness != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new BrightnessTransform {
                 Brightness = profile.Brightness
             });
         }
         if (profile.Contrast != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new TrueContrastTransform {
                 Contrast = profile.Contrast
             });
         }
     }
     if (profile.FlipDuplexedPages && pageNumber % 2 == 0)
     {
         AddTransformAndUpdateThumbnail(image, ref bitmap, new RotationTransform(RotateFlipType.Rotate180FlipNone));
     }
     if (profile.AutoDeskew)
     {
         var op = operationFactory.Create <DeskewOperation>();
         if (op.Start(new[] { image }))
         {
             operationProgress.ShowProgress(op);
             op.Wait();
         }
     }
     if (scanParams.DetectPatchCodes && image.PatchCode == PatchCode.None)
     {
         image.PatchCode = PatchCodeDetector.Detect(bitmap);
     }
 }
Пример #5
0
 private ScannedImage ExportJpegImage(PdfPage page, byte[] imageBytes, ImportParams importParams)
 {
     // Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
     using (var memoryStream = new MemoryStream(imageBytes))
     {
         using (var bitmap = new Bitmap(memoryStream))
         {
             bitmap.SetResolution(bitmap.Width / (float)page.Width.Inch, bitmap.Height / (float)page.Height.Inch);
             var image = new ScannedImage(bitmap, ScanBitDepth.C24Bit, false, -1);
             image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
             if (importParams.DetectPatchCodes)
             {
                 image.PatchCode = PatchCodeDetector.Detect(bitmap);
             }
             return(image);
         }
     }
 }
Пример #6
0
        private ScannedImage ExportAsPngImage(PdfPage page, PdfDictionary imageObject, ImportParams importParams)
        {
            int width            = imageObject.Elements.GetInteger(PdfImage.Keys.Width);
            int height           = imageObject.Elements.GetInteger(PdfImage.Keys.Height);
            int bitsPerComponent = imageObject.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

            var buffer = imageObject.Stream.UnfilteredValue;

            Bitmap       bitmap;
            ScanBitDepth bitDepth;

            switch (bitsPerComponent)
            {
            case 8:
                bitmap   = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                bitDepth = ScanBitDepth.C24Bit;
                RgbToBitmapUnmanaged(height, width, bitmap, buffer);
                break;

            case 1:
                bitmap   = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
                bitDepth = ScanBitDepth.BlackWhite;
                BlackAndWhiteToBitmapUnmanaged(height, width, bitmap, buffer);
                break;

            default:
                throw new NotImplementedException("Unsupported image encoding (expected 24 bpp or 1bpp)");
            }

            using (bitmap)
            {
                bitmap.SafeSetResolution(bitmap.Width / (float)page.Width.Inch, bitmap.Height / (float)page.Height.Inch);
                var image = new ScannedImage(bitmap, bitDepth, true, -1);
                if (!importParams.NoThumbnails)
                {
                    image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
                }
                if (importParams.DetectPatchCodes)
                {
                    image.PatchCode = PatchCodeDetector.Detect(bitmap);
                }
                return(image);
            }
        }
Пример #7
0
        public IEnumerable <ScannedImage> Import(string filePath, ImportParams importParams, Func <int, int, bool> progressCallback)
        {
            if (!progressCallback(0, 1))
            {
                yield break;
            }
            Bitmap toImport;

            try
            {
                toImport = new Bitmap(filePath);
            }
            catch (Exception e)
            {
                Log.ErrorException("Error importing image: " + filePath, e);
                // Handle and notify the user outside the method so that errors importing multiple files can be aggregated
                throw;
            }
            using (toImport)
            {
                int frameCount = toImport.GetFrameCount(FrameDimension.Page);
                int i          = 0;
                foreach (var frameIndex in importParams.Slice.Indices(frameCount))
                {
                    if (!progressCallback(i++, frameCount))
                    {
                        yield break;
                    }
                    toImport.SelectActiveFrame(FrameDimension.Page, frameIndex);
                    var image = new ScannedImage(toImport, ScanBitDepth.C24Bit, IsLossless(toImport.RawFormat), -1);
                    image.SetThumbnail(thumbnailRenderer.RenderThumbnail(toImport));
                    if (importParams.DetectPatchCodes)
                    {
                        image.PatchCode = PatchCodeDetector.Detect(toImport);
                    }
                    yield return(image);
                }
                progressCallback(frameCount, frameCount);
            }
        }
Пример #8
0
        public ScannedImageSource Import(string filePath, ImportParams importParams, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        source.Done();
                        return;
                    }

                    Bitmap toImport;
                    try
                    {
                        toImport = new Bitmap(filePath);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorException("Error importing image: " + filePath, e);
                        // Handle and notify the user outside the method so that errors importing multiple files can be aggregated
                        throw;
                    }

                    using (toImport)
                    {
                        int frameCount = toImport.GetFrameCount(FrameDimension.Page);
                        int i          = 0;
                        foreach (var frameIndex in importParams.Slice.Indices(frameCount))
                        {
                            progressCallback(i++, frameCount);
                            if (cancelToken.IsCancellationRequested)
                            {
                                source.Done();
                                return;
                            }

                            toImport.SelectActiveFrame(FrameDimension.Page, frameIndex);
                            var image = new ScannedImage(toImport, ScanBitDepth.C24Bit, IsLossless(toImport.RawFormat), -1);
                            if (!importParams.NoThumbnails)
                            {
                                image.SetThumbnail(thumbnailRenderer.RenderThumbnail(toImport));
                            }
                            if (importParams.DetectPatchCodes)
                            {
                                image.PatchCode = PatchCodeDetector.Detect(toImport);
                            }

                            source.Put(image);
                        }

                        progressCallback(frameCount, frameCount);
                    }
                    source.Done();
                }
                catch (Exception e)
                {
                    source.Error(e);
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }