Exemple #1
0
 public override void Dispose()
 {
     Streaming = false;
     if (Reader != null)
     {
         Reader.FrameArrived -= NewFrameArrived;
         Reader.Dispose();
     }
     if (NativeDevice != null)
     {
         NativeDevice.Dispose();
     }
 }
        // Create and initialze the MediaCapture object.

        public async Task CaptureImageAsync()
        {
            autoStopCamera          = new System.Timers.Timer(500);
            autoStopCamera.Elapsed += AutoStopCamera;
            frameQueue              = new List <SoftwareBitmap>( );
            var cameraName       = "Surface Camera Front";
            var frameSourceGroup = await MediaFrameSourceGroup.FindAllAsync( );

            Debug.WriteLine($"frameSourceGroup = {frameSourceGroup}");
            var cameraGroup = frameSourceGroup.FirstOrDefault(fg => fg.DisplayName == cameraName);

            Debug.WriteLine($"cameraGroup = {cameraGroup}");
            var mediaCapture = new MediaCapture( );
            var settings     = new MediaCaptureInitializationSettings( )
            {
                SourceGroup          = cameraGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video,
            };

            await mediaCapture.InitializeAsync(settings);

            var exposureSuccess       = mediaCapture.VideoDeviceController.Exposure.TrySetAuto(true);
            var brightnessSuccess     = mediaCapture.VideoDeviceController.Brightness.TrySetAuto(true);
            var currExposureSuccess   = mediaCapture.VideoDeviceController.Exposure.TryGetValue(out double expValue);
            var currBrightnessSuccess = mediaCapture.VideoDeviceController.Brightness.TryGetValue(out double brightValue);

            Debug.WriteLine($"exposureSuccess: {exposureSuccess}");
            Debug.WriteLine($"brightnessSuccess: {brightnessSuccess}");
            Debug.WriteLine($"expValue: {expValue}");
            Debug.WriteLine($"brightValue: {brightValue}");

            var sourceInfoId = cameraGroup?.SourceInfos?.FirstOrDefault( )?.Id;

            var mediaFrameSource = (mediaCapture?.FrameSources?.ContainsKey(sourceInfoId) ?? false) ? mediaCapture.FrameSources[sourceInfoId] : null;
            var preferredFormat  = mediaFrameSource.SupportedFormats.Where(format =>
            {
                return(format.VideoFormat.Width >= 1080);
                //&& format.Subtype == "NV12";
            }).FirstOrDefault( );

            if (preferredFormat == null)
            {
                // Our desired format is not supported
                return;
            }

            await mediaFrameSource.SetFormatAsync(preferredFormat);

            frameReader?.Dispose( );
            frameReader = await mediaCapture.CreateFrameReaderAsync(mediaFrameSource);

            frameReader.FrameArrived += FrameReaderOnFrameArrived;

            await frameReader.StartAsync( );

            autoStopCamera.Start( );
        }
Exemple #3
0
 /// <summary>
 /// Dispose method implementation
 /// </summary>
 public void Dispose()
 {
     lock (m_lock)
     {
         m_frameReader.FrameArrived -= FrameReader_FrameArrived;
         m_frameReader.Dispose();
         m_mediaCapture.Dispose();
     }
 }
Exemple #4
0
        public override async Task DisposeAsync()
        {
            FrameReader.FrameArrived -= FrameArrived;
            await FrameReader.StopAsync();

            FrameReader.Dispose();
            MediaCapture.Dispose();
            Logger.Log($"Disposed {nameof(ColorCameraProcessor)}.");
        }
Exemple #5
0
        private async Task CleanupMediaCaptureAsync()
        {
            if (_mediaCapture != null)
            {
                await _reader.StopAsync();

                _reader.FrameArrived -= HandleFrameArrive;
                _reader.Dispose();
                _mediaCapture = null;
            }
        }
        /// <summary>
        /// Unregisters FrameArrived event handlers, stops and disposes frame readers
        /// and disposes the MediaCapture object.
        /// </summary>
        private async Task CleanupMediaCaptureAsync()
        {
            if (mediaCapture != null)
            {
                await reader.StopAsync();

                reader.FrameArrived -= ColorFrameReader_FrameArrivedAsync;
                reader.Dispose();
                mediaCapture = null;
            }
        }
