Пример #1
0
        private async Task ScanPdf(QuestionsParameters parameters, string range, bool simulate = false)
        {
            string pdfFile = GetPdfFile();

            if (!zFile.Exists(pdfFile))
            {
                Trace.WriteLine($"pdf file not found \"{pdfFile}\"");
                return;
            }
            if (range == null)
            {
                range = parameters.PageRange;
            }
            Trace.WriteLine($"scan pdf \"{pdfFile}\" range \"{range}\"");
            //OcrWebService ocrWebService = CreateOcrWebService();
            ScanManager scanManager = CreateScanManager();

            //OcrRequest request = CreateOcrRequest();
            //request.DocumentFile = pdfFile;
            //request.PageRange = range;
            if (!simulate)
            {
                await scanManager.ScanPdf(pdfFile, range, zPath.Combine(GetScanDirectory(), "scan"));

                //OcrResult<OcrProcessDocumentResponse> response = await ocrWebService.ProcessDocument(request);
                //if (response.Success)
                //{
                //    Trace.WriteLine($"scan ok {response.Data.ProcessedPages} pages - remainder {response.Data.AvailablePages} pages");
                //    string directory = GetScanDirectory();
                //    zdir.CreateDirectory(directory);
                //    string scanFile = zPath.Combine(directory, "scan");
                //    Trace.WriteLine($"save scan to \"{scanFile}\"");
                //    await ocrWebService.DownloadResultDocuments(response.Data, scanFile);
                //}
                //else
                //    Trace.WriteLine($"scan error code {response.StatusCode}");
            }
            else
            {
                Trace.WriteLine($"simulate scan pdf \"{pdfFile}\" range \"{range}\"");
            }
        }
Пример #2
0
        //private static string GetScanZone(QuestionsParameters parameters, string imageFile)
        //{
        //    // format: "top:left:height:width,...", example "zone=0:0:100:100,50:50:50:50"
        //    if (parameters.PageColumn == 1)
        //        return null;
        //    else if (parameters.PageColumn == 2)
        //    {
        //        int width;
        //        int height;
        //        zimg.GetImageWidthHeight(imageFile, out width, out height);
        //        string zone;
        //        switch (parameters.PageRotate)
        //        {
        //            case PageRotate.NoRotate:
        //            case PageRotate.Rotate180:
        //                int width2 = width / 2;
        //                zone = $"0:0:{width2}:{height},{width2}:0:{width - width2}:{height}";
        //                break;
        //            case PageRotate.Rotate90:
        //            case PageRotate.Rotate270:
        //                int height2 = height / 2;
        //                zone = $"0:0:{width}:{height2},0:{height2}:{width}:{height - height2}";
        //                break;
        //            default:
        //                throw new PBException($"unknow page rotation {parameters.PageRotate}");
        //        }
        //        return zone;
        //    }
        //    else
        //        throw new PBException($"can't create scan zone for {parameters.PageColumn} columns");
        //}

        //private OcrRequest CreateOcrRequest()
        //{
        //    return new OcrRequest { Language = "french,english", OutputFormat = "txt" };
        //}

        //private OcrWebService CreateOcrWebService()
        //{
        //    XmlConfig config = XmlConfig.CurrentConfig;
        //    XmlConfig ocrWebServiceConfig = config.GetConfig("OcrWebServiceConfig");
        //    OcrWebService ocrWebService = new OcrWebService(ocrWebServiceConfig.GetExplicit("UserName"), ocrWebServiceConfig.GetExplicit("LicenseCode"), _timeout);
        //    //ocrWebService.UserName = ocrWebServiceConfig.GetExplicit("UserName");
        //    //ocrWebService.LicenseCode = ocrWebServiceConfig.GetExplicit("LicenseCode");
        //    string cacheDirectory = config.Get("OcrWebServiceCacheDirectory");
        //    if (cacheDirectory != null)
        //    {
        //        UrlCache urlCache = new UrlCache(cacheDirectory);
        //        urlCache.UrlFileNameType = UrlFileNameType.Host | UrlFileNameType.Path;
        //        if (config.Get("OcrWebServiceCacheDirectory/@option")?.ToLower() == "indexedfile")
        //            urlCache.IndexedFile = true;
        //        ocrWebService.HttpManager.SetCacheManager(urlCache);
        //    }
        //    return ocrWebService;
        //}

        private ScanManager CreateScanManager()
        {
            XmlConfig config = XmlConfig.CurrentConfig;
            XmlConfig ocrWebServiceConfig = config.GetConfig("OcrWebServiceConfig");
            UrlCache  urlCache            = null;
            string    cacheDirectory      = config.Get("OcrWebServiceCacheDirectory");

            if (cacheDirectory != null)
            {
                urlCache = new UrlCache(cacheDirectory);
                urlCache.UrlFileNameType = UrlFileNameType.Host | UrlFileNameType.Path;
                if (config.Get("OcrWebServiceCacheDirectory/@option")?.ToLower() == "indexedfile")
                {
                    urlCache.IndexedFile = true;
                }
            }
            ScanManager scanManager = new ScanManager(ocrWebServiceConfig.GetExplicit("UserName"), ocrWebServiceConfig.GetExplicit("LicenseCode"), urlCache);

            scanManager.Language     = "french,english";
            scanManager.OutputFormat = "txt";
            return(scanManager);
        }
