Beispiel #1
0
        public void PrepareOCR(Stream[] streams, string name = "Plik")
        {
            if (updateWorker != null && !updateWorker.IsCompleted)
            {
                updateWorkerCTS.Cancel();
                try {
                    updateWorker.Wait();
                } catch {}
                updateWorker = null;
            }
            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}");
                    wnd_waiter.SetPercentage((int)(100 * status.PageCurrent / status.PageCount));
                };
                OCRResult result = OCR.GetTextFromStreamsAsync(streams, this.Engine, status).Result;
                if (result != null)
                {
                    wnd.SetResult(name, 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;
            }
        }
Beispiel #2
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;
            }
        }