private void InitOpenNI() { // ContextとImageGeneratorの作成 ScriptNode node; context = Context.CreateFromXmlFile( "SamplesConfig.xml", out node ); context.GlobalMirror = false; image = context.FindExistingNode( NodeType.Image ) as ImageGenerator; // 画像更新のためのスレッドを作成 shouldRun = true; readerThread = new Thread( new ThreadStart( () => { while ( shouldRun ) { context.WaitAndUpdateAll(); ImageMetaData imageMD = image.GetMetaData(); // ImageMetaDataをBitmapSourceに変換する(unsafeにしなくてもOK!!) this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () => { imageOpenNI.Source = BitmapSource.Create( imageMD.XRes, imageMD.YRes, 96, 96, PixelFormats.Rgb24, null, imageMD.ImageMapPtr, imageMD.DataSize, imageMD.XRes * imageMD.BytesPerPixel ); } ) ); } } ) ); readerThread.Start(); }
public MainWindow() { InitializeComponent(); try { // ContextとImageGeneratorの作成 ScriptNode node; context = Context.CreateFromXmlFile( "../../SamplesConfig.xml", out node ); context.GlobalMirror = false; image = context.FindExistingNode( NodeType.Image ) as ImageGenerator; audio = context.FindExistingNode( NodeType.Audio ) as AudioGenerator; wavePlayer = new StreamingWavePlayer( audio.WaveOutputMode.SampleRate, audio.WaveOutputMode.BitsPerSample, audio.WaveOutputMode.Channels, 100 ); // 画像更新のためのスレッドを作成 shouldRun = true; readerThread = new Thread( new ThreadStart( () => { while ( shouldRun ) { context.WaitAndUpdateAll(); ImageMetaData imageMD = image.GetMetaData(); // WAVEデータの出力 wavePlayer.Output( audio.AudioBufferPtr, audio.DataSize ); // ImageMetaDataをBitmapSourceに変換する(unsafeにしなくてもOK!!) this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () => { image1.Source = BitmapSource.Create( imageMD.XRes, imageMD.YRes, 96, 96, PixelFormats.Rgb24, null, imageMD.ImageMapPtr, imageMD.DataSize, imageMD.XRes * imageMD.BytesPerPixel ); } ) ); } } ) ); readerThread.Start(); } catch ( Exception ex ) { MessageBox.Show( ex.Message ); } }
public MainWindow() { InitializeComponent(); try { // ContextとImageGeneratorの作成 ScriptNode node; context = Context.CreateFromXmlFile( "SamplesConfig.xml", out node ); context.GlobalMirror = false; depth = context.FindExistingNode( NodeType.Depth ) as DepthGenerator; // 画像更新のためのスレッドを作成 shouldRun = true; readerThread = new Thread( new ThreadStart( () => { while ( shouldRun ) { context.WaitAndUpdateAll(); DepthMetaData depthMD = depth.GetMetaData(); // ImageMetaDataをBitmapSourceに変換する(unsafeにしなくてもOK!!) this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () => { Int16[] depthArray = new Int16[depthMD.XRes * depthMD.YRes]; Marshal.Copy( depthMD.DepthMapPtr, depthArray, 0, depthArray.Length ); for ( int i = 0; i < depthArray.Length; i++ ) { depthArray[i] = (Int16)(0xffff - (0xffff * depthArray[i] / depth.DeviceMaxDepth)); } image1.Source = BitmapSource.Create( depthMD.XRes, depthMD.YRes, 96, 96, PixelFormats.Gray16, null, depthArray, depthMD.XRes * depthMD.BytesPerPixel ); } ) ); } } ) ); readerThread.Start(); } catch ( Exception ex ) { MessageBox.Show( ex.Message ); } }
public Sensor(string config) { if (string.IsNullOrEmpty(config)) throw new ArgumentNullException(); try { _context = Context.CreateFromXmlFile(config, out _scriptNode); _depthGenerator = _context.FindExistingNode(NodeType.Depth) as DepthGenerator; _imageGenerator = _context.FindExistingNode(NodeType.Image) as ImageGenerator; _userGenerator = _context.FindExistingNode(NodeType.User) as UserGenerator; if (_depthGenerator == null) throw new ApplicationException("No depth node found."); if (_imageGenerator == null) throw new ApplicationException("No image node found."); if (_userGenerator == null) throw new ApplicationException("No user node found."); if (_depthGenerator.MapOutputMode.FPS != _imageGenerator.MapOutputMode.FPS) throw new ApplicationException("Depth and image node must have common framerates."); if (_depthGenerator.MapOutputMode.XRes != _imageGenerator.MapOutputMode.XRes) throw new ApplicationException("Depth and image node must have common horizontal resolutions."); if (_depthGenerator.MapOutputMode.YRes != _imageGenerator.MapOutputMode.YRes) throw new ApplicationException("Depth and image node must have common vertical resolutions."); _depthMetaData = new DepthMetaData(); _imageMetaData = new ImageMetaData(); _imageWidth = _depthGenerator.MapOutputMode.XRes; _imageHeight = _depthGenerator.MapOutputMode.YRes; _userGenerator.NewUser += new EventHandler<NewUserEventArgs>(_userGenerator_NewUser); _userGenerator.LostUser += new EventHandler<UserLostEventArgs>(_userGenerator_LostUser); _userGenerator.StartGenerating(); _bitmapGenerator = new BitmapGenerator(this); _readerWaitHandle = new AutoResetEvent(false); _readerThread = new Thread(delegate() { try { while (_run) { if (_pause) _readerWaitHandle.WaitOne(); _context.WaitAndUpdateAll(); _depthGenerator.GetMetaData(_depthMetaData); _imageGenerator.GetMetaData(_imageMetaData); if (_depthMetaData.XRes != _imageWidth || _imageMetaData.XRes != _imageWidth) throw new ApplicationException("Image width must not change."); if (_depthMetaData.YRes != _imageHeight || _imageMetaData.YRes != _imageHeight) throw new ApplicationException("Image height must not change."); if (GeneratorUpdate != null) GeneratorUpdate(this, EventArgs.Empty); } } catch (ThreadInterruptedException) { Console.WriteLine("Reader thread interrupted."); } catch (Exception e) { throw new ApplicationException("Error while processing sensor data.", e); } }) { Name = "ONI Reader Thread" }; } catch (Exception) { throw; } }