C # Camera Programming

Source: Internet
Author: User
Tags bool keep alive

Camera programming

After installing the camera, you can usually find a Avicap32.dll file

This is a class about envisioning the head

using System;


using System.Runtime.InteropServices;


namespace Webcam


{


///

Summary description of the
///AVICap.


///


public class Showvideo


{


//Showvideo calls


[DllImport ("Avicap32.dll")] public static extern IntPtr Capcreatecapturewindowa (byte[] lpszwindowname, int dwstyle , int x, int y, int nwidth, int nheight, IntPtr hwndparent, int nID);


[DllImport ("Avicap32.dll")] public static extern bool Capgetdriverdescriptiona (short wdriver, byte[] lpszname, int c Bname, byte[] lpszver, int cbver);


[DllImport ("User32.dll")] public static extern bool SendMessage (IntPtr hWnd, int wmsg, bool wParam, int lParam);


[DllImport ("User32.dll")] public static extern bool SendMessage (IntPtr hWnd, int wmsg, short wParam, int lParam);


[DllImport ("User32.dll")] public static extern bool SendMessage (IntPtr hWnd, int wmsg, short wParam, Frameeventhandl Er lParam);


[DllImport ("User32.dll")] public static extern bool SendMessage (IntPtr hWnd, int wmsg, int wParam, ref bitmapinfo LP Aram);


[DllImport ("User32.dll")] public static extern int SetWindowPos (IntPtr hWnd, int hwndinsertafter, int x, int y, int CX, int cy, int wflags);


[DllImport ("Avicap32.dll")]public static extern int Capgetvideoformat (IntPtr hWnd, IntPtr psvideoformat, int wsize) ;


//Constants


Public const int wm_user = 0x400;


Public const int ws_child = 0x40000000;


Public const int ws_visible = 0x10000000;


Public const int swp_nomove = 0x2;


Public const int swp_nozorder = 0x4;


Public const int wm_cap_driver_connect = Wm_user 10;


public const int wm_cap_driver_disconnect = Wm_user 11;


Public const int wm_cap_set_callback_frame = Wm_user 5;


Public const int wm_cap_set_preview = Wm_user 50;


Public const int wm_cap_set_previewrate = Wm_user 52;


Public const int wm_cap_set_videoformat = Wm_user 45;





//Structures


[StructLayout (layoutkind.sequential)] public struct VIDEOHDR


{


[MarshalAs (UNMANAGEDTYPE.I4)] public int lpdata;


[MarshalAs (UNMANAGEDTYPE.I4)] public int dwbufferlength;


[MarshalAs (UNMANAGEDTYPE.I4)] public int dwbytesused;


[MarshalAs (UNMANAGEDTYPE.I4)] public int dwtimecaptured;


[MarshalAs (UNMANAGEDTYPE.I4)] public int dwuser;


[MarshalAs (UNMANAGEDTYPE.I4)] public int dwflags;


[MarshalAs (UnmanagedType.ByValArray, sizeconst=4)] public int[] dwreserved;


}


[StructLayout (layoutkind.sequential)] public struct Bitmapinfoheader


{


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 bisize;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 biwidth;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 biheight;


[MarshalAs (UNMANAGEDTYPE.I2)] public short biplanes;


[MarshalAs (UNMANAGEDTYPE.I2)] public short bibitcount;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 bicompression;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 bisizeimage;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 bixpelspermeter;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 biypelspermeter;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 biclrused;


[MarshalAs (UNMANAGEDTYPE.I4)] public Int32 biclrimportant;


}


[StructLayout (layoutkind.sequential)] public struct bitmapinfo


{


[MarshalAs (Unmanagedtype.struct, sizeconst=40)] public bitmapinfoheader bmiheader;


[MarshalAs (UnmanagedType.ByValArray, sizeconst=1024)] public int32[] bmicolors;


}





public delegate void Frameeventhandler (IntPtr lwnd, IntPtr LPVHDR);





//Public methods


public static Object Getstructure (INTPTR ptr,valuetype structure)


{


return Marshal.PtrToStructure (ptr,structure. GetType ());


}





public static object Getstructure (int ptr,valuetype structure)


{


return Getstructure (New IntPtr (PTR), structure);


}





public static void Copy (INTPTR ptr,byte[] data)


{


marshal.copy (Ptr,data,0,data. Length);


}





public static void Copy (int ptr,byte[] data)


{


Copy (New IntPtr (PTR), data);


}





public static int SizeOf (object structure)


{


return marshal.sizeof (structure);


}


}


//web Camera Class


public class Webcamera


{


//Constructur


public Webcamera (IntPtr handle, int width,int height)


{


mcontrolptr = handle;


mwidth = width;


mheight = height;


}





//Delegate for frame callback


public delegate void Recievedframeeventhandler (byte[] data);


public event Recievedframeeventhandler Recievedframe;





private IntPtr LWNDC; Holds the unmanaged handle of the control


private IntPtr mcontrolptr; Holds the managed pointer of the control


private int mwidth;


private int mheight;





private Showvideo.frameeventhandler Mframeeventhandler; Delegate instance for the frame Callback-must keep alive! GC should not collect it





//Close the Web camera


public void Closewebcam ()


{


This.capdriverdisconnect (THIS.LWNDC);


}





//Start the Web camera


public void Startwebcam ()


{


byte[] lpszname = new byte[100];


byte[] Lpszver = new byte[100];





Showvideo.capgetdriverdescriptiona (0, Lpszname, 100,lpszver, 100);


THIS.LWNDC = Showvideo.capcreatecapturewindowa (lpszname, showvideo.ws_visible showVideo.WS_CHILD, 0, 0, MWidth, MH Eight, mcontrolptr, 0);





if (this.capdriverconnect (THIS.LWNDC, 0))


{


this.cappreviewrate (THIS.LWNDC, 66);


This.cappreview (THIS.LWNDC, true);


showvideo.bitmapinfo bitmapinfo = new Showvideo.bitmapinfo ();


bitmapinfo.bmiHeader.biSize = showvideo.sizeof (Bitmapinfo.bmiheader);


bitmapinfo.bmiHeader.biWidth = 352;


bitmapinfo.bmiHeader.biHeight = 288;


bitmapinfo.bmiHeader.biPlanes = 1;


Bitmapinfo.bmiHeader.biBitCount = 24;


This.capsetvideoformat (THIS.LWNDC, ref bitmapinfo, Showvideo.sizeof (bitmapinfo));


This.mframeeventhandler = new Showvideo.frameeventhandler (framecallback);


this.capsetcallbackonframe (THIS.LWNDC, This.mframeeventhandler);


Showvideo.setwindowpos (THIS.LWNDC, 0, 0, 0, Mwidth, mheight, 6);


}


}


//Private Functions


private bool Capdriverconnect (IntPtr Lwnd, short i)


{


Return Showvideo.sendmessage (Lwnd, Showvideo.wm_cap_driver_connect, I, 0);


}


private bool Capdriverdisconnect (IntPtr lwnd)


{


return Showvideo.sendmessage (Lwnd, showvideo.wm_cap_driver_disconnect, 0, 0);


}





private bool Cappreview (INTPTR Lwnd, bool f)


{


return Showvideo.sendmessage (Lwnd, Showvideo.wm_cap_set_preview, F, 0);


}


private bool Cappreviewrate (IntPtr lwnd, short wms)


{


return Showvideo.sendmessage (Lwnd, showvideo.wm_cap_set_previewrate, WMS, 0);


}





private bool Capsetcallbackonframe (IntPtr Lwnd, Showvideo.frameeventhandler lpproc)


{ 


return Showvideo.sendmessage (Lwnd, showvideo.wm_cap_set_callback_frame, 0, Lpproc);


}


private bool Capsetvideoformat (IntPtr hcapwnd, ref showvideo.bitmapinfo Bmpformat, int capformatsize)


{


return Showvideo.sendmessage (Hcapwnd, Showvideo.wm_cap_set_videoformat, capformatsize, ref BmpFormat);


}


private void Framecallback (IntPtr lwnd, IntPtr lpvhdr)


{


SHOWVIDEO.VIDEOHDR videoheader = new Showvideo.videohdr ();


byte[] Videodata;


Videoheader = (SHOWVIDEO.VIDEOHDR) showvideo.getstructure (Lpvhdr,videoheader);


videodata = new byte[videoheader.dwbytesused];


showvideo.copy (Videoheader.lpdata, videodata);


if (this. Recievedframe!= null)


this. Recievedframe (Videodata);


}


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.