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()); } }
void UpdateAudioLevels(NSTimer timer) { // Get the mean audio level from the movie file output's audio connections float totalDecibels = 0f; QTCaptureConnection connection = null; int i = 0; int numberOfPowerLevels = 0; // Keep track of the total number of power levels in order to take the mean // TODO: https://bugzilla.xamarin.com/show_bug.cgi?id=27702 IntPtr library = Dlfcn.dlopen("/System/Library/Frameworks/QTKit.framework/QTKit", 0); NSString soundType = Dlfcn.GetStringConstant(library, "QTMediaTypeSound"); var connections = movieFileOutput.Connections; for (i = 0; i < connections.Length; i++) { connection = connections [i]; if (connection.MediaType == soundType) { // TODO: https://bugzilla.xamarin.com/show_bug.cgi?id=27708 Use typed property NSArray powerLevelsNative = (NSArray)connection.GetAttribute(QTCaptureConnection.AudioAveragePowerLevelsAttribute); NSNumber[] powerLevels = NSArray.FromArray <NSNumber> (powerLevelsNative); int j = 0; int powerLevelCount = powerLevels.Length; for (j = 0; j < powerLevelCount; j++) { totalDecibels += powerLevels [j].FloatValue; numberOfPowerLevels++; } } } if (numberOfPowerLevels > 0) { audioLevelIndicator.FloatValue = 20 * (float)Math.Pow(10, 0.05 * (totalDecibels / numberOfPowerLevels)); } else { audioLevelIndicator.FloatValue = 0; } }
bool ShouldChangeOutputFile (QTCaptureFileOutput captureOutput, NSUrl outputFileURL, QTCaptureConnection[] connections, NSError reason) { // Should change the file on error Console.WriteLine (reason.LocalizedDescription); return false; }