C # Video header operations

Source: Internet
Author: User

Underlying operation class:

Using system;
Using system. runtime. interopservices;

Namespace getphoto
{
/// <Summary>
/// Summary of avicapture.
/// </Summary>
Public class showvideo
{
// Call acicap32.dll to read camera data
[Dllimport ("avicap32.dll")]
Public static extern intptr capcreatecapturew.wa (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 cbname, 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, frameeventhandler lparam );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, int wparam, ref bitmapinfo lparam );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, int wparam, ref capdrivercaps lparam );
[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 );

// Some 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;
Public const int wm_cap_savedib = wm_user + 25;
Public const int wm_cap_set_overlay = wm_user + 51;
Public const int wm_cap_get_caps = wm_user + 14;
Public const int wm_cap_dlg_videoformat = wm_user + 41;
Public const int wm_cap_dlg_videosource = wm_user + 42;
Public const int wm_cap_dlg_videodisplay = wm_user + 43;
Public const int wm_cap_edit_copy = wm_user + 30;
Public const int wm_cap_set_sequence_setup = wm_user + 64;
Public const int wm_cap_get_sequence_setup = wm_user + 65;


// Structure
[Structlayout (layoutkind. Sequential)]
// Videohdr
Public struct videohdr
{
[Financialas (unmanagedtype. I4)]
Public int lpdata;
[Financialas (unmanagedtype. I4)]
Public int dwbufferlength;
[Financialas (unmanagedtype. I4)]
Public int dwbytesused;
[Financialas (unmanagedtype. I4)]
Public int dwtimecaptured;
[Financialas (unmanagedtype. I4)]
Public int dwuser;
[Financialas (unmanagedtype. I4)]
Public int dwflags;
[Financialas (unmanagedtype. byvalarray, sizeconst = 4)]
Public int [] dwreserved;
}

[Structlayout (layoutkind. Sequential)]
// Bitmapinfoheader
Public struct bitmapinfoheader
{
[Financialas (unmanagedtype. I4)]
Public int32 bisize;
[Financialas (unmanagedtype. I4)]
Public int32 biwidth;
[Financialas (unmanagedtype. I4)]
Public int32 biheight;
[Financialas (unmanagedtype. I2)]
Public short biplanes;
[Financialas (unmanagedtype. I2)]
Public short bibitcount;
[Financialas (unmanagedtype. I4)]
Public int32 bicompression;
[Financialas (unmanagedtype. I4)]
Public int32 bisizeimage;
[Financialas (unmanagedtype. I4)]
Public int32 bixpelspermeter;
[Financialas (unmanagedtype. I4)]
Public int32 biypelspermeter;
[Financialas (unmanagedtype. I4)]
Public int32 biclrused;
[Financialas (unmanagedtype. I4)]
Public int32 biclrimportant;
}

[Structlayout (layoutkind. Sequential)]
// Bitmapinfo
Public struct bitmapinfo
{
[Financialas (unmanagedtype. struct, sizeconst = 40)]
Public bitmapinfoheader bmiheader;
[Financialas (unmanagedtype. byvalarray, sizeconst = 1024)]
Public int32 [] bmicolors;
}

[Structlayout (layoutkind. Sequential)]
Public struct capdrivercaps
{
[Financialas (unmanagedtype. U2)]
Public uint16 wdeviceindex;
[Financialas (unmanagedtype. bool)]
Public bool fhasoverlay;
[Financialas (unmanagedtype. bool)]
Public bool fhasdlgvideosource;
[Financialas (unmanagedtype. bool)]
Public bool fhasdlgvideoformat;
[Financialas (unmanagedtype. bool)]
Public bool fhasdlgvideodisplay;
[Financialas (unmanagedtype. bool)]
Public bool fcaptureinitialized;
[Financialas (unmanagedtype. bool)]
Public bool fdriversuppliespalettes;
[Financialas (unmanagedtype. I4)]
Public int hvideoin;
[Financialas (unmanagedtype. I4)]
Public int hvideoout;
[Financialas (unmanagedtype. I4)]
Public int hvideoextin;
[Financialas (unmanagedtype. I4)]
Public int hvideoextout;
}


