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(); }
public void onImageCapture(Affdex.Frame frame) { #region 使用一下代码测试帧捕获的图片是否可以正常生成图片,如果不能则是输入给帧的像素的数组有问题 var len = frame.getBGRByteArrayLength(); byte[] imageData = frame.getBGRByteArray();//这里捕获的数据 不同于生成该frame时的buff 并且是3通道的数据 int width = frame.getWidth(); int height = frame.getHeight(); var ColorFormat = frame.getColorFormat(); if (imageData != null && imageData.Length > 0) { var _stride = (width * System.Windows.Media.PixelFormats.Rgb24.BitsPerPixel + 7) / 8; var imageSrc = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96d, 96d, System.Windows.Media.PixelFormats.Bgr24, null, imageData, _stride); System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(imageSrc)); using (var stream = new System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "我是分析前图片.png"), System.IO.FileMode.Create)) { encoder.Save(stream); } } #endregion }
private void DrawCapturedImage(Affdex.Frame image) { var result = this.Dispatcher.BeginInvoke((Action)(() => { try { cameraDisplay.Source = ConstructImage(image.getBGRByteArray(), image.getWidth(), image.getHeight()); if (++DrawSkipCount > 4) { canvas.Faces = new Dictionary <int, Affdex.Face>(); canvas.InvalidateVisual(); DrawSkipCount = 0; } if (image != null) { image.Dispose(); } } catch (Exception ex) { String message = String.IsNullOrEmpty(ex.Message) ? "AffdexMe error encountered." : ex.Message; ShowExceptionAndShutDown(message); } })); }
private void DrawData(Affdex.Frame image, Dictionary <int, Affdex.Face> faces) { try { if (faces != null) { var result = this.Dispatcher.BeginInvoke((Action)(() => { if ((Detector != null) && (Detector.isRunning())) { canvas.Faces = faces; canvas.Width = cameraDisplay.ActualWidth; canvas.Height = cameraDisplay.ActualHeight; canvas.XScale = canvas.Width / image.getWidth(); canvas.YScale = canvas.Height / image.getHeight(); canvas.InvalidateVisual(); DrawSkipCount = 0; } })); } } catch (Exception ex) { String message = String.IsNullOrEmpty(ex.Message) ? "AffdexMe error encountered." : ex.Message; ShowExceptionAndShutDown(message); } }
/// <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) { } }
/// <summary> /// Draws the image captured from the camera. /// </summary> /// <param name="image">The image captured.</param> private void DrawCapturedImage(Affdex.Frame image) { // Update the Image control from the UI thread try { // Update the Image control from the UI thread //cameraDisplay.Source = rtb; pictureBox1.Image = BitmapFromSource(ConstructImage(image.getBGRByteArray(), image.getWidth(), image.getHeight())); // Allow N successive OnCapture callbacks before the FacePoint drawing canvas gets cleared. if (image != null) { image.Dispose(); } } catch (Exception ex) { String message = String.IsNullOrEmpty(ex.Message) ? "AffdexMe error encountered." : ex.Message; // ShowExceptionAndShutDown(message); } }