using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
class WebCam
{
const int WM_USER = 0x400;
const int WS_CHILD = 0x40000000;
const int WS_VISIBLE = 0x10000000;
const int WM_CAP_START = WM_USER;
const int WM_CAP_STOP = WM_CAP_START + 68;
const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
const int WM_CAP_SET_EDIT_COPY = WM_CAP_START + 30;
IntPtr hWndc;
bool bStat;
IntPtr mControlPtr;
int mWidth;
int mHeight;
int mLeft;
int mTop;
string grabImagePath = "";
string kinescopePath = "";
///
/// 初始化摄像头
///
/// 控件句柄
///
///
///
///
public WebCam(IntPtr handle,int left,int top,int width,int height)
{
mControlPtr = handle;
mLeft = left;
mTop = top;
mWidth = width;
mHeight = height;
}
public int Left
{
get { return mLeft; }
set { mLeft = value; }
}
public int Top
{
get { return mTop;
}
set
{
mTop = value;
}
}
public int Width
{
set
{
mWidth = value;
}
get
{
return mWidth;
}
}
public int Height
{
set
{
mHeight = value;
}
get
{
return mHeight;
}
}
///
/// 截图存放路径
///
public string GrabImagePath
{
get
{
return grabImagePath;
}
set
{
grabImagePath = value;
}
}
///
/// 录像存放路径
///
public string KinescopePath
{
get
{
return kinescopePath;
}
set
{
kinescopePath = value;
}
}
///
/// 启动摄像头
///
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);
bool flag = false;
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);
while (true)
{
flag = SendMessage(hWndc, WM_CAP_DRIVER_CONNECT, 0, 0);
if (flag) break;
}
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;
}
///
/// 停止摄像
///
public void Stop()
{
SendMessage(hWndc, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bStat = false;
}
///
/// 抓图
///
public void GrabImage()
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(GrabImagePath);
SendMessage(hWndc, WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
///
/// 录像
///
public void Kinescope()
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(KinescopePath);
SendMessage(hWndc, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
SendMessage(hWndc, WM_CAP_SEQUENCE, 0, 0);
}
///
/// 将摄像头一帧数据存放在剪切板上
///
public void CapturePicture()
{
SendMessage(hWndc, WM_CAP_SET_PREVIEWRATE, 125, 0);
SendMessage(hWndc, WM_CAP_SET_EDIT_COPY, 0, 0);
}
public void StopKinescope()
{
SendMessage(hWndc, WM_CAP_STOP, 0, 0);
}
///
/// 创建一个视频捕捉窗口
///
/// 标识窗口的名字
/// 标识窗口的风格
/// 标识窗口的左上角x点
/// 标识窗口的左上角y点
/// 标识窗口的宽度
/// 标识窗口的高度
/// 标识父窗口句柄
/// 标识窗口ID
///
[DllImport("avicap32.dll")]
static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowsName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
///
/// 向windows系统发送消息
///
/// 窗口句柄
/// 发送的消息
/// 消息参数
/// 消息参数
///
[DllImport("User32.dll")]
static extern bool SendMessage(IntPtr hWhd, int msg, int wParam, int lParam);
}