Exemple #7
0
        public async Task Cleanup()
        {
            if (_mediaCapture != null)
            {
                await _reader.StopAsync();

                _reader.FrameArrived -= ColorFrameReader_FrameArrivedAsync;
                _reader.Dispose();
                _mediaCapture = null;
            }
        }
        /// <summary>
        /// Disposes the camera
        /// </summary>
        public void Dispose()
        {
#if CAN_USE_UWP_TYPES
            if (frameReader != null)
            {
                frameReader.FrameArrived -= OnMediaFrameArrived;
                frameReader.Dispose();
            }
            mediaCapture?.Dispose();
            mediaCapture          = null;
            videoDeviceController = null;
#endif
        }
    public async Task StopCapturing()
    {
#if ENABLE_WINMD_SUPPORT
        if (_captureManager != null && _captureManager.CameraStreamState != CameraStreamState.Shutdown)
        {
            await _frameReader.StopAsync();

            _frameReader.Dispose();
            _captureManager.Dispose();
            _captureManager = null;
        }
#endif
    }
Exemple #10
0
 /// <summary>
 /// Dispose method implementation
 /// </summary>
 public void Dispose()
 {
     lock (m_lock)
     {
         if (m_frameReader != null)
         {
             m_frameReader.FrameArrived -= FrameReader_FrameArrived;
             m_frameReader.Dispose();
             m_frameReader = null;
         }
         m_mediaCapture?.Dispose();
         m_mediaCapture = null;
     }
 }
Exemple #11
0
    /// <summary>
    /// Asynchronously stop media capture and dispose of resources
    /// </summary>
    /// <returns></returns>
    public async Task StopMediaFrameReaderAsync()
    {
#if ENABLE_WINMD_SUPPORT
        if (_mediaCapture != null && _mediaCapture.CameraStreamState != CameraStreamState.Shutdown)
        {
            await _mediaFrameReader.StopAsync();

            _mediaFrameReader.Dispose();
            _mediaCapture.Dispose();
            _mediaCapture = null;
        }
        IsCapturing = false;
#endif
    }
        /// <summary>
        /// カメラプレビューを停止する
        /// </summary>
        /// <param name="onVideoModeStoppedCallback"></param>
        public async Task <bool> StopVideoModeAsync()
        {
            if (IsStreaming == false)
            {
                // すでにプレビューを停止している場合は何もしない
                return(false);
            }

            _frameReader.FrameArrived -= HandleFrameArrived;
            await _frameReader.StopAsync();

            _frameReader.Dispose();
            _frameReader = null;
            return(true);
        }
        /// <summary>
        /// Stops reading from the frame reader, disposes of the reader and updates the button state.
        /// </summary>
        private async Task StopReaderAsync()
        {
            _streaming = false;

            if (_reader != null)
            {
                await _reader.StopAsync();

                _reader.FrameArrived -= Reader_FrameArrived;
                _reader.Dispose();
                _reader = null;

                _logger.Log("Reader stopped.");
            }
        }
Exemple #14
0
        /// <summary>
        /// Asynchronously stops video mode.
        /// </summary>
        /// <param name="onVideoModeStoppedCallback">This callback will be invoked once video mode has been deactivated.</param>
        public async void StopVideoModeAsync(OnVideoModeStoppedCallback onVideoModeStoppedCallback)
        {
            if (IsStreaming == false)
            {
                onVideoModeStoppedCallback?.Invoke(new VideoCaptureResult(1, ResultType.InappropriateState, false));
                return;
            }

            _frameReader.FrameArrived -= HandleFrameArrived;
            await _frameReader.StopAsync();

            _frameReader.Dispose();
            _frameReader = null;

            onVideoModeStoppedCallback?.Invoke(new VideoCaptureResult(0, ResultType.Success, true));
        }
Exemple #15
0
        /// <summary>
        /// Dispose of camera pipeline resources.
        /// </summary>
        /// <returns></returns>
        public async Task CleanupAsync()
        {
            if (m_frameReader != null)
            {
                m_frameReader.FrameArrived -= FrameArrivedHandler;
                await m_frameReader.StopAsync();

                m_frameReader.Dispose();
                m_frameReader = null;
            }
            if (m_mediaCapture != null)
            {
                m_mediaCapture.Failed -= MediaCapture_Failed;
                m_mediaCapture.Dispose();
            }
            m_mediaCapture = null;
        }
