Exemple #1
0
        public ScannedImageSource Scan()
        {
            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < ImageCount; i++)
                {
                    Thread.Sleep(500);
                    source.Put(MakeImage());
                }
                source.Done();
            }, TaskCreationOptions.LongRunning);
            return(source);
        }
Exemple #2
0
        public ScannedImageSource Scan()
        {
            if (!IsSupported)
            {
                throw new DriverNotSupportedException();
            }
            if (ScanProfile == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanProfile must be specified before calling Scan().");
            }
            if (ScanParams == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanParams must be specified before calling Scan().");
            }
            if (ScanDevice == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanDevice must be specified before calling Scan().");
            }
            if (DialogParent == null && !ScanParams.NoUI)
            {
                throw new InvalidOperationException("IScanDriver.DialogParent must be specified before calling Scan() without NoUI.");
            }

            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(async() =>
            {
                try
                {
                    await ScanInternal(source);
                    source.Done();
                }
                catch (ScanDriverException e)
                {
                    source.Error(e);
                }
                catch (FaultException <ScanDriverExceptionDetail> e)
                {
                    source.Error(e.Detail.Exception);
                }
                catch (Exception e)
                {
                    source.Error(new ScanDriverUnknownException(e));
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }
Exemple #3
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);
        }
Exemple #4
0
        public ScannedImageSource Import(string filePath, ImportParams importParams, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            var source = new ScannedImageSource.Concrete();

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

                int passwordAttempts = 0;
                bool aborted         = false;
                int i = 0;
                try
                {
                    PdfDocument document = PdfReader.Open(filePath, PdfDocumentOpenMode.Import, args =>
                    {
                        if (!pdfPasswordProvider.ProvidePassword(Path.GetFileName(filePath), passwordAttempts++, out args.Password))
                        {
                            args.Abort = true;
                            aborted    = true;
                        }
                    });
                    if (passwordAttempts > 0 &&
                        !document.SecuritySettings.HasOwnerPermissions &&
                        !document.SecuritySettings.PermitExtractContent)
                    {
                        errorOutput.DisplayError(string.Format(MiscResources.PdfNoPermissionToExtractContent, Path.GetFileName(filePath)));
                        source.Done();
                    }

                    var pages = importParams.Slice.Indices(document.PageCount)
                                .Select(index => document.Pages[index])
                                .TakeWhile(page =>
                    {
                        progressCallback(i++, document.PageCount);
                        return(!cancelToken.IsCancellationRequested);
                    });
                    if (document.Info.Creator != MiscResources.NAPS2 && document.Info.Author != MiscResources.NAPS2)
                    {
                        pdfRenderer.ThrowIfCantRender();
                        foreach (var page in pages)
                        {
                            source.Put(await ExportRawPdfPage(page, importParams));
                        }
                    }
                    else
                    {
                        foreach (var page in pages)
                        {
                            await GetImagesFromPage(page, importParams, source);
                        }
                    }
                }
                catch (ImageRenderException e)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.ImportErrorNAPS2Pdf, Path.GetFileName(filePath)));
                    Log.ErrorException("Error importing PDF file.", e);
                }
                catch (Exception e)
                {
                    if (!aborted)
                    {
                        errorOutput.DisplayError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(filePath)));
                        Log.ErrorException("Error importing PDF file.", e);
                    }
                }
                finally
                {
                    source.Done();
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }