none
C# Webcam'dan Görüntü Alma RRS feed

  • Soru

  • Merhaba, webcamdan görüntü almam gerekiyor ama herhangi bir kütüphaneye ihtiyacım yokmuş(Hocamız öyle söyledi). Kütüphaneye ihtiyaç olmadan nasıl görüntü alabilirim?
    26 Şubat 2018 Pazartesi 09:32

Yanıtlar

  • Dışarıdan bir kütüphaneye ihtiyaç olmayabilir.

    Windows işletim sisteminin kütüphanelerine ihtiyaç var. (user32.dll, avicap32.dll, GDI32.dll)

    Bu sınıfı kullanabilirsiniz...

            class Camera
            {
                #region api
                static int WS_CHILD = 0x40000000;
                int WS_VISIBLE = 0x10000000;
                //short SWP_NOMOVE = 0x2;
                //short SWP_NOZORDER = 0x4;
                static short WM_USER = 0x400;
                int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
                int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
                int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
                int WM_CAP_SET_PREVIEW = WM_USER + 50;
                int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
                //long WM_CAP_GET_FRAME = 1084;
                //long WM_CAP_COPY = 1054;
                static long WM_CAP_START = WM_USER;
                long WM_CAP_STOP = (WM_CAP_START + 68);
                long WM_CAP_SEQUENCE = (WM_CAP_START + 62);
                long WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START + 64);
                long WM_CAP_FILE_SET_CAPTURE_FILEA = (WM_CAP_START + 20);
                [DllImport("user32.dll")]
                private static extern int SendMessage(int hwnd, int wMsg, short wParam, string lParam);
                [DllImport("avicap32.dll")]
                private static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
                [DllImport("avicap32.dll")]
                private static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
                [DllImport("GDI32.dll")]
                private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);
                #endregion
    
                #region properties
                private string iDevice;
                private int hHwnd;
                private int lwndC;
                public bool iRunning;
                private int CamFrameRate = 15;
                private int OutputHeight = 240;
                private int OutputWidth = 360;
                public int FPS
                {
                    get
                    {
                        return 1000 / CamFrameRate;
                    }
                }
                #endregion
    
                #region functions
                public Camera()
                {
                }
                public void resetCam()
                {
                    if (iRunning)
                    {
                        closeCam();
                        if (!setCam())
                            MessageBox.Show("Kamera ayarlarında bir hata oluştu.");
                    }
                }
                public void initCam(int parentH)
                {
                    if (iRunning)
                        MessageBox.Show("Kamera zaten çalışıyor.");
                    else hHwnd = capCreateCaptureWindowA(iDevice, (WS_VISIBLE | WS_CHILD), 0, 0, OutputWidth, (short)OutputHeight, parentH, 0);
                    if (!setCam())
                        MessageBox.Show("Kamera hata verdi.");
                }
                public void setFrameRate(long iRate)
                {
                    CamFrameRate = (int)(1000 / iRate);
                    resetCam();
                }
                private bool setCam()
                {
                    if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, 0, 0.ToString()) == 1)
                    {
                        SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, (short)CamFrameRate, 0.ToString());
                        SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0.ToString());
                        iRunning = true;
                        return true;
                    }
                    else
                    {
                        iRunning = false;
                        return false;
                    }
                }
                public bool closeCam()
                {
                    if (iRunning)
                    {
                        iRunning = false;
                        return SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0.ToString()) == 0;
                    }
                    else return true;
                }
                public Bitmap copyFrame(PictureBox src, RectangleF rect)
                {
                    if (iRunning)
                    {
                        Graphics srcPic = src.CreateGraphics();
                        Bitmap srcBmp = new Bitmap(src.Width, src.Height, srcPic);
                        Graphics srcMem = Graphics.FromImage(srcBmp);
                        IntPtr HDC1 = srcPic.GetHdc();
                        IntPtr HDC2 = srcMem.GetHdc();
                        BitBlt(HDC2, 0, 0, (int)rect.Width, (int)rect.Height, HDC1, (int)rect.X, (int)rect.Y, 13369376);
                        srcPic.ReleaseHdc(HDC1);
                        srcMem.ReleaseHdc(HDC2);
                        srcPic.Dispose();
                        srcMem.Dispose();
                        return (Bitmap)(srcBmp.Clone());
                    }
                    else
                    {
                        MessageBox.Show("Kamera çalışmıyor!");
                        return null;
                    }
                }
                #endregion
            }
    


    Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...

    26 Şubat 2018 Pazartesi 10:09