Exemple #16
0
        private async Task StopReaderAsync2()
        {
            _streaming2 = false;

            if (_reader2 != null)
            {
                await _reader2.StopAsync();

                _reader2.FrameArrived -= Reader_FrameArrived2;
                _reader2.Dispose();
                _reader2 = null;

                _logger2.Log("Reader stopped.");
            }

            await UpdateButtonStateAsync2();
        }
        private async void SettingSensorData(int deviceNum, int cameraNum)
        {
            if (mediaFrameReader != null)
            {
                await mediaFrameReader.StopAsync();

                mediaFrameReader.FrameArrived -= FrameArrived;
                mediaFrameReader.Dispose();
                mediaFrameReader = null;
            }

            var mediaFrameSourceGroupList = await MediaFrameSourceGroup.FindAllAsync();

            var mediaFrameSourceGroup = mediaFrameSourceGroupList[deviceNum];
            var mediaFrameSourceInfo  = mediaFrameSourceGroup.SourceInfos[cameraNum];
            MediaFrameSourceKind kind = mediaFrameSourceInfo.SourceKind;
            var mediaCapture          = new MediaCapture();
            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = mediaFrameSourceGroup,
                SharingMode          = MediaCaptureSharingMode.SharedReadOnly,
                StreamingCaptureMode = StreamingCaptureMode.Video,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);

                var mediaFrameSource = mediaCapture.FrameSources[mediaFrameSourceInfo.Id];
                if (kind == MediaFrameSourceKind.Color)
                {
                    mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(mediaFrameSource, MediaEncodingSubtypes.Argb32);
                }
                else
                {
                    mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(mediaFrameSource, mediaFrameSource.CurrentFormat.Subtype);
                }
                mediaFrameReader.FrameArrived += FrameArrived;
                await mediaFrameReader.StartAsync();
            }
            catch (Exception)
            {
                throw;
            }
        }
        private async Task stopCameraAsync()
        {
            if (mediaCapture != null)
            {
                await mediaCapture.StopPreviewAsync();
                mediaCapture.Dispose();
                mediaCapture = null;
            }

            if (frameReader != null)
            {
                frameReader.FrameArrived -= FrameReader_FrameArrived;
                frameReader.Dispose();
                frameReader = null;
            }
            QR_CODE_READER.ResultFound -= QR_CODE_READER_ResultFound;
            Application.Current.Suspending -= Current_Suspending;
        }
        /// <summary>
        /// Cleans up the camera resources (after stopping any video recording and/or preview if necessary) and unregisters from MediaCapture events
        /// </summary>
        /// <returns></returns>
        private async Task ShutdownAsync()
        {
            Debug.WriteLine("ShutdownAsync: " + new System.Diagnostics.StackTrace().ToString());

            if (_isInitialized)
            {
                // If a recording is in progress during cleanup, stop it
                if (_mediaFrameReader != null)
                {
                    await _mediaFrameReader.StopAsync();

                    _mediaFrameReader.Dispose();
                }

                _mediaCapture.Dispose();
                _mediaCapture  = null;
                _isInitialized = false;
            }
        }
Exemple #20
0
        /// <summary>
        /// Unregisters FrameArrived event handlers, stops and disposes frame readers
        /// and disposes the MediaCapture object.
        /// </summary>
        private async Task CleanupMediaCaptureAsync()
        {
            if (_mediaCapture != null)
            {
                if (_mediaCapture != null)
                {
                    _mediaCapture.Dispose();
                    _mediaCapture = null;
                }

                if (frameReader != null)
                {
                    frameReader.FrameArrived -= FrameReader_FrameArrived;
                    await frameReader.StopAsync();

                    frameReader.Dispose();
                }
            }
        }
Exemple #21
0
        /// <summary>
        /// Unregisters FrameArrived event handlers, stops and disposes frame readers
        /// and disposes the MediaCapture object.
        /// </summary>
        private async Task CleanupMediaCaptureAsync()
        {
            if (_mediaCapture != null)
            {
                using (var mediaCapture = _mediaCapture)
                {
                    _mediaCapture = null;

                    if (_depthReader != null)
                    {
                        if (_streaming)
                        {
                            _depthReader.FrameArrived -= FrameReader_FrameArrived;
                        }
                        await _depthReader.StopAsync();

                        _depthReader.Dispose();
                    }
                }
            }
        }
Exemple #22
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // dispose managed state (managed objects).
                    mediaFrameReader?.Dispose();
                    mediaFrameReader = null;

                    mediaCapture?.Dispose();
                    mediaCapture = null;

                    evtFrame?.Dispose();
                    evtFrame = null;
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
 internal void Dispose()
 {
     _bodyReader?.Dispose();
     _bodyReader = null;
 }
Exemple #24
0
 internal void Dispose()
 {
     _depthReader?.Dispose();
 }
 internal void Dispose()
 {
     _colorReader?.Dispose();
     _colorReader = null;
 }
