public void onImageResults(Dictionary <int, Affdex.Face> faces, Affdex.Frame frame) { process_fps = 1.0f / (frame.getTimestamp() - process_last_timestamp); process_last_timestamp = frame.getTimestamp(); System.Console.WriteLine(" pfps: {0}", process_fps.ToString()); byte[] pixels = frame.getBGRByteArray(); this.img = new Bitmap(frame.getWidth(), frame.getHeight(), PixelFormat.Format24bppRgb); var bounds = new Rectangle(0, 0, frame.getWidth(), frame.getHeight()); BitmapData bmpData = img.LockBits(bounds, ImageLockMode.WriteOnly, img.PixelFormat); IntPtr ptr = bmpData.Scan0; int data_x = 0; int ptr_x = 0; int row_bytes = frame.getWidth() * 3; // The bitmap requires bitmap data to be byte aligned. // http://stackoverflow.com/questions/20743134/converting-opencv-image-to-gdi-bitmap-doesnt-work-depends-on-image-size for (int y = 0; y < frame.getHeight(); y++) { Marshal.Copy(pixels, data_x, ptr + ptr_x, row_bytes); data_x += row_bytes; ptr_x += bmpData.Stride; } img.UnlockBits(bmpData); this.faces = faces; //rwLock.ReleaseWriterLock(); this.Invalidate(); frame.Dispose(); }
/// <summary> /// Handles the Image capture from source produced by Affdex.Detector /// </summary> /// <param name="image">The <see cref="Affdex.Frame"/> instance containing the image captured from camera.</param> public void onImageCapture(Affdex.Frame frame) { byte[] imageData = frame.getBGRByteArray(); int width = frame.getWidth(); int height = frame.getHeight(); try { if (imageData != null && imageData.Length > 0) { var stride = (width * PixelFormats.Bgr24.BitsPerPixel + 7) / 8; var imageSrc = BitmapSource.Create(width, height, 96d, 96d, PixelFormats.Bgr24, null, imageData, stride); SaveImageToFile(imageSrc, System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, frame.getTimestamp().ToString() + ".png")); } } catch (Exception ex) { } }