欢迎光临
我们一直在努力

一个用于驱动摄像头的类-.NET教程,评论及其它

建站超值云服务器,限时71元/月

本文是借助avicap32.dll库来驱动摄像头。做到了抓图、录像功能。

using system;

using system.runtime.interopservices;

using system.drawing;

using system.drawing.imaging;

namespace using system;

using system.runtime.interopservices;

using system.drawing;

using system.drawing.imaging;

namespace pickhead

{

/// <summary>

/// 一个控制摄像头的类

/// </summary>

public class pick

{

private const int wm_user = 0x400;

private const int ws_child = 0x40000000;

private const int ws_visible = 0x10000000;

private const int wm_cap_start = wm_user;

private const int wm_cap_stop = wm_cap_start + 68;

private const int wm_cap_driver_connect = wm_cap_start + 10;

private const int wm_cap_driver_disconnect = wm_cap_start + 11;

private const int wm_cap_savedib = wm_cap_start + 25;

private const int wm_cap_grab_frame = wm_cap_start + 60;

private const int wm_cap_sequence = wm_cap_start + 62;

private const int wm_cap_file_set_capture_filea = wm_cap_start + 20;

private const int wm_cap_sequence_nofile =wm_cap_start+ 63;

private const int wm_cap_set_overlay =wm_cap_start+ 51;

private const int wm_cap_set_preview =wm_cap_start+ 50;

private const int wm_cap_set_callback_videostream = wm_cap_start +6;

private const int wm_cap_set_callback_error=wm_cap_start +2;

private const int wm_cap_set_callback_statusa= wm_cap_start +3;

private const int wm_cap_set_callback_frame= wm_cap_start +5;

private const int wm_cap_set_scale=wm_cap_start+ 53;

private const int wm_cap_set_previewrate=wm_cap_start+ 52;

private intptr hwndc;

private bool bstat = false;

private intptr mcontrolptr;

private int mwidth;

private int mheight;

private int mleft;

private int mtop;

/// <summary>

/// 初始化摄像头

/// </summary>

/// <param name="handle">控件的句柄</param>

/// <param name="left">开始显示的左边距</param>

/// <param name="top">开始显示的上边距</param>

/// <param name="width">要显示的宽度</param>

/// <param name="height">要显示的长度</param>

public pick(intptr handle, int left, int top, int width,int height)

{

mcontrolptr = handle;

mwidth = width;

mheight = height;

mleft = left;

mtop = top;

}

[dllimport("avicap32.dll")]

private static extern intptr capcreatecapturewindowa(byte[] lpszwindowname, int dwstyle, int x, int y, int nwidth, int nheight, intptr hwndparent, int nid);

[dllimport("avicap32.dll")]

private static extern int capgetvideoformat(intptr hwnd, intptr psvideoformat, int wsize );

[dllimport("user32.dll")]

private static extern bool sendmessage(intptr hwnd, int wmsg, int wparam, long lparam);

/// <summary>

/// 开始显示图像

/// </summary>

public void start()

{

if (bstat)

return;

bstat = true;

byte[] lpszname = new byte[100];

hwndc = capcreatecapturewindowa(lpszname,ws_child|ws_visible ,mleft,mtop,mwidth,mheight,mcontrolptr,0);

if (hwndc.toint32() != 0)

{

sendmessage(hwndc, wm_cap_set_callback_videostream, 0, 0);

sendmessage(hwndc, wm_cap_set_callback_error, 0, 0);

sendmessage(hwndc, wm_cap_set_callback_statusa, 0, 0);

sendmessage(hwndc, wm_cap_driver_connect, 0, 0);

sendmessage(hwndc, wm_cap_set_scale, 1, 0);

sendmessage(hwndc, wm_cap_set_previewrate, 66, 0);

sendmessage(hwndc, wm_cap_set_overlay, 1, 0);

sendmessage(hwndc, wm_cap_set_preview, 1, 0);

}

return;

}

/// <summary>

/// 停止显示

/// </summary>

public void stop()

{

sendmessage(hwndc, wm_cap_driver_disconnect, 0, 0);

bstat = false;

}

/// <summary>

/// 抓图

/// </summary>

/// <param name="path">要保存bmp文件的路径</param>

public void grabimage(string path)

{

intptr hbmp = marshal.stringtohglobalansi(path);

sendmessage(hwndc,wm_cap_savedib,0,hbmp.toint64());

}

/// <summary>

/// 录像

/// </summary>

/// <param name="path">要保存avi文件的路径</param>

public void kinescope(string path)

{

intptr hbmp = marshal.stringtohglobalansi(path);

sendmessage(hwndc,wm_cap_file_set_capture_filea,0,hbmp.toint64());

sendmessage(hwndc, wm_cap_sequence, 0, 0);

}

/// <summary>

/// 停止录像

/// </summary>

public void stopkinescope()

{

sendmessage(hwndc, wm_cap_stop, 0, 0);

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个用于驱动摄像头的类-.NET教程,评论及其它
分享到: 更多 (0)