The Easiest Way to Save and Share Code Snippets on the web

WiseTrend OCR API C# Code Sample

csharp

posted: Oct, 22nd 2010 | jump to bottom

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Net;
using System.Threading;
 
namespace OCRSampleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string imageUrl = "http://forums.adobe.com/servlet/JiveServlet/showImage/2-2038783-5434/articleweb.jpg"; //REPLACE WITH YOUR OWN IMAGE
            string key = "aLxwa12eVjcP02XiuGzF0vtkhdb3UMpq"; //REPLACE WITH YOUR OWN KEY, GET THE KEY AT http://wwww.wisetrend.com/wisetrend_ocr_cloud.shtml
 
            XElement xe = new XElement("Job",
                    new XElement("InputURL", imageUrl)
                );
 
            WebClient wc = new WebClient();
            wc.Headers[HttpRequestHeader.ContentType] = "text/xml";
            ServicePointManager.Expect100Continue = false;
 
            string results = wc.UploadString(
                    "https://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey=" + key,
                    "POST",
                    xe.ToString()
                );
 
            string textURL = null;
 
            //Use "dumb polling" every 2 seconds in this simplified example, until done.
            //In a real application, notification with NotifyURL should be used instead.
            while (true)
            {
                XElement resultsXml = XElement.Parse(results);
                string jobURL = resultsXml.Element("JobURL").Value;
                string status = resultsXml.Element("Status").Value;
 
                if (status == "Finished")
                {
                    textURL = (
                        from elem in resultsXml.Element("Download").Elements("File")
                        where elem.Element("OutputType").Value == "TXT"
                        select elem.Element("Uri").Value
                       ).First();
                    break;
                }
 
                //Exit if there's an error
                if ((status != "Submitted") && (status != "Processing")) break;
 
                Thread.Sleep(2000); // 2 seconds
                results = wc.DownloadString(jobURL);
            }
 
            if (textURL == null)
            {
                Console.WriteLine("An error has occurred");
            }
            else
            {
                string text = wc.DownloadString(textURL);
                Console.WriteLine(text);
            }
            Console.WriteLine("PRESS ENTER TO EXIT");
            Console.ReadLine();
        }
    }
}
398 views