基本信息
源码名称:C# 开启、关闭摄像头视频(window API)
源码大小:0.03M
文件格式:.7z
开发语言:C#
更新时间:2019-06-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
API摄像头视频c#源码

using System;
using System.Runtime.InteropServices;
public class VideoAPI
{
	[DllImport("avicap32.dll")]
	public static extern IntPtr capCreateCaptureWindowA(byte[] IpszWindowName,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[] ipszVer,int cbVer);
	[DllImport("User32.dll")]
	public static extern bool SendMessage(IntPtr hWnd,int wMsg,bool wParam,int iparam);
    [DllImport("User32.dll")]
	public static extern bool SendMessage(IntPtr hWnd,int wMsg,short wParam,long iparam);



    #region 消息常量(向窗口发送消息的指令)

    //消息常量 -------------------------------------------- 
    public const int WM_START = 0x400;    //此并非摄像头消息0x400表示的就是1024 
    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_GET_CAPSTREAMPTR = WM_START   1;
    public const int WM_CAP_SET_CALLBACK_ERROR = WM_START   2;//设置收回错误
    public const int WM_CAP_SET_CALLBACK_STATUS = WM_START   3;//设置收回状态
    public const int WM_CAP_SET_CALLBACK_YIELD = WM_START   4;//设置收回出产
    public const int WM_CAP_SET_CALLBACK_FRAME = WM_START   5;//设置收回结构
    public const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_START   6;//设置收回视频流
    public const int WM_CAP_SET_CALLBACK_WAVESTREAM = WM_START   7;//设置收回视频波流
    public const int WM_CAP_GET_USER_DATA = WM_START   8;//获得使用者数据
    public const int WM_CAP_SET_USER_DATA = WM_START   9;//设置使用者数据
    public const int WM_CAP_DRIVER_CONNECT = WM_START   10;//驱动程序连接
    public const int WM_CAP_DRIVER_DISCONNECT = WM_START   11;//断开启动程序连接
    public const int WM_CAP_DRIVER_GET_NAME = WM_START   12;//获得驱动程序名字
    public const int WM_CAP_DRIVER_GET_VERSION = WM_START   13;//获得驱动程序版本
    public const int WM_CAP_DRIVER_GET_CAPS = WM_START   14;//获得驱动程序帽子
    public const int WM_CAP_FILE_SET_CAPTURE_FILE = WM_START   20;//设置捕获文件
    public const int WM_CAP_FILE_GET_CAPTURE_FILE = WM_START   21;//获得捕获文件
    public const int WM_CAP_FILE_ALLOCATE = WM_START   22;//分派文件
    public const int WM_CAP_FILE_SAVEAS = WM_START   23;//另存文件为
    public const int WM_CAP_FILE_SET_INFOCHUNK = WM_START   24;//设置开始文件
    public const int WM_CAP_FILE_SAVEDIB = WM_START   25;//保存文件
    public const int WM_CAP_EDIT_COPY = WM_START   30;//编辑复制
    public const int WM_CAP_SET_AUDIOFORMAT = WM_START   35;//设置音频格式
    public const int WM_CAP_GET_AUDIOFORMAT = WM_START   36;//捕获音频格式
    public const int WM_CAP_DLG_VIDEOFORMAT = WM_START   41;//1065 打开视频格式设置对话框
    public const int WM_CAP_DLG_VIDEOSOURCE = WM_START   42;//1066 打开属性设置对话框,设置对比度亮度等
    public const int WM_CAP_DLG_VIDEODISPLAY = WM_START   43;//1067 打开视频显示
    public const int WM_CAP_GET_VIDEOFORMAT = WM_START   44;//1068 获得视频格式
    public const int WM_CAP_SET_VIDEOFORMAT = WM_START   45;//1069 设置视频格式
    public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_START   46;//1070 打开压缩设置对话框
    public const int WM_CAP_SET_PREVIEW = WM_START   50;//设置预览
    public const int WM_CAP_SET_OVERLAY = WM_START   51;//设置覆盖
    public const int WM_CAP_SET_PREVIEWRATE = WM_START   52;//设置预览比例
    public const int WM_CAP_SET_SCALE = WM_START   53;//设置刻度
    public const int WM_CAP_GET_STATUS = WM_START   54;//获得状态
    public const int WM_CAP_SET_SCROLL = WM_START   55;//设置卷
    public const int WM_CAP_GRAB_FRAME = WM_START   60;//逮捕结构
    public const int WM_CAP_GRAB_FRAME_NOSTOP = WM_START   61;//停止逮捕结构
    public const int WM_CAP_SEQUENCE = WM_START   62;//次序
    public const int WM_CAP_SEQUENCE_NOFILE = WM_START   63;//使用WM_CAP_SEUENCE_NOFILE消息(capCaptureSequenceNoFile宏),可以不向磁盘文件写入数据。该消息仅在配合回调函数时有用,它允许你的应用程序直接使用音视频数据。
    public const int WM_CAP_SET_SEQUENCE_SETUP = WM_START   64;//设置安装次序
    public const int WM_CAP_GET_SEQUENCE_SETUP = WM_START   65;//获得安装次序
    public const int WM_CAP_SET_MCI_DEVICE = WM_START   66;//设置媒体控制接口
    public const int WM_CAP_GET_MCI_DEVICE = WM_START   67;//获得媒体控制接口
    public const int WM_CAP_STOP = WM_START   68;//停止
    public const int WM_CAP_ABORT = WM_START   69;//异常中断
    public const int WM_CAP_SINGLE_FRAME_OPEN = WM_START   70;//打开单一的结构
    public const int WM_CAP_SINGLE_FRAME_CLOSE = WM_START   71;//关闭单一的结构
    public const int WM_CAP_SINGLE_FRAME = WM_START   72;//单一的结构
    public const int WM_CAP_PAL_OPEN = WM_START   80;//打开视频
    public const int WM_CAP_PAL_SAVE = WM_START   81;//保存视频
    public const int WM_CAP_PAL_PASTE = WM_START   82;//粘贴视频
    public const int WM_CAP_PAL_AUTOCREATE = WM_START   83; //自动创造
    public const int WM_CAP_PAL_MANUALCREATE = WM_START   84;//手动创造
    public const int WM_CAP_SET_CALLBACK_CAPCONTROL = WM_START   85;// 设置收回的错误

