Exemplo n.º 1
0
        //
        // Not strictly necessary, but the movie might be very large, so we might
        // as well help the system by disposing before the GC kicks-in
        //
        protected override void Dispose(bool disposing)
        {
            if (captureSession != null)
            {
                captureSession.StopRunning();
            }

            if (captureInput != null && captureInput.Device != null && captureInput.Device.IsOpen)
            {
                captureInput.Device.Close();
            }

            if (disposing)
            {
                movie.Dispose();
                captureSession.Dispose();
                captureInput.Dispose();
                decompressedVideo.Dispose();
            }

            base.Dispose(disposing);
        }
        public override void WindowControllerDidLoadNib(NSWindowController windowController)
        {
            base.WindowControllerDidLoadNib(windowController);

            // A reference to the window controller must be kept on the managed side
            // to keep the object from being GC'd so that the delegates below resolve.
            // Don't remove unless the framework is updated to track the reference.
            this.windowController = windowController;

            NSError err;

            windowController.Window.WillClose += delegate {
                if (captureSession != null)
                {
                    captureSession.StopRunning();
                }
                var dev = captureInput.Device;
                if (dev.IsOpen)
                {
                    dev.Close();
                }
            };

            // Create a movie, and store the information in memory on an NSMutableData
            movie = new QTMovie(new NSMutableData(1), out err);
            if (movie == null)
            {
                NSAlert.WithError(err).RunModal();
                return;
            }
            movieView.Movie = movie;

            // Find video device
            captureSession = new QTCaptureSession();
            var device = QTCaptureDevice.GetDefaultInputDevice(QTMediaType.Video);

            if (!device.Open(out err))
            {
                NSAlert.WithError(err).RunModal();
                return;
            }

            // Add device input
            captureInput = new QTCaptureDeviceInput(device);
            if (!captureSession.AddInput(captureInput, out err))
            {
                NSAlert.WithError(err).RunModal();
                return;
            }

            // Create decompressor for video output, to get raw frames
            decompressedVideo = new QTCaptureDecompressedVideoOutput();
            decompressedVideo.DidOutputVideoFrame += delegate(object sender, QTCaptureVideoFrameEventArgs e) {
                lock (this){
                    currentImage = e.VideoFrame;
                }
            };
            if (!captureSession.AddOutput(decompressedVideo, out err))
            {
                NSAlert.WithError(err).RunModal();
                return;
            }

            // Activate preview
            captureView.CaptureSession = captureSession;

            // Start running.
            captureSession.StartRunning();
        }