Beispiel #1
0
        public static OcrEngine Create(OcrEngineType type, string language)
        {
            OcrEngine ocrEngine     = null;
            Type      ocrEngineType = GetType(type);

            if (ocrEngineType != null)
            {
                ocrEngine          = (OcrEngine)Activator.CreateInstance(ocrEngineType);
                ocrEngine.Language = language;
            }
            return(ocrEngine);
        }
Beispiel #2
0
        private static void UpdateRemoteEngines(string ip)
        {
            if (string.IsNullOrWhiteSpace(ip))
            {
                return;
            }

            try
            {
                using (WebClient webClient = new WebClient())
                {
                    string url = "http://" + ip + ":" + port + "/" + GetEnginesCommand;

                    webClient.Proxy    = null;
                    webClient.Encoding = Encoding.UTF8;
                    string response = webClient.DownloadString(url);

                    string[] splitted = response.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string value in splitted)
                    {
                        try
                        {
                            string[] splitted2 = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            string        typeStr   = splitted2[0];
                            List <string> languages = new List <string>();
                            for (int i = 1; i < splitted2.Length; i++)
                            {
                                languages.Add(splitted2[i]);
                            }

                            OcrEngineType type = (OcrEngineType)Enum.Parse(typeof(OcrEngineType), typeStr);
                            if (!OcrEngine.Create(type).IsInstalled)
                            {
                                if (!RemoteTypes.ContainsKey(ip))
                                {
                                    RemoteTypes[ip] = new Dictionary <OcrEngineType, List <string> >();
                                }

                                RemoteTypes[ip][type] = languages.ToList();
                                RemoteOcr.SetRemote(type);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            catch
            {
            }
        }
Beispiel #3
0
 private static void RunGoogleOcrTests()
 {
     while (true)
     {
         OcrEngine abbyy = OcrEngine.Create(OcrEngineType.Google);//OcrEngineType.ABBYY);
         MessageBox.Show("Installed: " + abbyy.IsInstalled);
         Stopwatch sw1 = new Stopwatch();
         sw1.Start();
         if (abbyy.IsInstalled)
         {
             OcrResult result = abbyy.LoadFile("image1.png", "ko");
             if (result != null)
             {
                 sw1.Stop();
                 if (!string.IsNullOrEmpty(result.Text))
                 {
                     Clipboard.SetText(result.Text);
                 }
                 MessageBox.Show(result.ResultType + Environment.NewLine + result.Text + " " + sw1.Elapsed);
             }
         }
     }
 }
Beispiel #4
0
 private static void RunNicomsoftOcrTests()
 {
     while (true)
     {
         OcrEngine engine = OcrEngine.Create(OcrEngineType.Nicomsoft);
         MessageBox.Show("Installed: " + engine.IsInstalled);
         Stopwatch sw1 = new Stopwatch();
         sw1.Start();
         if (engine.IsInstalled)
         {
             OcrResult result = engine.LoadFile("image1.png", "ko");
             if (result != null)
             {
                 sw1.Stop();
                 if (!string.IsNullOrEmpty(result.Text))
                 {
                     Clipboard.SetText(result.Text);
                 }
                 MessageBox.Show(result.ResultType + Environment.NewLine + result.Text + " " + result.Error + " " + sw1.Elapsed);
             }
         }
     }
 }
Beispiel #5
0
            private void Process(HttpListenerContext context)
            {
                try
                {
                    string url = context.Request.Url.OriginalString;

                    string response = string.Empty;

                    if (url.Contains("favicon.ico"))
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        context.Response.OutputStream.Close();
                        return;
                    }

                    if (url.Contains(GetEnginesCommand))
                    {
                        if (Program.Engines != null)
                        {
                            Dictionary <OcrEngineType, OcrEngine> engines = new Dictionary <OcrEngineType, OcrEngine>(Program.Engines);
                            foreach (OcrEngine engine in engines.Values)
                            {
                                if (engine.IsInstalled)
                                {
                                    string languages = string.Empty;
                                    foreach (string language in engine.SupportedLanguages.Keys)
                                    {
                                        languages += language + ",";
                                    }
                                    languages = languages.TrimEnd(',');
                                    response += engine.Name + "," + languages + "|";
                                }
                            }
                            response = response.TrimEnd('|', ',');
                        }
                    }
                    else if (url.Contains(RunOcrCommand))
                    {
                        Dictionary <string, string> postData = GetPostData(context);
                        OcrEngineType type     = (OcrEngineType)Enum.Parse(typeof(OcrEngineType), postData["type"]);
                        string        language = postData["language"];
                        byte[]        data     = Convert.FromBase64String(postData["data"]);
                        OcrImage      image    = OcrImage.FromBinary(data);
                        OcrResult     result   = null;

                        try
                        {
                            result = OcrEngine.Create(type).Load(image, language);
                        }
                        catch (Exception e)
                        {
                            result = OcrResult.Create(OcrResultType.Exception, e.ToString());
                        }

                        try
                        {
                            response = HttpUtility.UrlEncode(Convert.ToBase64String(result.ToBinary()));
                        }
                        catch
                        {
                        }
                    }

                    byte[] resposeBuffer = Encoding.UTF8.GetBytes(response.ToString());

                    context.Response.ContentType     = "text/html";
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.ContentLength64 = resposeBuffer.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.AddHeader("Last-Modified", DateTime.Now.ToString("r"));
                    context.Response.OutputStream.Write(resposeBuffer, 0, resposeBuffer.Length);
                    context.Response.OutputStream.Flush();
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                }
                catch
                {
                }

                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.OutputStream.Close();
            }
Beispiel #6
0
 public static bool IsRemote(OcrEngineType type)
 {
     return(IsRemote(OcrEngine.GetType(type)));
 }
Beispiel #7
0
 public static void SetRemote(OcrEngineType type)
 {
     remoteTypes[OcrEngine.GetType(type)] = type;
 }
Beispiel #8
0
        public static void LoadFileAsync(OcrEngineType type, string file, string language, Action <OcrResult> callback)
        {
            OcrEngine ocrEngine = Create(type);

            ocrEngine.LoadFile(file, language);
        }
Beispiel #9
0
        public static OcrResult LoadFile(OcrEngineType type, string file, string language)
        {
            OcrEngine ocrEngine = Create(type);

            return(ocrEngine.LoadFile(file, language));
        }