Ejemplo n.º 1
0
        public void PrepareOCR(string file)
        {
            if (updateWorker != null && !updateWorker.IsCompleted)
            {
                updateWorkerCTS.Cancel();
                try {
                    updateWorker.Wait();
                } catch {}
                updateWorker = null;
            }
            int pageFirst = 0, pageLast = 0;

            if (Path.GetExtension(file).ToLower() == ".pdf")
            {
                int cnt = (int)OCR.GetPDFPagesCount(file);
                if (cnt > 1)
                {
                    var wnd_range = new PageRangeWindow(cnt);
                    wnd_range.ShowDialog(wnd);
                    if (wnd_range.Cancelled)
                    {
                        return;
                    }
                    pageFirst = wnd_range.PageFirst;
                    pageLast  = wnd_range.PageLast;
                }
            }
            LoadingWindow wnd_waiter = new LoadingWindow("Rozpoznawanie");

            updateWorkerCTS = new CancellationTokenSource();
            updateWorkerCT  = updateWorkerCTS.Token;
            updateWorker    = Task.Factory.StartNew(() => {
                wnd_waiter.SetStatus("Przygotowywanie");

                OCRStatus status            = new OCRStatus();
                status.OCRCancellationToken = updateWorkerCT;
                status.PageCurrentChanged  += (sender, e) => {
                    wnd_waiter.SetStatus($"Rozpoznawanie strony {status.PageCurrent} z {status.PageCount}");
                    int prc = (int)(100 * status.PageCurrent / status.PageCount);
                    if (prc > 100)
                    {
                        prc = 100;
                    }
                    wnd_waiter.SetPercentage(prc);
                };
                OCRResult result = OCR.GetTextFromFileAsync(file, this.Engine, status, pageFirst, pageLast).Result;
                if (result != null)
                {
                    wnd.SetResult(file, result);
                }
                else
                if (status.Error != OctarineError.CancellationRequested)
                {
                    wnd.ShowError(status.Error, status.ErrorMessage);
                }
                wnd_waiter.Close();
                updateWorker = null;
            }, updateWorkerCT);
            wnd_waiter.ShowDialog(wnd);
            if (updateWorker != null && !updateWorker.IsCompleted)
            {
                updateWorkerCTS.Cancel();
                try {
                    updateWorker.Wait();
                } catch {}
                updateWorker = null;
            }
        }
Ejemplo n.º 2
0
        public bool OpenDownloader(string[] sources, string[] destinations, Form window = null, string label = "Pobieranie")
        {
            if (window == null)
            {
                window = wnd;
            }
            bool cancellationProcessed = false;
            var  l = new LoadingWindow(label);

            l.SetStatus("Inicjowanie...");
            bool cancelled = true;
            var  cts       = new CancellationTokenSource();
            var  ct        = cts.Token;

            Task.Factory.StartNew(() => {
                int i = 0;
                using (var client = new WebClient()) {
                    client.DownloadProgressChanged += (sender, e) => {
                        int percentage = i * 100;
                        percentage    += e.ProgressPercentage;
                        percentage     = percentage / sources.Count();
                        l.SetPercentage(percentage);
                        l.SetStatus($"Pobieranie pliku {i+1} z {sources.Count()}: {e.BytesReceived/1048576} / {e.TotalBytesToReceive/1048576} MB");
                        if (ct.IsCancellationRequested)
                        {
                            client.CancelAsync();
                            cancellationProcessed = true;
                        }
                    };
                    client.DownloadFileCompleted += (sender, e) => {
                        i++;
                        if (i < sources.Count())
                        {
                            client.DownloadFileAsync(new Uri(sources[i]), destinations[i]);
                        }
                        else
                        {
                            cancelled = false;
                            l.Close();
                        }
                    };
                    client.DownloadFileAsync(new Uri(sources[i]), destinations[i]);
                }
            }, ct);
            l.ShowDialog(window);
            if (cancelled)
            {
                cts.Cancel();
                while (cancellationProcessed == false)
                {
                    Thread.Sleep(100);
                }
                for (int i = 0; i < destinations.Count(); ++i)
                {
                    try {
                        if (File.Exists(destinations[i]))
                        {
                            File.Delete(destinations[i]);
                        }
                    } catch {}
                }
            }
            return(!cancelled);
        }