Exemple #26
0
        public async void GetCameraIntrinsicsAync(OnCameraIntrinsicsGotCallback onGotCallback)
        {
            Camera​Intrinsics cameraIntrinsics = null;

            // Start video
            var mediaFrameSource = _mediaCapture.FrameSources[_frameSourceInfo.Id]; //Returns a MediaFrameSource

            if (mediaFrameSource == null)
            {
                onGotCallback?.Invoke(null, null);
                return;
            }

            var pixelFormat = MediaEncodingSubtypes.Bgra8;

            _frameReader = await _mediaCapture.CreateFrameReaderAsync(mediaFrameSource, pixelFormat);

            await _frameReader.StartAsync();

            IEnumerable <VideoEncodingProperties> allProperties = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(STREAM_TYPE).Select(x => x as VideoEncodingProperties);

            foreach (var property in allProperties)
            {
                await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(STREAM_TYPE, property);

                // Get CameraIntrinsics
                var taskCompletionSource = new TaskCompletionSource <bool>();

                TypedEventHandler <MediaFrameReader, MediaFrameArrivedEventArgs> handler = null;
                handler = (MediaFrameReader sender, MediaFrameArrivedEventArgs args) =>
                {
                    using (var frameReference = _frameReader.TryAcquireLatestFrame()) //frame: MediaFrameReference
                    {
                        if (frameReference != null)
                        {
                            cameraIntrinsics = frameReference.VideoMediaFrame.CameraIntrinsics;

                            taskCompletionSource.SetResult(true);
                        }
                        else
                        {
                            taskCompletionSource.SetResult(false);
                        }
                    }
                    _frameReader.FrameArrived -= handler;
                };
                _frameReader.FrameArrived += handler;

                var result = await taskCompletionSource.Task;

                if (result == false)
                {
                    onGotCallback?.Invoke(null, null);
                    return;
                }

                onGotCallback?.Invoke(cameraIntrinsics, property);
            }

            // Stop video
            await _frameReader.StopAsync();

            _frameReader.Dispose();
            _frameReader = null;
        }
Exemple #27
0
 internal void Dispose()
 {
     _depthReader?.Dispose();
     _depthReader = null;
 }
Exemple #28
0
        static async Task <int> MainAsync(AppOptions options)
        {
            //Log.WriteLine("pause...");
            //var x = Console.ReadLine();
            Log.WriteLine("Starting async...");
            Model            model      = null;
            AzureConnection  connection = null;
            MediaFrameReader reader     = null;
            EventWaitHandle  evtFrame   = null;

            if (options.List)
            {
                await EnumFrameSourcesAsync();

                Environment.Exit(3);
            }

            await Task.WhenAll(
                Task.Run(async() =>
            {
                try
                {
                    model = await Model.CreateModelAsync(
                        Directory.GetCurrentDirectory() + "\\resources\\office_fruit.onnx", options.Gpu);
                    if (options.Test)
                    {
                        await Task.CompletedTask;
                    }
                    else
                    {
                        connection = await AzureConnection.CreateAzureConnectionAsync();
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLineError("failed to create model {0}", e.ToString());
                    Environment.Exit(2);
                }
            }),
                Task.Run(async() => {
                try
                {
                    (reader, evtFrame) = await GetFrameReaderAsync();
                    await AsyncHelper.AsAsync(reader.StartAsync());
                }
                catch (Exception e)
                {
                    Log.WriteLineError("failed to start frame reader {0}", e.ToString());
                    Environment.Exit(2);
                }
            }));

            try
            {
                AzureModule           m = null;
                EventHandler <string> ModuleLoadedHandler = async(Object sender, string moduleName) =>
                {
                    try
                    {
                        Log.WriteLine("module loaded.   resending state");
                        await connection.NotifyNewModuleOfCurrentStateAsync();
                    }
                    catch (Exception e)
                    {
                        Log.WriteLineError("failed to notify state {0}", e.ToString());
                        Environment.Exit(2);
                    }
                };
                if (connection != null)
                {
                    m = (AzureModule)connection.Module;
                    m.ModuleLoaded += ModuleLoadedHandler;
                }
                try
                {
                    Log.WriteLine("Model loaded, Azure Connection created, and FrameReader Started\n\n\n\n");

                    await CameraProcessingAsync(model, reader, evtFrame, connection);
                } finally
                {
                    if (connection != null)
                    {
                        m.ModuleLoaded -= ModuleLoadedHandler;
                    }
                }
            } finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
                reader.Dispose();
                model.Dispose();
            }
            return(0);
        }
Exemple #29
0
 internal void Dispose()
 {
     _bodyReader?.Dispose();
 }
 internal void Dispose()
 {
     _colorReader?.Dispose();
 }