Beispiel #1
0
        private int getPicture()
        {
            int progress = 0;//0:progress 1:successfuly finished  -1:Failed

            try
            {
                //// Read the data from the stream in chunks
                byte[] buffer = new byte[constants.CHUNK_SIZE];
                //updateUI("Waiting for data.  Expecting " + progressData_Remaining + " more bytes.");
                int bytesRead = socketStream.Read(buffer, 0, buffer.Length);

                count++;
                int prg = 100 - (int)((progressData_Remaining / progressData_Total) * 100.0);

                METState.Current.METCoreObject.SendToForm("(" + prg + "%) : Read " + bytesRead + " bytes into buffer", "tbOutput");
                METState.Current.METCoreObject.SendToForm(prg, "progressbar");


                dataOutputStream.Write(buffer, 0, bytesRead);

                //dataOutputStream.WriteByte(buffer);


                progressData_Remaining -= bytesRead;

                if (progressData_Remaining <= 0)
                {
                    // check the integrity of the data
                    byte[] data = dataOutputStream.ToArray();

                    byte[] receivedDigest = GetMd5Hash(data);
                    socketStream.Write(receivedDigest, 0, receivedDigest.Length);
                    socketStream.Flush();

                    // check the integrity of the data
                    if (digestMatch(receivedDigest, digest))
                    {
                        METState.Current.METCoreObject.SendToForm("Picture received", "tbOutput");
                        METState.Current.METCoreObject.SendToForm(0, "progressbar");

                        bitmap = Image.FromStream(dataOutputStream);

                        FXPAL_Utils.SetupHyperImage(bitmap);

                        string folder = @"fromGlass\Images\";

                        if (!Directory.Exists(folder))
                        {
                            Directory.CreateDirectory(folder);
                        }

                        String photoTime      = DateTime.Now.ToString("hh.mm.ss");
                        String SuspiciousPath = Path.Combine(folder, photoTime + ".jpg");
                        // bitmap.Save(SuspiciousPath);


                        dataOutputStream.Flush();
                        dataOutputStream.Close();
                        progress = 1;
                    }
                    else
                    {
                        METState.Current.METCoreObject.SendToForm("Data NOT received completely!", "tbOutput");

                        progress = -1;
                    }
                }
            }
            catch (IOException e)
            {
                progress = -1;
            }
            return(progress);
        }
Beispiel #2
0
        private int getJson()
        {
            int progress = 0;//0:progress 1:successfuly finished  -1:Failed

            try
            {
                //// Read the data from the stream in chunks
                byte[] buffer = new byte[constants.CHUNK_SIZE];
                //updateUI("Waiting for data.  Expecting " + progressData_Remaining + " more bytes.");
                int bytesRead = socketStream.Read(buffer, 0, buffer.Length);


                int prg = 100 - (int)((progressData_Remaining / progressData_Total) * 100.0);

                METState.Current.METCoreObject.SendToForm("(" + prg + "%) : Read " + bytesRead + " bytes into buffer", "tbOutput");
                METState.Current.METCoreObject.SendToForm(prg, "progressbar");

                progressData_Remaining -= bytesRead;

                if (progressData_Remaining >= 0)
                {
                    dataOutputStream.Write(buffer, 0, bytesRead);
                }
                else
                {
                    dataOutputStream.Write(buffer, 0, (int)(bytesRead - Math.Abs(progressData_Remaining)));
                }

                //dataOutputStream.WriteByte(buffer);



                if (progressData_Remaining <= 0)
                {
                    // check the integrity of the data
                    byte[] data = dataOutputStream.ToArray();

                    String jsonString = Encoding.UTF8.GetString(data);//didn't help     .TrimEnd('\0');

                    var json_serializer = new JavaScriptSerializer();

                    string folder = @"fromGlass\Jsons\";
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    {//MagicPointing
                     //myJsonClass mjson = new myJsonClass();
                     //mjson = json_serializer.Deserialize<myJsonClass>(jsonString);
                     //myJsonClass_test temp = new myJsonClass_test(mjson);
                     //System.IO.StreamWriter file = new System.IO.StreamWriter(folder + temp.name + ".csv");
                     //file.Write(temp.text);

                        //file.Close();
                    }
                    {
                        Bitmap keptImage = new Bitmap(10, 10);
                        if (FXPAL_Utils.mHyperImage != null)
                        {
                            keptImage = new Bitmap(FXPAL_Utils.mHyperImage.img);
                        }
                        FXPAL_Utils.mHyperImage = json_serializer.Deserialize <HyperImage>(jsonString);
                        if (FXPAL_Utils.mHyperImage.img == null)
                        {
                            FXPAL_Utils.mHyperImage.img = keptImage;
                        }
                        String SuspiciousPath = Path.Combine(folder, FXPAL_Utils.mHyperImage.name + ".jpg");
                        FXPAL_Utils.mHyperImage.img.Save(SuspiciousPath);
                        HyperImage_without_image temp = new HyperImage_without_image(FXPAL_Utils.mHyperImage);
                        System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
                            new System.Web.Script.Serialization.JavaScriptSerializer();
                        String s = jSearializer.Serialize(temp);
                        System.IO.File.WriteAllText(Path.Combine(folder, temp.name + ".txt"), s);
                        FXPAL_Utils.UpdateUI();
                    }
                    {//calib experiment
                     //myJsonClass mjson = new myJsonClass();
                     //mjson = json_serializer.Deserialize<myJsonClass>(jsonString);


                        //    // ProcessJson_test(mjson);
                        //    CalibExp.ProcessJson(mjson);
                    }


                    METState.Current.METCoreObject.SendToForm("Picture received", "tbOutput");
                    METState.Current.METCoreObject.SendToForm(0, "progressbar");



                    dataOutputStream.Flush();
                    dataOutputStream.Close();
                    progress = 1;
                }
            }
            catch (IOException e)
            {
                progress = -1;
            }
            return(progress);
        }
