Camera programming

xiaoxiao2021-03-06  79

During this time, engage in video acquisition procedures, find a program that drives the camera written by C # on the foreign website.

After transformation and debugging, as follows:

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

This is a class regarding the idea

Using system; using system.runtime.interopservices;

Namespace Webcam {? ///

"/// Avicap's summary description. ? ///

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 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 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 swap_nomove = 0x2;

?? public const Int swap_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 dwuse;

??? [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, syzeconst = 1024)] public int32 [] bmicolors;

??}

????

?? Public Delegate Void FrameEventHandler (INTPTR LWND, INTPTR LPVHDR);

????

?? // public Methods

?? Public Static Object GetStructure (INTPTR PTR, VALUETYPE STRUCTURE)

?? {

??? Return Marshal.PTRTRTOStructure (PTR, Structure.gettype ());

??}

?

?? Public Static Object GetStructure (int Ptr, ValyEType 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 (int in?t) ?? {??? 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, mHeight, mControlPtr, 0); ????????? ??? if (this.capDriverConnect (this.lwndC, 0)) ??? {???? this.cappreviewrate (this.lwndc, 66); ???? this.cappreview (this.lwndc, true); ???? showvideo.bitmapinfo bitmapinfo = new showvideoBitmapInfo (); ???? bitmapinfo.Bmiheader.bisize = showvideo.sizeof (bitmapinfo.bmihead); ???? 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); ???} ??} ???} ????} "??? {??? Return 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 S howVideo.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);?}}}

The specific call is as follows:

Using system; using system.collections; using system.bomponentmodel; using system.data; using webcam;

Summary description of Namespace Webcam {? ///? /// Form1. ? ///

PUBLIC CLASS FORM1: System.Windows.Forms.form

? {

?? private system.windows.Forms.Panel Panelpreview;

?? private system.windows.Forms.Button B_Play;

?? private system.windows.Forms.Button B_Stop;

?? ///

? /// The required designer variable. ?? ///

?? private system.componentmodel.container components = null;

?? Webcamera WC;

?? public form1 () ?? {??? // ??? // Windows Form Designer Support for the necessary ??? // ??? INIALIZECMOMPONENT ();

??? // ??? // Todo: Add any constructor code after INITIALIZECOMPONENT calls ??? // ??}

?? ///

?? /// Clean all the resources being used. ?? ///

?? protected override void dispose (Bool Disposing)

?? {

??? i (disposing)

??? {

???? if (Components! = NULL)

???? {

????? components.dispose ();

????}

???}

? ?????????????????????????????????????????????????????????????????????????????

??}

? # Region Windows Form Designer Generated code ?? ///

?? /// Designer supports the required method - do not use the code editor to modify ?? // This method. ?? ///

?? private void initializecomponent ()

?? {

??? this.b_play = new system.windows.Forms.Button ();

??? this.panelpreview = new system.windows.forms.panel ();

??? this.b_stop = new system.windows.Forms.Button ();

??? this.suspendlayout ();

??? //

??? // b_play

??? //

??? this.b_play.location = new system.drawing.point (280, 368);

??? this.b_play.name = "b_play";

??? this.b_play.tabindex = 0;

??? THIS.B_PLAY.TEXT = "& play";

??? this.b_play.click = new system.eventhandler (this.button1_click);

??? //

?? // panelpreview

??? //

??? this.panelpreview.location = new system.drawing.point (8, 8);

??? this.panelpreview.name = "panelpreview";

??? this.panelpreview.size = new system.drawing.size (344, 272);

??? this.panelpreview.tabindex = 1;

??? //

??? // b_stop

??? //

??? THIS.B_STOP.ENABLED = false;

??? this.b_stop.location = new system.drawing.point (360, 368); ??? THIS.B_STOP.NAME = "B_Stop";

??? this.b_stop.tabindex = 2;

??? THIS.B_STOP.TEXT = "& STOP";

??? this.b_stop.click = new system.eventhandler (this.b_stop_click);

??? //

??? // Form1

??? //

??? this.autoscalebasesize = new system.drawing.size (6, 14);

??? this.clientsize = new system.drawing.size (464, 413);

??? this.Controls.add (this.b_stop);

??? this.Controls.add (this.Panelpreview);

??? this.controls.add (this.b_play);

??? this.maximizebox = false;

??? this.minimizebox = false;

??? this.name = "form1";

??? this.text = "Goodview Test Web Camera";

??? this.load = new system.eventhandler (this.form1_load);

??? this.ResumeLayout (false);

??} ?? # endregion

?? ///

??/// The main entry point for the application. ?? ///

?? [stathread]

?? static void main ()

?? {

??? Application.run (New Form1 ());

??}

?? private void form1_load (Object sender, system.eventargs e) ?? {??? b_play.enabled = false; ??? b_stop.enabled = true; ??? panelpreview.size = new size (330, 330); ?? ? wc = new Webcamera (PanelPreview.Handle, Panelpreview.Height); ??? wc.startwebcam (); ??}

?? private void button1_click (object sender, system.eventargs e) ?? {??? b_play.enabled = false; ??? b_stop.enabled = true; ??? panelpreview.size = new size (330, 330);?? ? wc = new Webcamera (PanelPreview.Handle, Panelpreview.Height); ??? wc.startwebcam (); ??}

?? private void b_stop_click (Object sender, system.eventargs e) ?? {??? b_play.enabled = true; ??? b_stop.enabled = false; ??? wc.closewebcam ();??}?}}

转载请注明原文地址:https://www.9cbs.com/read-120813.html

New Post(0)