예제 #1
0
        private void sendStreamMjpegFromProvider(VsProviderManager providerManager, NetworkStream ns, int SendBufferSize, int framRate, Size videoSize, int videoQuality)
        {
            //
            //     BlockingQueue<VsImage> BQueue = new BlockingQueue<VsImage>();

            //    // coreProvider.FrameOut += new VsImageEventHandler(coreProvider_FrameOut);
            //     coreProvider.FrameOut += new VsImageEventHandler (
            //        (object sender, VsImageEventArgs e) =>
            //    {

            //        if (BQueue != null)
            //        {
            //            VsImage img = (VsImage)e.Image.Clone();
            //            BQueue.Enqueue(img);
            //        }

            //    });


            //    coreProvider.Start();
            try
            {
                DateTime start;
                TimeSpan span;

                int frameInterval = 1000 / framRate;

                #region setImageEncoder
                // Get a bitmap.
                // Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

                ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

                // Create an Encoder object based on the GUID
                // for the Quality parameter category.
                System.Drawing.Imaging.Encoder myEncoder =
                    System.Drawing.Imaging.Encoder.Quality;

                // Create an EncoderParameters object.
                // An EncoderParameters object has an array of EncoderParameter
                // objects. In this case, there is only one
                // EncoderParameter object in the array.
                EncoderParameters myEncoderParameters = new EncoderParameters();

                // Save the bitmap as a JPG file with zero quality level compression.
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)videoQuality);
                myEncoderParameters.Param[0] = myEncoderParameter;
                #endregion

                MemoryStream stream;
                Bitmap       bmp;
                //double i = 0;
                while (true)
                {
                    stream = new MemoryStream();
                    start  = DateTime.Now;

                    VsImage image = providerManager.getResultImage();

                    bmp = new Bitmap(image.Image, videoSize);

                    //Form1.bmp = new Bitmap(bmp);
                    //bmp.SetResolution(videoSize.Width, videoSize.Height);

                    bmp.Save(stream, jgpEncoder, myEncoderParameters);
                    sendMjpegHead(ns, stream.Length);
                    //bmp.Save(ns, jgpEncoder, myEncoderParameters);
                    //Bitmap.FromStream(stream).Save(ns,ImageFormat.Jpeg);

                    sendStream(ns, SendBufferSize, stream);

                    byte[] bHeadersString = Encoding.ASCII.GetBytes("\r\n");
                    // Send headers
                    ns.Write(bHeadersString, 0, bHeadersString.Length);

                    stream.Close();

                    // ;
                    // i += 1.0 / framRate;
                    //if (i > fe.streamLength)
                    //{
                    //    break;
                    //}


                    span = DateTime.Now.Subtract(start);
                    // miliseconds to sleep
                    int msec = frameInterval - (int)span.TotalMilliseconds;

                    while (msec > 0)
                    {
                        // sleeping ...
                        Thread.Sleep((msec < 100) ? msec : 100);
                        msec -= 100;
                        //span = DateTime.Now.Subtract(start);
                        //msec = frameInterval - (int)span.TotalMilliseconds;
                    }
                }
            }
            finally
            {
                //coreProvider.FrameOut -= new VsImageEventHandler(framedOut);
                //coreProvider.Stop();
            }
        }
예제 #2
0
        private void sendStreamMjpegFromProvider(VsProviderManager providerManager, NetworkStream ns, int SendBufferSize, int framRate, Size videoSize, int videoQuality)
        {
            //
            //     BlockingQueue<VsImage> BQueue = new BlockingQueue<VsImage>();

            //    // coreProvider.FrameOut += new VsImageEventHandler(coreProvider_FrameOut);
            //     coreProvider.FrameOut += new VsImageEventHandler (
            //        (object sender, VsImageEventArgs e) =>
            //    {

            //        if (BQueue != null)
            //        {
            //            VsImage img = (VsImage)e.Image.Clone();
            //            BQueue.Enqueue(img);
            //        }

            //    });


            //    coreProvider.Start();
            try
            {
                DateTime start;
                TimeSpan span;

                int frameInterval = 1000 / framRate;

                #region setImageEncoder
                // Get a bitmap.
                // Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

                ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

                // Create an Encoder object based on the GUID
                // for the Quality parameter category.
                System.Drawing.Imaging.Encoder myEncoder =
                    System.Drawing.Imaging.Encoder.Quality;

                // Create an EncoderParameters object.
                // An EncoderParameters object has an array of EncoderParameter
                // objects. In this case, there is only one
                // EncoderParameter object in the array.
                EncoderParameters myEncoderParameters = new EncoderParameters();

                // Save the bitmap as a JPG file with zero quality level compression.
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)videoQuality);
                myEncoderParameters.Param[0] = myEncoderParameter;
                #endregion

                MemoryStream stream;
                Bitmap bmp;
                //double i = 0;
                while (true)
                {
                    stream = new MemoryStream();
                    start = DateTime.Now;

                    VsImage image = providerManager.getResultImage();

                    bmp = new Bitmap(image.Image, videoSize);

                    //Form1.bmp = new Bitmap(bmp);
                    //bmp.SetResolution(videoSize.Width, videoSize.Height);

                    bmp.Save(stream, jgpEncoder, myEncoderParameters);
                    sendMjpegHead(ns, stream.Length);
                    //bmp.Save(ns, jgpEncoder, myEncoderParameters);
                    //Bitmap.FromStream(stream).Save(ns,ImageFormat.Jpeg);

                    sendStream(ns, SendBufferSize, stream);

                    byte[] bHeadersString = Encoding.ASCII.GetBytes("\r\n");
                    // Send headers	
                    ns.Write(bHeadersString, 0, bHeadersString.Length);

                    stream.Close();

                    // ;
                    // i += 1.0 / framRate;
                    //if (i > fe.streamLength)
                    //{
                    //    break;
                    //}


                    span = DateTime.Now.Subtract(start);
                    // miliseconds to sleep
                    int msec = frameInterval - (int)span.TotalMilliseconds;

                    while (msec > 0)
                    {
                        // sleeping ...
                        Thread.Sleep((msec < 100) ? msec : 100);
                        msec -= 100;
                        //span = DateTime.Now.Subtract(start);
                        //msec = frameInterval - (int)span.TotalMilliseconds;
                    }
                }
            }
            finally
            {

                //coreProvider.FrameOut -= new VsImageEventHandler(framedOut);
                //coreProvider.Stop();
            }
        }