void mTimer_Tick(object sender, EventArgs e)
        {
            if (mISampleGrabber == null)
            {
                return;
            }

            uint lByteSize = (uint)mData.Length;

            try
            {
                mISampleGrabber.readData(mData, out lByteSize);
            }
            catch (Exception exc)
            {
            }
            finally
            {
                updateDisplayImage(this, mData, lByteSize);
            }
        }
Пример #2
0
        private void mTakePhotoButton_Click(object sender, RoutedEventArgs e)
        {
            if (mISampleGrabberCall == null)
            {
                return;
            }

            uint lByteSize = (uint)mData.Length;

            try
            {
                mISampleGrabberCall.readData(mData, out lByteSize);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (lByteSize > 0)
                {
                    float ldiv = (float)lByteSize / (float)mGray8Data.Length;

                    if (ldiv == 2.0)
                    {
                        for (int i = 0; i < mGray8Data.Length; i++)
                        {
                            mGray8Data[i] = mData[(i * 2) + 1];
                        }

                        mDisplayImage.Source = FromArray(mGray8Data, mVideoWidth, mVideoHeight, 1);
                    }
                    else if (ldiv == 1.0)
                    {
                        mDisplayImage.Source = FromArray(mData, mVideoWidth, mVideoHeight, 1);
                    }
                }
            }
        }
        private void mTakePhotoButton_Click(object sender, RoutedEventArgs e)
        {
            if (mISampleGrabberCall == null)
            {
                return;
            }

            byte[] lData = new byte[lsampleByteSize];

            uint lByteSize = (uint)mData.Length;

            try
            {
                mISampleGrabberCall.readData(lData, out lByteSize);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (lByteSize > 0)
                {
                    if (mIsMJPG)
                    {
                        Stream            imageStreamSource = new MemoryStream(lData, 0, (int)lByteSize, false);
                        JpegBitmapDecoder decoder           = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        BitmapSource      bitmapSource      = decoder.Frames[0];

                        mDisplayImage.Source = bitmapSource;
                    }
                    else
                    {
                        mDisplayImage.Source = FromArray(lData, mVideoWidth, mVideoHeight, mChannels);
                    }
                }
            }
        }
Пример #4
0
        private void mTakePhotoButton_Click(object sender, RoutedEventArgs e)
        {
            mPhotoStack.Children.Clear();

            if (mISampleGrabberCallPull == null)
            {
                return;
            }

            byte[] lData = new byte[lsampleByteSize];

            uint lByteSize = (uint)mData.Length;

            int lframeCount = mSampleCount;

            while (true)
            {
                try
                {
                    mISampleGrabberCallPull.readData(lData, out lByteSize);
                }
                catch (Exception)
                {
                    lByteSize = 0;

                    break;
                }
                finally
                {
                    if (lByteSize > 0)
                    {
                        Image lthakePhoto = null;

                        if (mIsMJPG)
                        {
                            Stream            imageStreamSource = new MemoryStream(lData, 0, (int)lByteSize, false);
                            JpegBitmapDecoder decoder           = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                            BitmapSource      bitmapSource      = decoder.Frames[0];

                            lthakePhoto = new Image();

                            lthakePhoto.Source = bitmapSource;
                        }
                        else
                        {
                            lthakePhoto = new Image();

                            lthakePhoto.Source = FromArray(lData, mVideoWidth, mVideoHeight, mChannels);
                        }

                        if (lthakePhoto != null)
                        {
                            mPhotoStack.Children.Add(lthakePhoto);

                            lthakePhoto.Height = 70;

                            lthakePhoto.Margin = new Thickness(5);
                        }
                    }
                }

                if (lByteSize == 0)
                {
                    break;
                }

                if (--lframeCount == 0)
                {
                    break;
                }
            }
        }
