欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实现摄像头拍照功能示例

程序员文章站 2023-12-19 10:04:22
复制代码 代码如下:using system.drawing;using system.drawing.imaging;using system;using system....

复制代码 代码如下:

using system.drawing;
using system.drawing.imaging;
using system;
using system.runtime.interopservices;

复制代码 代码如下:

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 videoclass(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);

[dllimport("avicap32.dll")]
public static extern bool capgetdriverdescriptiona(short wdriver, byte[] lpszname, int cbname, byte[] lpszver, int cbver);

复制代码 代码如下:

/// <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);
        }

上一篇:

下一篇: