Esempio n. 1
0
        private void Savebtn_Click(object sender, EventArgs e)
        {
            HImage refImg = new HImage();
            string PathPJ = Environment.CurrentDirectory;

            PathPJ = PathPJ.Replace("bin\\Debug", "");
            string PathProgram = "C:\\Users\\Advantech\\Desktop\\CU-TNS_LightGuide\\ExecProcedures\\vs2005\\Picture\\PicRef\\";

            Framegrabber.GrabImageStart(-1);
            refImg = Framegrabber.GrabImageAsync(-1);
            refImg.WriteImage("tiff", 0, PathProgram + "Good_Shot" + shotComboBox.SelectedItem);
            refImg.Dispose();
            StopBtn_Click(sender, e);
        }
        public GrabberEventViewModel SnapShot()
        {
            var viewModel = new GrabberEventViewModel()
            {
                Result = new GrabberEventResult()
                {
                    State = GrabberEventState.Busy
                },
                ReportHandler = On_ConnectReport
            };

            try
            {
                setHFramegrabber();

                _hFrameGrabber.GrabImageStart(_delay);
                var himage = _hFrameGrabber.GrabImage();
                viewModel.Result.State = GrabberEventState.Done;
            }
            catch (Exception ex)
            {
                viewModel.Result.Error = ex;
                Hanbo.Log.LogManager.Debug("SnapShot Exception:" + ex.Message);
                Hanbo.Log.LogManager.Debug("SnapShot StackTrace:" + ex.StackTrace);
            }
            finally
            {
                //c中斷
                if (_hFrameGrabber != null)
                {
                    _hFrameGrabber.Dispose();
                }
            }
            return(viewModel);
        }
Esempio n. 3
0
        public void OpenDevice(HTuple acqHandle)
        {
            InitIA();
            img        = null;
            imCounter  = 0;
            grabThread = null;
            using (frameGrabber = new HFramegrabber(acqHandle[0].IP))
            {
                // Initialize acqHandle
                acqHandle = new HTuple(frameGrabber.Handle);
            }

            // try to start asynchronous grab
            try
            {
                frameGrabber.GrabImageStart(-1.0);
            }
            catch (HOperatorException exc)
            {
                // Catch the error, if the device doesn't support
                // asynchronous mode: H_ERR_FGASYNC
                if (exc.GetErrorCode() == 5320)
                {
                    asyncGrabbing = false;
                }
                else
                {
                    throw exc;
                }
            }
        }