Пример #5
0
        private void mLaunchButton_Click(object sender, RoutedEventArgs e)
        {
            if (mLaunchButton.Content.ToString() == "Stop")
            {
                mTimer.Stop();

                if (mISession != null)
                {
                    mISession.closeSession();
                }

                mLaunchButton.Content = "Launch";

                mTakePhotoButton.IsEnabled = false;

                return;
            }

            mTimer = new DispatcherTimer();

            mTimer.Interval = new TimeSpan(100000);

            var lSourceNode = mSourcesComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            var lNode = lSourceNode.SelectSingleNode("Source.Attributes/Attribute[@Name='MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK']/SingleValue/@Value");

            if (lNode == null)
            {
                return;
            }

            string lSymbolicLink = lNode.Value;

            lSourceNode = mStreamsComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("@Index");

            if (lNode == null)
            {
                return;
            }

            uint lStreamIndex = 0;

            if (!uint.TryParse(lNode.Value, out lStreamIndex))
            {
                return;
            }

            lSourceNode = mMediaTypesComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("@Index");

            if (lNode == null)
            {
                return;
            }

            uint lMediaTypeIndex = 0;

            if (!uint.TryParse(lNode.Value, out lMediaTypeIndex))
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[1]/@Value");

            if (lNode == null)
            {
                return;
            }

            uint lVideoWidth = 0;

            if (!uint.TryParse(lNode.Value, out lVideoWidth))
            {
                return;
            }

            mVideoWidth = lVideoWidth;

            lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value");

            if (lNode == null)
            {
                return;
            }

            uint lVideoHeight = 0;

            if (!uint.TryParse(lNode.Value, out lVideoHeight))
            {
                return;
            }

            mVideoHeight = lVideoHeight;

            int lWidthInBytes;

            mCaptureManager.getStrideForBitmapInfoHeader(
                MFVideoFormat_RGB32,
                lVideoWidth,
                out lWidthInBytes);

            lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * lVideoHeight;

            mData = new byte[lsampleByteSize];

            var lSinkControl = mCaptureManager.createSinkControl();


            //  create Spread node

            var lStreamControl = mCaptureManager.createStreamControl();

            ISpreaderNodeFactory lSpreaderNodeFactory = null;

            if (!lStreamControl.createStreamControlNodeFactory(
                    Guid.Parse("{85DFAAA1-4CC0-4A88-AE28-8F492E552CCA}"),
                    ref lSpreaderNodeFactory))
            {
                return;
            }


            // create Sample Accumulator node factory

            ISpreaderNodeFactory lSampleAccumulatorNodeFactory = null;

            XmlNode lselectedSampleAccumulatorXml = (XmlNode)mSampleAccumulatorComboBox.SelectedItem;

            if (lselectedSampleAccumulatorXml == null)
            {
                return;
            }

            var lAttrNode = lselectedSampleAccumulatorXml.SelectSingleNode("@GUID");

            if (lAttrNode == null)
            {
                return;
            }

            if (!lStreamControl.createStreamControlNodeFactory(
                    Guid.Parse(lAttrNode.Value),
                    ref lSampleAccumulatorNodeFactory))
            {
                return;
            }

            lAttrNode = lselectedSampleAccumulatorXml.SelectSingleNode("@Samples");

            if (lAttrNode == null)
            {
                return;
            }

            if (!int.TryParse(lAttrNode.Value, out mSampleCount))
            {
                return;
            }


            string lxmldoc = "";

            XmlDocument doc = new XmlDocument();


            mCaptureManager.getCollectionOfSinks(ref lxmldoc);


            doc.LoadXml(lxmldoc);

            var lSinkNode = doc.SelectSingleNode("SinkFactories/SinkFactory[@GUID='{759D24FF-C5D6-4B65-8DDF-8A2B2BECDE39}']");

            if (lSinkNode == null)
            {
                return;
            }

            // create view output.

            var lContainerNode = lSinkNode.SelectSingleNode("Value.ValueParts/ValuePart[1]");

            if (lContainerNode == null)
            {
                return;
            }

            setContainerFormat(lContainerNode);

            lSinkControl.createSinkFactory(
                mReadMode,
                out mSinkFactory);

            lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_SUBTYPE']/SingleValue/@Value");

            if (lNode == null)
            {
                return;
            }

            Guid lMFVideoFormat = MFVideoFormat_RGB32;

            int lChannels = 4;

            if (lNode.Value == "MFVideoFormat_RGB24")
            {
                lMFVideoFormat = MFVideoFormat_RGB24;

                lChannels = 3;
            }

            mSinkFactory.createOutputNode(
                MFMediaType_Video,
                lMFVideoFormat,
                lsampleByteSize,
                out mISampleGrabberCall);

            if (mISampleGrabberCall != null)
            {
                byte[] lData = new byte[lsampleByteSize];


                mTimer.Tick += delegate
                {
                    if (mISampleGrabberCall == null)
                    {
                        return;
                    }

                    uint lByteSize = 0;

                    try
                    {
                        mISampleGrabberCall.readData(lData, out lByteSize);
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (lByteSize > 0)
                        {
                            mDisplayImage.Source = FromArray(lData, lVideoWidth, lVideoHeight, lChannels);
                        }
                    }
                };


                // create take photo output

                lContainerNode = lSinkNode.SelectSingleNode("Value.ValueParts/ValuePart[3]");

                if (lContainerNode == null)
                {
                    return;
                }

                setContainerFormat(lContainerNode);

                ISampleGrabberCallSinkFactory lSinkFactory = null;

                lSinkControl.createSinkFactory(
                    mReadMode,
                    out lSinkFactory);

                lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_SUBTYPE']/SingleValue/@Value");

                if (lNode == null)
                {
                    return;
                }

                if (lNode.Value == "MFVideoFormat_MJPG")
                {
                    lSinkFactory.createOutputNode(
                        MFMediaType_Video,
                        MFVideoFormat_MJPG,
                        lsampleByteSize,
                        out mISampleGrabberCallPull);

                    mIsMJPG = true;
                }
                else if (lNode.Value == "MFVideoFormat_RGB24")
                {
                    mIsMJPG = false;

                    lSinkFactory.createOutputNode(
                        MFMediaType_Video,
                        MFVideoFormat_RGB24,
                        lsampleByteSize,
                        out mISampleGrabberCallPull);

                    mChannels = 3;
                }
                else
                {
                    mIsMJPG = false;

                    lSinkFactory.createOutputNode(
                        MFMediaType_Video,
                        MFVideoFormat_RGB32,
                        lsampleByteSize,
                        out mISampleGrabberCallPull);

                    mChannels = 4;
                }

                // connect take photo output with sample accumulator


                var lISampleGrabberCallPullNode = mISampleGrabberCallPull.getTopologyNode();

                if (lISampleGrabberCallPullNode == null)
                {
                    return;
                }

                List <object> lSampleAccumulatorList = new List <object>();

                lSampleAccumulatorList.Add(lISampleGrabberCallPullNode);

                object lSampleAccumulatorNode = null;

                lSampleAccumulatorNodeFactory.createSpreaderNode(lSampleAccumulatorList, out lSampleAccumulatorNode);


                // connect view output and take photo output with spreader node



                var lSampleGrabberCallNode = mISampleGrabberCall.getTopologyNode();

                if (lSampleGrabberCallNode == null)
                {
                    return;
                }



                List <object> llSpreaderNodeList = new List <object>();

                llSpreaderNodeList.Add(lSampleAccumulatorNode);

                llSpreaderNodeList.Add(lSampleGrabberCallNode);

                object lSpreaderNode = null;

                lSpreaderNodeFactory.createSpreaderNode(llSpreaderNodeList, out lSpreaderNode);


                object lPtrSourceNode;

                var lSourceControl = mCaptureManager.createSourceControl();

                if (lSourceControl == null)
                {
                    return;
                }

                lSourceControl.createSourceNode(
                    lSymbolicLink,
                    lStreamIndex,
                    lMediaTypeIndex,
                    lSpreaderNode,
                    out lPtrSourceNode);

                List <object> lSourceMediaNodeList = new List <object>();

                lSourceMediaNodeList.Add(lPtrSourceNode);

                var lSessionControl = mCaptureManager.createSessionControl();

                if (lSessionControl == null)
                {
                    return;
                }

                mISession = lSessionControl.createSession(
                    lSourceMediaNodeList.ToArray());

                if (mISession == null)
                {
                    return;
                }

                if (!mISession.startSession(0, Guid.Empty))
                {
                    return;
                }

                mLaunchButton.Content = "Stop";

                mWebCamControl = lSourceControl.createWebCamControl(lSymbolicLink);

                if (mWebCamControl != null)
                {
                    string lXMLstring;

                    mWebCamControl.getCamParametrs(out lXMLstring);

                    XmlDataProvider lXmlDataProvider = (XmlDataProvider)this.Resources["XmlWebCamParametrsProvider"];

                    if (lXmlDataProvider == null)
                    {
                        return;
                    }

                    System.Xml.XmlDocument ldoc = new System.Xml.XmlDocument();

                    ldoc.LoadXml(lXMLstring);

                    lXmlDataProvider.Document = ldoc;
                }

                mTimer.Start();

                mTakePhotoButton.IsEnabled = true;
            }
        }
