Beispiel #1
0
        private String recognizeFileAsync(IXConnection conn, String fileName, int timeoutSeconds)
        {
            // Open an event bus private to the session ticket and add a listener.
            // OCR results are send over this bus.
            EventBusApi.Bus eventBus = conn.EventBusApi.OpenEventBusChannel(conn.LoginResult.clientInfo.ticket);
            eventBus.AddListener(EventBusC.EVENT_OCR_RESULT);

            // Add a handler function for events.
            conn.EventBusApi.EventBusHandler += new EventBusApi.ProcessEventBusEvents(EventBusApi_EventBusHandler);

            OcrInfo ocrInfo = new OcrInfo();

            ocrInfo.recognizeFile                       = new OcrInfoRecognizeFile();
            ocrInfo.recognizeFile.imageData             = new FileData();
            ocrInfo.recognizeFile.imageData.data        = File.ReadAllBytes(fileName);
            ocrInfo.recognizeFile.imageData.contentType = ".TIF";
            ocrInfo.recognizeFile.recognizeLangs        = new String[] { "English" };
            ocrInfo.recognizeFile.timeoutSeconds        = timeoutSeconds;
            ocrInfo.recognizeFile.outputFormat          = OcrInfoC.TEXT;
            ocrInfo.recognizeFile.pageNo                = -1; // All pages

            // Set this properties for asynchronous processing:

            // Event bus that receives the result
            ocrInfo.busId = eventBus.Id;
            // ID for the request, unique for all open OCR requests on ocrInfo.busId
            ocrInfo.eventId = 123;

            // Process OCR
            conn.Ix.processOcr(ocrInfo);

            // Wait for result
            lock (syncObject)
            {
                long t1 = DateTime.Now.Ticks / 10000;
                int  dt = timeoutSeconds * 1000;
                while (result == null)
                {
                    Monitor.Wait(syncObject, dt);
                    long t2 = DateTime.Now.Ticks / 10000;
                    dt -= (int)(t2 - t1);
                    if (dt <= 0)
                    {
                        throw new Exception("Timeout");
                    }
                    t1 = t2;
                }
            }

            String text = result.recognizeFile.text;

            return(text);
        }
        public void run()
        {
            IXConnFactory connFact = null;
            IXConnection  ix1      = null;
            IXConnection  ix2      = null;

            // generate an event type that is most likely only used by me.
            myEventType = computeMyEventType();

            try
            {
                IXProperties connProps = IXConnFactory.CreateConnProperties(url);
                IXProperties sessOpts  = IXConnFactory.CreateSessionOptions("IX-Examples", "1.0");
                connFact = new IXConnFactory(connProps, sessOpts);
                Logger.instance().log("create IXConnFactory OK");

                // LOGIN
                Logger.instance().log("login connections...");
                ix1 = connFact.Create(userName, userPwd, "myComputer-conn1", null);
                ix2 = connFact.Create(userName, userPwd, "myComputer-conn2", null);
                // ci = ix.Login.ci
                Logger.instance().log("login OK");

                // Add event bus handlers.
                // This handlers are called for the received events from a background thread.
                // Thus we cannot update the user interfache in this threads directly.
                // The handlers will add the received events to the list receivedMessages and
                // the items of the list are printed from the main thread.
                ix1.EventBusApi.EventBusHandler += new EventBusApi.ProcessEventBusEvents(EventBusApi_EventBusHandler1);
                ix2.EventBusApi.EventBusHandler += new EventBusApi.ProcessEventBusEvents(EventBusApi_EventBusHandler2);

                // Add listeners to our private event.
                ix1.EventBusApi.BroadcastBus.AddListener(myEventType);
                ix2.EventBusApi.BroadcastBus.AddListener(myEventType);

                Logger.instance().log("conn1.subsId=" + ix1.EventBusApi.SubscriberId);
                Logger.instance().log("conn2.subsId=" + ix2.EventBusApi.SubscriberId);

                // Each connection sends 3 events to the other connection.
                // Here, the event data is a simple string. But it could be a Sord e.g. too.
                Logger.instance().log("send events");
                for (int i = 0; i < 3; i++)
                {
                    EventBusApi.Bus bbus1 = ix1.EventBusApi.BroadcastBus;
                    bbus1.Send(myEventType, "Hello conn2 - " + i);

                    EventBusApi.Bus bbus2 = ix2.EventBusApi.BroadcastBus;
                    bbus2.Send(myEventType, "Hello conn1 - " + i);
                }

                // Wait for the events and print them.
                Logger.instance().log("wait 1");
                Thread.Sleep(1 * 1000);

                // Print received messages
                Logger.instance().log("received:");
                foreach (String s in receivedMessages)
                {
                    Logger.instance().log(s);
                }

                if (receivedMessages.Count != 6)
                {
                    Logger.instance().log("Error: 6 events should have been received");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // Logout
                // --
                if (ix1 != null && ix2 != null)
                {
                    Logger.instance().log("IX logout...");
                    ix1.Logout();
                    ix2.Logout();
                    Logger.instance().log("IX logout OK");
                }
            }
        }