Пример #3
0
        private async Task ScanImages(QuestionsParameters parameters, string range, bool simulate = false)
        {
            string directory = GetImagesDirectory();

            if (!zDirectory.Exists(directory))
            {
                Trace.WriteLine($"images directory not found \"{directory}\"");
                return;
            }
            if (range == null)
            {
                range = parameters.PageRange;
            }
            Trace.WriteLine($"scan images \"{directory}\" range \"{range}\"");
            //OcrWebService ocrWebService = CreateOcrWebService();
            ScanManager scanManager = CreateScanManager();

            //OcrRequest request = CreateOcrRequest();
            foreach (int page in zstr.EnumRange(range))
            {
                string imageBaseFile = zPath.Combine(directory, $"page-{page:000}");
                string imageFile     = FindImageFile(imageBaseFile);
                if (!zFile.Exists(imageFile))
                {
                    Trace.WriteLine($"image not found \"{imageBaseFile}\"");
                    return;
                }
                //request.DocumentFile = imageFile;
                //request.Zone = GetScanZone(parameters, imageFile);
                //if (!simulate)
                //{
                //    Trace.WriteLine($"scan image \"{imageFile}\"");
                //    await scanManager.ScanImage(imageFile, zPath.Combine(GetScanDirectory(), $"scan-page-{page:000}"));
                //    //OcrResult<OcrProcessDocumentResponse> response = await ocrWebService.ProcessDocument(request);
                //    //if (response.Success)
                //    //{
                //    //    string scanDirectory = GetScanDirectory();
                //    //    zdir.CreateDirectory(scanDirectory);
                //    //    string scanFile = zPath.Combine(scanDirectory, $"scan-page-{page:000}");
                //    //    Trace.WriteLine($"save scan to \"{scanFile}\"");
                //    //    await ocrWebService.DownloadResultDocuments(response.Data, scanFile);
                //    //}
                //}
                //else
                //{
                //    //Trace.WriteLine("OcrRequest :");
                //    //request.zTraceJson();
                //    Trace.WriteLine($"simulate scan image \"{imageFile}\"");
                //}
                foreach (ImageColumn imageColumn in GetScanImages(parameters, imageFile))
                {
                    string scanFile = zPath.Combine(GetScanDirectory(), $"scan-page-{page:000}");
                    if (imageColumn.Column != 0)
                    {
                        scanFile += $"-{imageColumn.Column:00}";
                    }
                    if (!simulate)
                    {
                        Trace.WriteLine($"scan image \"{imageColumn.ImageFile}\" to \"{scanFile}\"");
                        await scanManager.ScanImage(imageColumn.ImageFile, scanFile);
                    }
                    else
                    {
                        Trace.WriteLine($"simulate scan image \"{imageColumn.ImageFile}\" to \"{scanFile}\"");
                    }
                }
            }
        }