Esempio n. 4
0
        /*********************************************************************
        * Open and Close Device
        *********************************************************************/

        /// <summary>
        /// Initialize the connection to the image acquisition interface and
        /// initialization of its image acquisition parameters
        /// </summary>
        public void OpenDevice()
        {
            InitIA();
            img        = null;
            imCounter  = 0;
            grabThread = null;

            try
            {
                if (this.IAName != "File")
                {
                    OpenFrameGrabber();
                }
                else
                {
                    OpenOfflineIADevice();
                }

                // Initialize acqHandle
                acqHandle = new HTuple(frameGrabber.Handle);
            }
            catch (HOperatorException exp)
            {
                acqHandle = null;
                throw exp;
            }

            // try to start asynchronous grab
            try
            {
                frameGrabber.GrabImageStart(-1.0);
            }
            catch (HOperatorException exc)
            {
                // Catch the error, if the device doesn't support
                // asynchronous mode: H_ERR_FGASYNC
                if (exc.GetErrorCode() == 5320)
                {
                    asyncGrabbing = false;
                }
                else
                {
                    throw exc;
                }
            }
        }
        private void _bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;

            try
            {
                _hFrameGrabber.GrabImageStart(_delay);
                if (GrabImageStart != null)
                {
                    GrabImageStart(sender, e);
                }
                while (true)
                {
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        try
                        {
                            var himage = _hFrameGrabber.GrabImageAsync(_delay);
                            worker.ReportProgress(1, himage);
                            _currentImage = himage;
                        }
                        catch (HalconDotNet.HOperatorException ex)
                        {
                            Hanbo.Log.LogManager.Error(ex);
                            worker.ReportProgress(99, ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Hanbo.Log.LogManager.Error(ex);
                if (GrabImageException != null)
                {
                    GrabImageException(ex);
                }
            }
        }
        public void OpenDevice(HTuple acqHandle)
        {
            InitIA();
            img = null;
            imCounter = 0;
            grabThread = null;
            using (frameGrabber = new HFramegrabber(acqHandle[0].IP))
            {
                // Initialize acqHandle
                acqHandle = new HTuple(frameGrabber.Handle);
            }

            // try to start asynchronous grab
            try
            {
                frameGrabber.GrabImageStart(-1.0);
            }
            catch (HOperatorException exc)
            {
                // Catch the error, if the device doesn't support
                // asynchronous mode: H_ERR_FGASYNC
                if (exc.GetErrorCode() == 5320)
                {
                    asyncGrabbing = false;
                }
                else
                {
                    throw exc;
                }
            }
        }
Esempio n. 7
0
        private async void uploadDB(String licenseTxt, String containerTxt, HImage licImage, HImage conImage)
        {
            {
                var firebase = new FirebaseClient("https://nhp-container.firebaseio.com/");
                var count    = await firebase
                               .Child("count")
                               .OnceSingleAsync <int>();

                Console.WriteLine("Count : " + count);
                t = DateTime.Now;
                String date = t.ToString("dd-MM-yyyy");
                String time = t.ToString("HH:mm:ss");
                var    post = Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    Date      = date,
                    Time      = time,
                    License   = licenseTxt,
                    Container = containerTxt
                });
                // add new item to list of data and let the client generate new key for you (done offline)
                await firebase
                .Child("car")
                .Child((count + 1).ToString())
                .PutAsync(post);

                // note that there is another overload for the PostAsync method which delegates the new key generation to the firebase server
                //Console.WriteLine($"Key for the new license: {upload.Key}");
                count += 1;
                await firebase
                .Child("count")
                .PutAsync(count);

                keyUpload = (count).ToString();
            }

            {
                Console.WriteLine("key : " + keyUpload);
                // Get any Stream - it can be FileStream, MemoryStream or any other type of Stream
                //var stream = File.Open(@"C:\Users\donna\Desktop\save.jpg", FileMode.Open);
                Image timage1, timage2;
                topView.GrabImageStart(-1);
                if (engMode)
                {
                    var image  = topView.GrabImageAsync(-1);
                    var image2 = topView.GrabImageAsync(-1);
                    timage1 = himage2image(image);
                    timage2 = himage2image(image2);
                }
                else
                {
                    timage1 = himage2image(licImage);
                    timage2 = himage2image(conImage);
                }
                var st1 = ToStream(timage1, ImageFormat.Jpeg);
                var st2 = ToStream(timage2, ImageFormat.Jpeg);
                // Constructr FirebaseStorage, path to where you want to upload the file and Put it there
                var task1 = new FirebaseStorage("nhp-container.appspot.com")
                            .Child("car")
                            .Child(keyUpload)
                            .Child("licImage.jpg")
                            .PutAsync(st1);
                var task2 = new FirebaseStorage("nhp-container.appspot.com")
                            .Child("car")
                            .Child(keyUpload)
                            .Child("conImage.jpg")
                            .PutAsync(st2);
                // Track progress of the upload
                task1.Progress.ProgressChanged += (s1, e1) => Console.WriteLine($"Progress: {e1.Percentage} %");
                task2.Progress.ProgressChanged += (s2, e2) => Console.WriteLine($"Progress: {e2.Percentage} %");
                // await the task to wait until upload completes and get the download url
                var downloadUrl1 = await task1;
                Console.WriteLine("Upload Complete, URL = " + downloadUrl1);
                var downloadUrl2 = await task2;
                Console.WriteLine("Upload Complete, URL = " + downloadUrl2);
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            HTuple        interfaces;
            HTuple        devices;
            HTuple        callbackTypes;
            HTuple        openDefaults;
            long          callbackIndex;
            long          interfaceIndex;
            long          deviceIndex;
            HFramegrabber acq                  = null;
            Payload       userContext          = default(Payload);
            bool          bWaitUserInteraction = true;

            try
            {
                gAcqCondition = new HCondition("", "");
                gAcqMutex     = new HMutex("", "");

                // Get available interfaces.
                interfaces = showAvailableInterfaces();

                if (interfaces.Length < 1)
                {
                    exitProgram(ref acq, null, bWaitUserInteraction);
                }

                Console.Write(System.Environment.NewLine +
                              "Enter the number of the interface you want to use: ");

                interfaceIndex = readUserSelection(interfaces.Length);

                // Show selected interface and revision.
                HTuple revision;
                HInfo.InfoFramegrabber(
                    interfaces.TupleSelect(interfaceIndex - 1).S.ToString(), "revision",
                    out revision);
                Console.WriteLine(
                    System.Environment.NewLine + "Interface selected: " +
                    interfaces.TupleSelect(interfaceIndex - 1).S.ToString() + " (Rev. " +
                    revision.S.ToString() + ")");

                // Get available devices for the selected interface.
                devices =
                    showAvailableDevices(interfaces.TupleSelect(interfaceIndex - 1));

                if (devices.Length < 1)
                {
                    exitProgram(ref acq, null, bWaitUserInteraction);
                }

                Console.Write(System.Environment.NewLine +
                              "Enter the number of the device you want to connect to: ");

                deviceIndex = readUserSelection(devices.Length);

                // Get open_framegrabber default values and open the selected device.
                Console.WriteLine(
                    System.Environment.NewLine +
                    "Open the specified device by calling open_framegrabber()...");

                HInfo.InfoFramegrabber(
                    interfaces.TupleSelect(interfaceIndex - 1).S.ToString(), "defaults",
                    out openDefaults);

                int vRes = 0, hRes = 0, iWidth = 0, iHeight = 0, sRow = 0, sColumn = 0;
                try
                {
                    hRes    = Convert.ToInt32(openDefaults.TupleSelect(0).L);
                    vRes    = Convert.ToInt32(openDefaults.TupleSelect(1).L);
                    iWidth  = Convert.ToInt32(openDefaults.TupleSelect(2).L);
                    iHeight = Convert.ToInt32(openDefaults.TupleSelect(3).L);
                    sRow    = Convert.ToInt32(openDefaults.TupleSelect(4).L);
                    sColumn = Convert.ToInt32(openDefaults.TupleSelect(5).L);
                }
                catch (Exception)
                {
                    // Failed to convert one of the parameters.
                    Console.WriteLine("Failed to convert long to int");
                    exitProgram(ref acq, null, bWaitUserInteraction);
                }
                acq = new HFramegrabber(
                    interfaces.TupleSelect(interfaceIndex - 1).S.ToString(), hRes, vRes,
                    iWidth, iHeight, sRow, sColumn,
                    openDefaults.TupleSelect(6).S.ToString(),
                    openDefaults.TupleSelect(7), openDefaults.TupleSelect(8),
                    openDefaults.TupleSelect(9),
                    openDefaults.TupleSelect(10).S.ToString(), cCameraType,
                    devices.TupleSelect(deviceIndex - 1), openDefaults.TupleSelect(13),
                    openDefaults.TupleSelect(14));

                // If the interface supports it, enable a helper that activates GenICam
                // events during SetFramegrabberCallback.
                try
                {
                    acq.SetFramegrabberParam("event_notification_helper", "enable");
                }
                catch (Exception) {}

                // Show the available callback types and register a specific one.
                callbackTypes = showAvailableCallbackTypes(ref acq);

                if (callbackTypes.Length < 1)
                {
                    exitProgram(ref acq, null, bWaitUserInteraction);
                }

                Console.Write(System.Environment.NewLine + "Enter the number of the " +
                              "callback type you like to register: ");

                callbackIndex = readUserSelection(callbackTypes.Length);

                // Use the userContext to pass your own class or structure with the
                // information that you need on the other side of the callback.
                // Since we are using Thread.Start(obj) save the variables we need
                // in userContext, before passing it to the Thread.
                // Note that if you want to do changes to 'acq' on different threads
                // the you should sincronize all thread an use a single
                // 'acq' reference. Additionally, if you change any of the
                //  internal values of 'userContext' will not be reflected
                //  on the other side.
                userContext = new Payload(acq, callbackTypes.TupleSelect(callbackIndex - 1));

                // Start worker thread to manage the incoming callbacks. With the
                // provided userContext.
                gThreadRunning = false;
                gThreadHandle  = new Thread(new ParameterizedThreadStart(workerThread));
                gThreadHandle.Start(userContext);

                gAcqMutex.LockMutex();

                // Register the callback after we start the thread. This is to avoid
                // cases where the events start comming immediately. If you don't do
                // it this way you might lose some events.
                if (!registerCallbackType(ref acq, userContext.eventType, userContext))
                {
                    gAcqMutex.UnlockMutex();
                    exitProgram(ref acq, userContext.eventType.S.ToString(),
                                bWaitUserInteraction);
                }

                // Please place your code here, to force the execution of a specific
                // callback type.

                Console.WriteLine(
                    System.Environment.NewLine +
                    "If the registered event is signaled, the previously registered" +
                    System.Environment.NewLine +
                    "user-specific callback function will be executed." +
                    System.Environment.NewLine);

                // e.g. 'ExposureEnd'
                Console.WriteLine(
                    "Start grabbing an image." + System.Environment.NewLine +
                    "If e.g. an exposure_end was registered, the image grabbing will " +
                    System.Environment.NewLine +
                    "force the execution of the user-specific callback function." +
                    System.Environment.NewLine);

                Console.WriteLine("This example is going to process " + cMaxGrabImages +
                                  " incoming callbacks");

                try
                {
                    acq.GrabImageStart(-1);
                }
                catch (HalconException exc)
                {
                    Console.WriteLine(exc.GetErrorMessage());
                }
                gAcqMutex.UnlockMutex();

                // Wait for user interaction to kill the program.
                waitUserInteraction();
                bWaitUserInteraction = false;
                exitProgram(ref acq, userContext.eventType.S.ToString(),
                            bWaitUserInteraction);
            }
            catch (HOperatorException exc)
            {
                Console.WriteLine(exc.GetErrorMessage());

                if (exc.GetExtendedErrorCode() != 0 ||
                    exc.GetExtendedErrorMessage().Length > 0)
                {
                    Console.WriteLine("Extended error code: " +
                                      exc.GetExtendedErrorCode());
                    Console.WriteLine("Extended error message: " +
                                      exc.GetExtendedErrorMessage());
                }

                if (userContext.eventType == null || userContext.eventType.Length == 0)
                {
                    exitProgram(ref acq, null, bWaitUserInteraction);
                }
                else
                {
                    exitProgram(ref acq, userContext.eventType.S.ToString(),
                                bWaitUserInteraction);
                }
            }
        }