public async Task <object> init(XmlNode aMediaTypeXmlNode) { await initInterface(); object lresult = null; do { if (aMediaTypeXmlNode == null) { break; } var lNode = aMediaTypeXmlNode.SelectSingleNode("@Index"); if (lNode == null) { break; } uint lMediaTypeIndex = 0; if (!uint.TryParse(lNode.Value, out lMediaTypeIndex)) { break; } lNode = aMediaTypeXmlNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[1]/@Value"); if (lNode == null) { break; } if (!uint.TryParse(lNode.Value, out mVideoWidth)) { break; } lNode = aMediaTypeXmlNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value"); if (lNode == null) { break; } if (!uint.TryParse(lNode.Value, out mVideoHeight)) { break; } Guid MFMediaType_Video = new Guid( 0x73646976, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71); Guid MFVideoFormat_RGB24 = new Guid( 20, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); Guid MFVideoFormat_RGB32 = new Guid( 22, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); int lWidthInBytes = await MainWindow.mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB32, mVideoWidth); uint lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * mVideoHeight; mChannels = 4; var lSampleGrabberCall = await mSampleGrabberSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB32, lsampleByteSize); if (lSampleGrabberCall == null) { break; } mData = new byte[lsampleByteSize]; lresult = lSampleGrabberCall.getTopologyNode(); mTimer.Tick += async delegate(object sender, EventArgs e) { uint lByteSize = (uint)mData.Length; try { lByteSize = await lSampleGrabberCall.readDataAsync(mData); if (mEnableTrigger && mChangeState != null) { float lvalue = 0; for (int i = 0; i < lByteSize; i++) { lvalue += mData[i]; } lvalue = ((lvalue / (float)lByteSize) * 100) / 255.0f; if (lvalue >= mThreshold) { mChangeState(true); } else { mChangeState(false); } } } finally { updateDisplayImage(); } }; } while (false); return(lresult); }
//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); // } //} private async void mLaunchButton_Click(object sender, RoutedEventArgs e) { if (mLaunchButton.Content.ToString() == "Stop") { //mTimer.Stop(); if (mISession != null) { await mISession.closeSessionAsync(); } mLaunchButton.Content = "Launch"; mTakePhotoButton.IsEnabled = false; 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; } 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 = await mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB32, lVideoWidth); m_sampleByteSize = (uint)Math.Abs(lWidthInBytes) * lVideoHeight; var lSinkControl = await mCaptureManager.createSinkControlAsync(); string lxmldoc = await mCaptureManager.getCollectionOfSinksAsync(); 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[3]"); if (lContainerNode == null) { return; } setContainerFormat(lContainerNode); ISampleGrabberCallSinkFactoryAsync mSinkFactory = await lSinkControl.createSampleGrabberCallSinkFactoryAsync( mReadMode); lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_SUBTYPE']/SingleValue/@Value"); if (lNode == null) { return; } if (lNode.Value == "MFVideoFormat_MJPG") { mISampleGrabberCall = await mSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_MJPG, m_sampleByteSize); mIsMJPG = true; } else { mISampleGrabberCall = await mSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB32, m_sampleByteSize); } if (mISampleGrabberCall != null) { byte[] lData = new byte[m_sampleByteSize]; var lSampleGrabberCallNode = mISampleGrabberCall.getTopologyNode(); if (lSampleGrabberCallNode == null) { return; } var lSourceControl = await mCaptureManager.createSourceControlAsync(); if (lSourceControl == null) { return; } object lPtrSourceNode = await lSourceControl.createSourceNodeWithDownStreamConnectionAsync( lSymbolicLink, lStreamIndex, lMediaTypeIndex, lSampleGrabberCallNode); List <object> lSourceMediaNodeList = new List <object>(); lSourceMediaNodeList.Add(lPtrSourceNode); var lSessionControl = await mCaptureManager.createSessionControlAsync(); if (lSessionControl == null) { return; } mISession = await lSessionControl.createSessionAsync( lSourceMediaNodeList.ToArray()); if (mISession == null) { return; } if (!await mISession.startSessionAsync(0, Guid.Empty)) { return; } mLaunchButton.Content = "Stop"; mTakePhotoButton.IsEnabled = true; } }
private async void mLaunchButton_Click(object sender, RoutedEventArgs e) { if (mLaunchButton.Content.ToString() == "Stop") { mTimer.Stop(); if (mISession != null) { await mISession.closeSessionAsync(); } 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 = await mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB32, lVideoWidth); lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * lVideoHeight; mData = new byte[lsampleByteSize]; var lSinkControl = await mCaptureManager.createSinkControlAsync(); // create Spread node var lStreamControl = await mCaptureManager.createStreamControlAsync(); var lSpreaderNodeFactory = await lStreamControl.createSpreaderNodeFactoryAsync( Guid.Parse("{85DFAAA1-4CC0-4A88-AE28-8F492E552CCA}")); // create Sample Accumulator node factory XmlNode lselectedSampleAccumulatorXml = (XmlNode)mSampleAccumulatorComboBox.SelectedItem; if (lselectedSampleAccumulatorXml == null) { return; } var lAttrNode = lselectedSampleAccumulatorXml.SelectSingleNode("@GUID"); if (lAttrNode == null) { return; } var lSampleAccumulatorNodeFactory = await lStreamControl.createSpreaderNodeFactoryAsync( Guid.Parse(lAttrNode.Value)); 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); mSinkFactory = await lSinkControl.createSampleGrabberCallSinkFactoryAsync( mReadMode); 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; } mISampleGrabberCall = await mSinkFactory.createOutputNodeAsync( MFMediaType_Video, lMFVideoFormat, lsampleByteSize); if (mISampleGrabberCall != null) { byte[] lData = new byte[lsampleByteSize]; mTimer.Tick += async delegate { if (mISampleGrabberCall == null) { return; } uint lByteSize = 0; try { lByteSize = await mISampleGrabberCall.readDataAsync(lData); } 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); var lSinkFactory = await lSinkControl.createSampleGrabberCallSinkFactoryAsync( mReadMode); lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_SUBTYPE']/SingleValue/@Value"); if (lNode == null) { return; } if (lNode.Value == "MFVideoFormat_MJPG") { mISampleGrabberCallPull = await lSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_MJPG, lsampleByteSize); mIsMJPG = true; } else if (lNode.Value == "MFVideoFormat_RGB24") { mIsMJPG = false; mISampleGrabberCallPull = await lSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB24, lsampleByteSize); mChannels = 3; } else { mIsMJPG = false; mISampleGrabberCallPull = await lSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB32, lsampleByteSize); 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 = await lSampleAccumulatorNodeFactory.createSpreaderNodeAsync(lSampleAccumulatorList); // 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 = await lSpreaderNodeFactory.createSpreaderNodeAsync(llSpreaderNodeList); var lSourceControl = await mCaptureManager.createSourceControlAsync(); if (lSourceControl == null) { return; } object lPtrSourceNode = await lSourceControl.createSourceNodeWithDownStreamConnectionAsync( lSymbolicLink, lStreamIndex, lMediaTypeIndex, lSpreaderNode); List <object> lSourceMediaNodeList = new List <object>(); lSourceMediaNodeList.Add(lPtrSourceNode); var lSessionControl = await mCaptureManager.createSessionControlAsync(); if (lSessionControl == null) { return; } mISession = await lSessionControl.createSessionAsync( lSourceMediaNodeList.ToArray()); if (mISession == null) { return; } if (!await mISession.startSessionAsync(0, Guid.Empty)) { return; } mLaunchButton.Content = "Stop"; mTimer.Start(); mTakePhotoButton.IsEnabled = true; } }
private async void mLaunchButton_Click(object sender, RoutedEventArgs e) { if (mLaunchButton.Content.ToString() == "Stop") { mTimer.Stop(); if (mISession != null) { await mISession.closeSessionAsync(); } 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; } if (!uint.TryParse(lNode.Value, out mVideoWidth)) { return; } lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value"); if (lNode == null) { return; } if (!uint.TryParse(lNode.Value, out mVideoHeight)) { return; } int lWidthInBytes = await mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB32, mVideoWidth); lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * mVideoHeight; mData = new byte[lsampleByteSize]; var lSinkControl = await mCaptureManager.createSinkControlAsync(); string lxmldoc = await mCaptureManager.getCollectionOfSinksAsync(); 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); mSinkFactory = await lSinkControl.createSampleGrabberCallSinkFactoryAsync(mReadMode); mISampleGrabberCall = await mSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB32, lsampleByteSize); if (mISampleGrabberCall != null) { var lSampleGrabberCallNode = mISampleGrabberCall.getTopologyNode(); if (lSampleGrabberCallNode == null) { return; } object lPtrSourceNode; var lSourceControl = await mCaptureManager.createSourceControlAsync(); if (lSourceControl == null) { return; } lPtrSourceNode = await lSourceControl.createSourceNodeWithDownStreamConnectionAsync( lSymbolicLink, lStreamIndex, lMediaTypeIndex, lSampleGrabberCallNode); List <object> lSourceMediaNodeList = new List <object>(); lSourceMediaNodeList.Add(lPtrSourceNode); var lSessionControl = await mCaptureManager.createSessionControlAsync(); if (lSessionControl == null) { return; } mISession = await lSessionControl.createSessionAsync( lSourceMediaNodeList.ToArray()); if (mISession == null) { return; } await mISession.startSessionAsync(0, Guid.Empty); mLaunchButton.Content = "Stop"; mWebCamControl = await lSourceControl.createWebCamControlAsync(lSymbolicLink); if (mWebCamControl != null) { string lXMLstring = await mWebCamControl.getCamParametrsAsync(); XmlDataProvider lXmlDataProvider = (XmlDataProvider)this.Resources["XmlWebCamParametrsProvider"]; if (lXmlDataProvider == null) { return; } System.Xml.XmlDocument ldoc = new System.Xml.XmlDocument(); if (!string.IsNullOrWhiteSpace(lXMLstring)) { ldoc.LoadXml(lXMLstring); } lXmlDataProvider.Document = ldoc; } mTimer.Start(); } }