Tüm Yanıtlar

  • Dışarıdan bir kütüphaneye ihtiyaç olmayabilir.

    Windows işletim sisteminin kütüphanelerine ihtiyaç var. (user32.dll, avicap32.dll, GDI32.dll)

    Bu sınıfı kullanabilirsiniz...

            class Camera
            {
                #region api
                static int WS_CHILD = 0x40000000;
                int WS_VISIBLE = 0x10000000;
                //short SWP_NOMOVE = 0x2;
                //short SWP_NOZORDER = 0x4;
                static short WM_USER = 0x400;
                int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
                int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
                int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
                int WM_CAP_SET_PREVIEW = WM_USER + 50;
                int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
                //long WM_CAP_GET_FRAME = 1084;
                //long WM_CAP_COPY = 1054;
                static long WM_CAP_START = WM_USER;
                long WM_CAP_STOP = (WM_CAP_START + 68);
                long WM_CAP_SEQUENCE = (WM_CAP_START + 62);
                long WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START + 64);
                long WM_CAP_FILE_SET_CAPTURE_FILEA = (WM_CAP_START + 20);
                [DllImport("user32.dll")]
                private static extern int SendMessage(int hwnd, int wMsg, short wParam, string lParam);
                [DllImport("avicap32.dll")]
                private static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
                [DllImport("avicap32.dll")]
                private static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
                [DllImport("GDI32.dll")]
                private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);
                #endregion
    
                #region properties
                private string iDevice;
                private int hHwnd;
                private int lwndC;
                public bool iRunning;
                private int CamFrameRate = 15;
                private int OutputHeight = 240;
                private int OutputWidth = 360;
                public int FPS
                {
                    get
                    {
                        return 1000 / CamFrameRate;
                    }
                }
                #endregion
    
                #region functions
                public Camera()
                {
                }
                public void resetCam()
                {
                    if (iRunning)
                    {
                        closeCam();
                        if (!setCam())
                            MessageBox.Show("Kamera ayarlarında bir hata oluştu.");
                    }
                }
                public void initCam(int parentH)
                {
                    if (iRunning)
                        MessageBox.Show("Kamera zaten çalışıyor.");
                    else hHwnd = capCreateCaptureWindowA(iDevice, (WS_VISIBLE | WS_CHILD), 0, 0, OutputWidth, (short)OutputHeight, parentH, 0);
                    if (!setCam())
                        MessageBox.Show("Kamera hata verdi.");
                }
                public void setFrameRate(long iRate)
                {
                    CamFrameRate = (int)(1000 / iRate);
                    resetCam();
                }
                private bool setCam()
                {
                    if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, 0, 0.ToString()) == 1)
                    {
                        SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, (short)CamFrameRate, 0.ToString());
                        SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0.ToString());
                        iRunning = true;
                        return true;
                    }
                    else
                    {
                        iRunning = false;
                        return false;
                    }
                }
                public bool closeCam()
                {
                    if (iRunning)
                    {
                        iRunning = false;
                        return SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0.ToString()) == 0;
                    }
                    else return true;
                }
                public Bitmap copyFrame(PictureBox src, RectangleF rect)
                {
                    if (iRunning)
                    {
                        Graphics srcPic = src.CreateGraphics();
                        Bitmap srcBmp = new Bitmap(src.Width, src.Height, srcPic);
                        Graphics srcMem = Graphics.FromImage(srcBmp);
                        IntPtr HDC1 = srcPic.GetHdc();
                        IntPtr HDC2 = srcMem.GetHdc();
                        BitBlt(HDC2, 0, 0, (int)rect.Width, (int)rect.Height, HDC1, (int)rect.X, (int)rect.Y, 13369376);
                        srcPic.ReleaseHdc(HDC1);
                        srcMem.ReleaseHdc(HDC2);
                        srcPic.Dispose();
                        srcMem.Dispose();
                        return (Bitmap)(srcBmp.Clone());
                    }
                    else
                    {
                        MessageBox.Show("Kamera çalışmıyor!");
                        return null;
                    }
                }
                #endregion
            }
    


    Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...

    26 Şubat 2018 Pazartesi 10:09
  • Bu sınıfı kullanabilmek için formunuza bir PictureBox ekleyin. Genişliği 360 ve yüksekliği 240 olsun.

    Camera cam = new Camera();
    cam.initCam((int)pictureBox1.Handle);
    

    Başarılar...


    Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...

    26 Şubat 2018 Pazartesi 10:16