Public Delegate void frameeventhandler (intptr lwnd, intptr lpvhdr );

// Public Functions
Public static object getstructure (intptr, 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, 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 );
}
}
}

Function operation class:

Using system;
Using system. runtime. interopservices;

Namespace getphoto
{
/// <Summary>
/// Summary of camera.
/// </Summary>

Public class webcamera
{
Private intptr lwndc;
Private intptr mcontrolptr;
Private int mwidth;
Private int mheight;

// Constructor
Public webcamera (intptr handle, int width, int height)
{
Mcontrolptr = handle;
Mwidth = width;
Mheight = height;
}

// Delegate of frame callback
Public Delegate void recievedframeeventhandler (byte [] data );
Public event recievedframeeventhandler recievedframe;
Private showvideo. frameeventhandler mframeeventhandler;

// Close the camera
Public void closewebcam ()
{
This. capdriverdisconnect (this. lwndc );
}

// enable the camera
Public bool startwebcam ()
{
byte [] lpszname = new byte [100];
byte [] lpszver = new byte [100];
try
{< br> showvideo. capgetdriverdescriptiona (0, lpszname, 100, lpszver, 100);
This. lwndc = showvideo. capcreatecapturew.wa (lpszname, showvideo. ws_visible + showvideo. ws_child, 0, 0, mwidth, mheight, mcontrolptr, 0);

If (this. capdriverconnect (this. lwndc, 0 ))
{
This. cappreviewrate (this. lwndc, 66 );

This. cappreview (this. lwndc, true );
This. capoverlay (this. lwndc, true );
Showvideo. bitmapinfo = new showvideo. bitmapinfo ();
Bitmapinfo. bmiheader. bisize = showvideo. sizeof (bitmapinfo. bmiheader );
Bitmapinfo. bmiheader. biwidth = This. mwidth;
Bitmapinfo. bmiheader. biheight = This. mheight;
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 );
Return true;
}
Else
{
Return false;
}
}
Catch
{
Return false;
}
}

Public void grabimage (string path) // capture a graph to a file
{
Intptr hbmp = marshal. stringtohglobalansi (PATH );
Showvideo. sendmessage (lwndc, showvideo. wm_cap_savedib, 0, hbmp. toint32 ());
}
Public void setcapturesource () // The color Setting dialog box is displayed.
{
Showvideo. capdrivercaps caps = new showvideo. capdrivercaps ();
Showvideo. sendmessage (lwndc, showvideo. wm_cap_get_caps, showvideo. sizeof (CAPs), ref caps );
If (caps. fhasdlgvideosource)
{
Showvideo. sendmessage (lwndc, showvideo. wm_cap_dlg_videosource, 0, 0 );

}
}
Public void setcaptureformat () // The video format Setting dialog box is displayed.
{
Showvideo. capdrivercaps caps = new showvideo. capdrivercaps ();
Showvideo. sendmessage (lwndc, showvideo. wm_cap_get_caps, showvideo. sizeof (CAPs), ref caps );
If (caps. fhasdlgvideosource)
{
Showvideo. sendmessage (lwndc, showvideo. wm_cap_dlg_videoformat, 0, 0 );

}< BR >}< br> Public bool copytoclipboard () // capture the image to the clipboard
{< br> return showvideo. sendmessage (lwndc, showvideo. wm_cap_edit_copy, 0, 0);
}

// The following are 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 BMP format, int capformatsize)
{
Return showvideo. sendmessage (hcapwnd, showvideo. wm_cap_set_videoformat, capformatsize, ref BMP format );
}

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 );
}
Private bool capoverlay (intptr lwnd, bool F)
{
Return showvideo. sendmessage (lwnd, showvideo. wm_cap_set_overlay, F, 0 );
}

}
}

Related Article

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.