Пример #6
0
        private void mLaunchButton_Click(object sender, RoutedEventArgs e)
        {
            if (mLaunchButton.Content.ToString() == "Stop")
            {
                mTimer.Stop();

                if (mISession != null)
                {
                    mISession.closeSession();
                }

                mLaunchButton.Content = "Launch";

                if (mISampleGrabberCall != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(mISampleGrabberCall.getTopologyNode());
                }

                return;
            }

            var lSourceNode = mSourcesComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            var lNode = lSourceNode.SelectSingleNode("Source.Attributes/Attribute[@Name='MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK']/SingleValue/@Value");

            if (lNode == null)
            {
                return;
            }

            string lSymbolicLink = lNode.Value;

            lSourceNode = mStreamsComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("@Index");

            if (lNode == null)
            {
                return;
            }

            uint lStreamIndex = 0;

            if (!uint.TryParse(lNode.Value, out lStreamIndex))
            {
                return;
            }

            lSourceNode = mMediaTypesComboBox.SelectedItem as XmlNode;

            if (lSourceNode == null)
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("@Index");

            if (lNode == null)
            {
                return;
            }

            uint lMediaTypeIndex = 0;

            if (!uint.TryParse(lNode.Value, out lMediaTypeIndex))
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[1]/@Value");

            if (lNode == null)
            {
                return;
            }

            uint lVideoWidth = 0;

            if (!uint.TryParse(lNode.Value, out lVideoWidth))
            {
                return;
            }

            lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value");

            if (lNode == null)
            {
                return;
            }

            uint lVideoHeight = 0;

            if (!uint.TryParse(lNode.Value, out lVideoHeight))
            {
                return;
            }

            int lWidthInBytes;

            mCaptureManager.getStrideForBitmapInfoHeader(
                MFVideoFormat_RGB32,
                lVideoWidth,
                out lWidthInBytes);

            lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * lVideoHeight;

            mData = new byte[lsampleByteSize];

            var lSinkControl = mCaptureManager.createSinkControl();

            string lxmldoc = "";

            mCaptureManager.getCollectionOfSinks(ref lxmldoc);

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(lxmldoc);

            var lSinkNode = doc.SelectSingleNode("SinkFactories/SinkFactory[@GUID='{759D24FF-C5D6-4B65-8DDF-8A2B2BECDE39}']");

            if (lSinkNode == null)
            {
                return;
            }

            var lContainerNode = lSinkNode.SelectSingleNode("Value.ValueParts/ValuePart[1]");

            if (lContainerNode == null)
            {
                return;
            }

            setContainerFormat(lContainerNode);

            lSinkControl.createSinkFactory(
                mReadMode,
                out mSinkFactory);

            mSinkFactory.createOutputNode(
                MFMediaType_Video,
                MFVideoFormat_RGB32,
                lsampleByteSize,
                out mISampleGrabberCall);

            if (mISampleGrabberCall != null)
            {
                byte[] lData = new byte[lsampleByteSize];

                mTimer.Tick += delegate
                {
                    if (mISampleGrabberCall == null)
                    {
                        return;
                    }

                    uint lByteSize = 0;

                    try
                    {
                        mISampleGrabberCall.readData(lData, out lByteSize);
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (lByteSize > 0)
                        {
                            mDisplayImage.Source = FromArray(lData, lVideoWidth, lVideoHeight, mChannels);
                        }
                    }
                };

                var lSampleGrabberCallNode = mISampleGrabberCall.getTopologyNode();

                if (lSampleGrabberCallNode == null)
                {
                    return;
                }

                object lPtrSourceNode;

                var lSourceControl = mCaptureManager.createSourceControl();

                if (lSourceControl == null)
                {
                    return;
                }

                lSourceControl.createSourceNode(
                    lSymbolicLink,
                    lStreamIndex,
                    lMediaTypeIndex,
                    lSampleGrabberCallNode,
                    out lPtrSourceNode);

                List <object> lSourceMediaNodeList = new List <object>();

                lSourceMediaNodeList.Add(lPtrSourceNode);

                var lSessionControl = mCaptureManager.createSessionControl();

                if (lSessionControl == null)
                {
                    return;
                }

                mISession = lSessionControl.createSession(
                    lSourceMediaNodeList.ToArray());

                if (mISession == null)
                {
                    return;
                }

                mISession.startSession(0, Guid.Empty);

                mLaunchButton.Content = "Stop";

                mWebCamControl = lSourceControl.createWebCamControl(lSymbolicLink);

                if (mWebCamControl != null)
                {
                    string lXMLstring;

                    mWebCamControl.getCamParametrs(out lXMLstring);

                    XmlDataProvider lXmlDataProvider = (XmlDataProvider)this.Resources["XmlWebCamParametrsProvider"];

                    if (lXmlDataProvider == null)
                    {
                        return;
                    }

                    System.Xml.XmlDocument ldoc = new System.Xml.XmlDocument();

                    ldoc.LoadXml(lXMLstring);

                    lXmlDataProvider.Document = ldoc;
                }

                mTimer.Start();
            }
        }