Beispiel #3
0
        /// <summary>
        ///   Runs this instance.
        /// </summary>
        public void Run()
        {
            METState.Current.METCoreObject.SendToForm(0, "progressbar");

            ////UDP
            int cnt = 0;

            byte[] message;
            string msg = "";

            byte[] received;

            METState.Current.GlassServer.Send(myGlass.MessageType.toGLASS_WHAT_IS_YOUR_IP);

            Thread thdUDPServer = new Thread(new ThreadStart(serverThread));

            thdUDPServer.Start();


            Thread.Sleep(500);

            do
            {
                try
                {
                    switch (mode)
                    {
                    case Mode.mainLoop:
                    {
                        received = new byte[constants.MSG_SIZE];
                        //socketStream.ReadTimeout = 10000;
                        int tmp = socketStream.Read(received, 0, received.Length);


                        if (tmp <= 0)
                        {
                            cnt++;
                            break;
                        }
                        else if (tmp == 12)
                        {
                            //msg = System.Text.Encoding.UTF8.GetString(received, 0, tmp);//.TrimEnd('\0');



                            //String received_MSG = myGlass.MessageType.commands.SingleOrDefault(x => x.Value == GetIndicator(received)).Key;
                            //METState.Current.METCoreObject.SendToForm("Received msg: " + received_MSG, "tbOutput");


                            interpretMSG(received);
                        }
                    }
                    break;

                    case Mode.waitingForJSON_size:
                    {
                        byte[] buffer = new byte[4];
                        //updateUI("Waiting for data.  Expecting " + progressData_Remaining + " more bytes.");
                        int bytesRead = METState.Current.GlassServer.client.socketStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 4)
                        {
                            progressData_Total     = byteArrayToInt(buffer);
                            progressData_Remaining = progressData_Total;
                            dataOutputStream       = new MemoryStream();

                            mode = Mode.waitingForJSON;
                        }
                        else
                        {
                            mode = Mode.mainLoop;
                        }
                    }
                    break;

                    case Mode.waitingForJSON:
                    {
                        int prg = getJson();

                        if (prg == 1)
                        {
                            mode = Mode.mainLoop;
                            METState.Current.GlassServer.Send(MessageType.toGLASS_DataReceived);
                        }
                        else if (prg == -1)
                        {
                            mode = Mode.mainLoop;
                        }
                    }
                    break;

                    case Mode.waitingForHeader:
                    {
                        int prg = getHeader();

                        if (prg == -1)
                        {
                            mode = Mode.mainLoop;
                            //Calibration terminated!
                            server.Send(myGlass.MessageType.toGLASS_Calibrate_Scene, new Point(-3, -3));        //point x (th) of y (total)
                        }

                        else if (prg == 1)
                        {
                            mode = Mode.waitingForpicture;
                        }
                    }
                    break;

                    case Mode.waitingForpicture:
                    {
                        int prg = getPicture();

                        if (prg == 1)
                        {
                            if (snapshot)
                            {
                                mode = Mode.mainLoop;

                                #region Draw gaze  on image
                                if (METState.Current.ShowGaze)
                                {
                                    //This will show the gaze obtained from eyeToScene when taking the snapshot

                                    EmgImgProcssing.DrawCircle((Bitmap)bitmap,

                                                               Convert.ToInt32(METState.Current.Gaze_SnapShot_Glass.X), Convert.ToInt32(METState.Current.Gaze_SnapShot_Glass.Y), System.Drawing.Color.LightGreen);


                                    FXPAL_Utils.UpdateUI();
                                }
                                #endregion Draw gaze cross on image


                                //METState.Current.METCoreObject.SendToForm(bitmap, "imScene");
                                //METState.Current.METCoreObject.SendToForm("", "Update Glass Picturebox");

                                METState.Current.GlassFrontView_Resolution = new Size(bitmap.Width, bitmap.Height);

                                currentImage = bitmap;


                                snapshot = false;
                            }

                            else if (CalibExp.scene_is_sampling)
                            {
                                mode         = Mode.mainLoop;
                                currentImage = new Bitmap(bitmap);//This will be automatically shown in the form

                                CalibExp.mySampling_Scene.ProcessImg(bitmap);


                                METState.Current.GlassFrontView_Resolution = new Size(bitmap.Width, bitmap.Height);
                            }
                            else if (myCalibration_Scene.is_sampling)        //calibration
                            {
                                mode         = Mode.mainLoop;
                                currentImage = new Bitmap(bitmap);//This will be automatically shown in the form

                                myCalibration_Scene.ProcessImg(bitmap);


                                METState.Current.GlassFrontView_Resolution = new Size(bitmap.Width, bitmap.Height);
                            }
                        }
                        else if (prg == -1)
                        {
                            mode = Mode.mainLoop;
                        }
                    }
                    break;
                    }
                }
                catch (IOException exception)
                {
                    break;
                }
            }while (cnt == 0 && !msg.StartsWith("CLIENT>>> TERMINATE") && tcpClient.Client.Connected); //cnt < 10 &&

            // close the socket connection
            finish("Client has disconnected!!!!");

            METState.Current.METCoreObject.SendToForm("*********************************************\r\n", "tbOutput");
        }