2011年5月28日 星期六

emgu WPF

Emgu in WPF example
1. Add reference
    Emgu.CV.dll
    Emgu.CV.UI.dll
    Emgu.Util.dll
2. namespace
    //---------------------
using System.IO;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.Util;
using System.Threading;
using System.Timers;
using System.Runtime.InteropServices;
using System.Windows.Threading;
3. Window1.xaml
3.1 Set window load and closing events: Loaded="Window_Loaded" Closing="Window_Closing"
3.2 Build Image tag (Testing rendering functions)
<Image x:Name="webcam" Width="640" Height="480" >
            <Image.Clip>
                <EllipseGeometry  RadiusX="240" RadiusY="240">
                    <EllipseGeometry.Center>
                        <Point X="320" Y="240" />
                    </EllipseGeometry.Center>
                </EllipseGeometry>
            </Image.Clip>
            <Image.RenderTransform>
                <RotateTransform Angle="15"></RotateTransform>
            </Image.RenderTransform>
        </Image>
4. Wndow1.xaml.cs
        private Capture capture;
        private DispatcherTimer timer;
        //ImageBox im = new ImageBox();
        /// <summary>
        /// Window1.xaml 的互動邏輯
        /// </summary>
        [DllImport("gdi32")]
        private static extern int DeleteObject(IntPtr o);
        /// <summary>
        /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
        /// </summary>
        /// <param name="image">The Emgu CV Image</param>
        /// <returns>The equivalent BitmapSource</returns>
        public static BitmapSource ToBitmapSource(IImage image)
        {
            using (System.Drawing.Bitmap source = image.Bitmap)
            {
                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    ptr,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                DeleteObject(ptr); //release the HBitmap
                return bs;
            }
        }

5. Form_load and timer event methods
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //G1.Children.Add(im);
            capture = new Capture();
            capture.FlipHorizontal = true;
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(150000); // 150000 x 100 ns  = 1.5 E-4 s 15 ms
            //timer.Interval = 15;
            timer.Tick += new EventHandler(timer_Tick);
            //timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            using (Image<Bgr, byte> frame = capture.QueryFrame())
            {
                if (frame != null)
                {
                    //var bmp = frame.Bitmap;
                    // How do I pass this bitmap to my Image control called "webcam"?
                    //webcam.Source = frame.Bitmap;
                    //MemoryStream ms = new MemoryStream();
                    //System.Drawing.Bitmap bmp = frame.Bitmap;
                    //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //BitmapImage bi = new BitmapImage();
                    //bi.BeginInit();
                    //bi.StreamSource = ms;
                    //bi.EndInit();
                    webcam.Source = ToBitmapSource(frame);
                }
            }
        }
6. Window_Closing
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
             if (capture != null)
            {
                capture.Dispose();
            }
        }

沒有留言:

張貼留言