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 (); }
public override void OutputVideoFrame(CVImageBuffer videoFrame, QTSampleBuffer sampleBuffer, QTCaptureConnection connection) { if (videoFrame == null) { throw new ArgumentNullException ("videoFrame"); } if (sampleBuffer == null) { throw new ArgumentNullException ("sampleBuffer"); } if (connection == null) { throw new ArgumentNullException ("connection"); } if (!capture) { videoFrame.Dispose (); sampleBuffer.Dispose (); return; } Console.WriteLine("Capturing photo"); capture = false; try { var imagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "captured_image.tiff"); var image = CIImage.FromImageBuffer(videoFrame); var imageRep = new NSBitmapImageRep(image); var tiffImage = imageRep.TiffRepresentation; var bytes = new byte[tiffImage.Length]; Marshal.Copy(tiffImage.Bytes, bytes, 0, (int)tiffImage.Length); File.WriteAllBytes(imagePath, bytes); // NSError error; // tiffImage.Save(imagePath, MonoMac.Foundation.NSDataWritingOptions.FileProtectionNone, out error); Console.WriteLine("Wrote image: '{0}'", imagePath); tiffImage.Dispose(); imageRep.Dispose(); videoFrame.Dispose(); sampleBuffer.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public static CMSampleBuffer CreateForImageBuffer (CVImageBuffer imageBuffer, bool dataReady, CMVideoFormatDescription formatDescription, CMSampleTimingInfo sampleTiming, out CMSampleBufferError error) { if (imageBuffer == null) throw new ArgumentNullException ("imageBuffer"); if (formatDescription == null) throw new ArgumentNullException ("formatDescription"); IntPtr buffer; error = CMSampleBufferCreateForImageBuffer (IntPtr.Zero, imageBuffer.handle, dataReady, IntPtr.Zero, IntPtr.Zero, formatDescription.handle, ref sampleTiming, out buffer); if (error != CMSampleBufferError.None) return null; return new CMSampleBuffer (buffer, true); }
public bool VideoMatchesImageBuffer(CVImageBuffer imageBuffer) { if (imageBuffer == null) throw new ArgumentNullException ("imageBuffer"); return CMVideoFormatDescriptionMatchesImageBuffer (handle, imageBuffer.Handle) != 0; }
public static CMVideoFormatDescription CreateForImageBuffer(CVImageBuffer imageBuffer, out CMFormatDescriptionError error) { if (imageBuffer == null) throw new ArgumentNullException ("imageBuffer"); IntPtr desc; error = CMVideoFormatDescriptionCreateForImageBuffer (IntPtr.Zero, imageBuffer.handle, out desc); if (error != CMFormatDescriptionError.None) return null; return new CMVideoFormatDescription (desc, true); }