C# 读取摄像头老是显示黑色
public bool IsConnected { get; set; }
//保存无符号句柄
private IntPtr lwndC;
//保存管理指示器
private IntPtr mControlPtr;
//摄像头显示宽度
private int mWidth;
//摄像头显示高度
private int mHeight;
public Camera(IntPtr handle, int width, int height)
{
//显示视频控件的句柄
mControlPtr = handle;
//视频宽度
mWidth = width;
//视频高度
mHeight = height;
}
/// <summary>
/// 打开视频设备
/// </summary>
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
CameraWindowsAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = CameraWindowsAPI.capCreateCaptureWindowA(lpszName, CameraWindowsAPI.WS_CHILD | CameraWindowsAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{
IsConnected = true;
CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_SET_PREVIEWRATE, 1, 0);
CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_SET_PREVIEW, true, 0);
CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
}
else
{
IsConnected = false;
}
}
/// <summary>
/// 关闭视频设备
/// </summary>
public void CloseWebcam()
{
CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}
/// <summary>
/// 拍照
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
CameraWindowsAPI.SendMessage(lwndC, CameraWindowsAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
如图
唉。。看来没有人回答上来。。。这个问题是我的机子安装的一个程序的问题。
麻烦您说清楚点,到底是什么问题,我现在遇到了同样的问题,你说的程序是指什么程序,急求,谢谢
我把机子上面的一个编码器卸载了就没有问题了。具体的我忘记了。sorry.