コード例 #1
0
ファイル: RestOCR.cs プロジェクト: FrankLN/ImageToText
        public static string ProcessDocument(string user_name, string license_code, string file_path)
        {
            string ocrResult = "";
            string ocrURL    = @"http://www.ocrwebservice.com/restservices/processDocument?language=danish&gettext=true";

            byte[] uploadData = GetUploadedFile(file_path);

            HttpWebRequest request = CreateHttpRequest(ocrURL, user_name, license_code, "POST");

            request.ContentLength = uploadData.Length;

            //  Send request
            using (Stream post = request.GetRequestStream())
            {
                post.Write(uploadData, 0, (int)uploadData.Length);
            }

            try
            {
                //  Get response
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Parse JSON response
                    string          strJSON     = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    OCRResponseData ocrResponse = JsonConvert.DeserializeObject <OCRResponseData>(strJSON);

                    ocrResult = PrintOCRData(ocrResponse);

                    // Download output converted file
                    if (!string.IsNullOrEmpty(ocrResponse.OutputFileUrl))
                    {
                        HttpWebRequest request_get = (HttpWebRequest)WebRequest.Create(ocrResponse.OutputFileUrl);
                        request_get.Method = "GET";

                        using (HttpWebResponse result = request_get.GetResponse() as HttpWebResponse)
                        {
                            DownloadConvertedFile(result, "C:\\converted_file.doc");
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                Console.WriteLine(string.Format("OCR API Error. HTTPCode:{0}", ((HttpWebResponse)wex.Response).StatusCode));
            }
            return(ocrResult);
        }
コード例 #2
0
ファイル: RestOCR.cs プロジェクト: FrankLN/ImageToText
        private static string PrintOCRData(OCRResponseData ocrResponse)
        {
            string result = "";

            // Available pages
            //Console.WriteLine("Available pages: " + ocrResponse.AvailablePages);

            // Extracted text. For zonal OCR: OCRText[z][p]    z - zone, p - pages
            for (int zone = 0; zone < ocrResponse.OCRText.Count; zone++)
            {
                for (int page = 0; page < ocrResponse.OCRText[zone].Count; page++)
                {
                    //Console.WriteLine(string.Format("Extracted text from page №{0}, zone №{1} :{2}", page, zone, ocrResponse.OCRText[zone][page]));
                    result += ocrResponse.OCRText[zone][page];
                }
            }

            return(result);
        }