    #endregion 消息常量
}
public class Video
{
	private IntPtr lwndc;//保存无符号句柄
	private IntPtr mControlPtr;//保存管理指示器
	private int mWidth;
	private int mHeight;
    /// <summary>
    /// 初始化Video
    /// </summary>
    /// <param name="handle">对象的handle</param>
    /// <param name="width">对象的高</param>
    /// <param name="height">对象的宽</param>
	public Video(IntPtr handle,int width,int height)
	{
		mControlPtr =handle;
		mWidth =width;
		mHeight = height;
	}
    /// <summary>
    /// 开启
    /// </summary>
	public void StartVideo()
	{
		byte[] ipszName =new byte[100];
		byte[] ipszVer = new byte[100];

		VideoAPI.capGetDriverDescriptionA(0,ipszName,100,ipszVer,100);

		this.lwndc=VideoAPI.capCreateCaptureWindowA(ipszName,VideoAPI.WS_CHILD|VideoAPI.WS_VISIBLE,0,0,mWidth,mHeight,mControlPtr,0);
        if (VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
		{
            VideoAPI.SendMessage(this.lwndc, VideoAPI.WM_CAP_SET_OVERLAY, 1, 0);//启用叠加 注:据说启用此项可以加快渲染速度 
            VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SET_PREVIEWRATE, 30, 0);//设置在PREVIEW模式下设定视频窗口的刷新率 设置每30毫秒显示一帧,即显示帧速为每秒29帧
            VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SET_PREVIEW, 1, 0);//设置显示图像启动预览模式 PREVIEW

		}else
		{
			VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_SET_PREVIEW,true,0);
		}
	}
	/// <summary>
	/// 关闭视频
	/// </summary>
	public void CloseVideo()
	{
		VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_DRIVER_DISCONNECT,0,0);
	}
    /// <summary>
    /// 截图
    /// </summary>
    /// <param name="path">保存图片的路径</param>
	public void GrabImage(String path)
	{
        IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
        VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_FILE_SAVEDIB,0,hBmp.ToInt64());
	
    }
    /// <summary>
    /// 开始录像
    /// </summary>
    /// <param name="PATH">保存文件的路径</param>
    public void StartKinescope(String PATH) 
    {
        IntPtr hBmp = Marshal.StringToHGlobalAnsi(PATH);
        VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_FILE_SET_CAPTURE_FILE,0, hBmp.ToInt64());
        VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SEQUENCE, 0, 0);
	
    }
    /// <summary>
    /// 停止录像
    /// </summary>
    public void Stop() 
    {
        VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_STOP,0,0);
    }
    /// <summary>
    /// 视频压缩
    /// </summary>
    public void setVideoCompression()
    {
        VideoAPI.SendMessage(this.lwndc, VideoAPI.WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0);
    }
}