示例#1
0
        void Work1(object state)
        {
            if (Application.Current == null)
            {
                return;
            }
            int          destBufferSize = 0;
            TransferBits transfer       = state as TransferBits;;

            Api.FormatImage(transfer.bits, ref transfer.frameDesc, ImageFormat.Bmp, null, ref destBufferSize);
            //                if (FormattedBuf == null)
            transfer.FormattedBuf = new byte[destBufferSize];
            Api.FormatImage(transfer.bits, ref transfer.frameDesc, ImageFormat.Bmp, transfer.FormattedBuf, ref destBufferSize);
            //MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            //byte[] hash = md5.ComputeHash(FormattedBuf);
            //((App)Application.Current).logger.MyLogFile("hash ", string.Format(" thread {0} Bytes  {1}", Thread.CurrentThread.ManagedThreadId, ByteArrayToString(hash)));
            if (this.bActive == true)
            {
                preview.Work(transfer);
                hist.Work(transfer);
                return;
            }
            //this code will paint the camera in the tray
            Application.Current.Dispatcher.Invoke(
                DispatcherPriority.Render,
                new Action(() => showBuffer(transfer.FormattedBuf)));


            ((App)Application.Current).logger.MyLogFile("WorkThread ", String.Format(" Memory: {0:N0} bytes cam {1}  frame {2}", GC.GetTotalMemory(false), transfer.hCamera, transfer.frameDesc.FrameNumber));
        }
示例#2
0
        public int MyCallbackFunction(int hCamera, System.IntPtr pBuf, PixeLINK.PixelFormat dataFormat, ref FrameDescriptor frameDesc, int userData)
        {
            lock (thisLock)
            {
                if (Application.Current == null)
                {
                    return(0);
                }
                ((App)Application.Current).logger.MyLogFile("MyCallbackFunction ", string.Format("camera {0} threadid {1} ", hCamera, Thread.CurrentThread.ManagedThreadId));
                // Calculate actual framerate.
                long curtime     = (long)(frameDesc.FrameTime * 1000);
                long elapsedtime = curtime - m_startframetime;

                int elapsedframes = frameDesc.FrameNumber - m_startframe;
                Console.WriteLine("calc frame rate {0}  {1}", elapsedtime, elapsedframes);
                if (elapsedtime >= 50 && elapsedframes >= 5)
                {
                    // enough time and enough frames have elapsed to calculate a reasonably
                    // accurate frame rate.
                    m_rate           = (double)(1000 * elapsedframes / elapsedtime);
                    m_startframetime = curtime;
                    m_startframe     = frameDesc.FrameNumber;
                }
                else if (elapsedframes < 0 || elapsedtime < 0)
                {
                    // Stream has been restarted. Reset our start values.
                    m_startframetime = curtime;
                    m_startframe     = frameDesc.FrameNumber;
                }

                if ((elapsedtime < 50) || (elapsedframes < 0))
                {
                    // The rest of this function calculates the histogram data, and then
                    // sends a message to update the GUI. Do not do this more than 20 times
                    // per second - that would be a waste of processor power, since users
                    // can't tell the difference between a GUI that is updating 20 times
                    // per second and one that is updating 1000 times per second.
                    //
                    // The frame should also be ignored if the frame is older than
                    // the most recent frame we've seen. (i.e. elapsedframes < 0)

                    Console.WriteLine("skip frame");
                    return(0);
                }
                long numPixels = frameDesc.NumberOfPixels();
                if (rawbits == null)
                {
                    rawbits = new byte[numPixels];
                }
                TransferBits transfer = new TransferBits();

                transfer.bits = rawbits;
                //            byte[] bits = new byte[40];

                //copy the image bits from API to managed buffer
                try
                {
                    System.Runtime.InteropServices.Marshal.Copy(pBuf, transfer.bits, 0, (int)numPixels);
                }
                catch
                {
                    ((App)Application.Current).logger.MyLogFile("MyCallbackFunction ", "Exception in Marshal.Copy ");
                    return(1);
                }
                //Buffer.BlockCopy(transfer.bits, 0, bits, 0, 40);
                //((App)Application.Current).logger.MyLogFile("pBuf ", string.Format(" thread {0} Bytes  {1}", Thread.CurrentThread.ManagedThreadId, ByteArrayToString(bits)));
                transfer.dataFormat = dataFormat;
                transfer.frameDesc  = frameDesc;
                transfer.hCamera    = hCamera;

                //really should not need the worker thread but put this in to keep the camera red lite off
                ThreadPool.QueueUserWorkItem(Work1, transfer);


                return(1);
            }
        }