c#中为什么调用摄像头后是一片漆黑?

qq_30317945 2016-05-05 04:21:07
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing.Design;

namespace health
{
public class VideoAPI //视频API类
{
// 视频API调用
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, 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[] lpszVer, int cbVer);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

// 常量
public const int WM_USER = 0x400;
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_DRIVER_CONNECT = WM_USER + 10;
public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5;
public const int WM_CAP_SET_PREVIEW = WM_USER + 50;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
}

public class cVideo //视频类
{
private IntPtr lwndC; //保存无符号句柄
private IntPtr mControlPtr; //保存管理指示器
private int mWidth;
private int mHeight;

public cVideo(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];

bool res = VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, 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(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);
}
}
/// <summary>
/// 关闭视频设备
/// </summary>
public void CloseWebcam()
{
VideoAPI.SendMessage(lwndC, VideoAPI.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);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
}
}

这是调用摄像头代码,但调试时,摄像头一片漆黑


...全文
882 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
上帝的亲哥哥 2019-07-17
  • 打赏
  • 举报
回复
这程序写的够麻烦的,有一堆现成,比这个好用的方式!!!
试一下我用AForge写的~~

http://donetsoft.cn/download/camerTest.rar
gemingpei 2019-07-16
  • 打赏
  • 举报
回复
bool Connect = SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
if (!Connect)
{
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
}
上面的代码试一下,直到连上为止,拿走不谢
XBodhi. 2019-07-16
  • 打赏
  • 举报
回复
推荐你用 vlc 把。
gemingpei 2019-07-16
  • 打赏
  • 举报
回复
摄像头没有关闭吧,试了下也是不行
stevenjin 2018-01-14
  • 打赏
  • 举报
回复
在不同的设备上运行试下
泡泡龙 2018-01-13
  • 打赏
  • 举报
回复
vfw不是很可靠,尽量用厂商驱动,不要用通用驱动
邻家大哥呀 2018-01-12
  • 打赏
  • 举报
回复
可能是因为摄像头设置的位置的原因,看下位置
海宏AA 2018-01-12
  • 打赏
  • 举报
回复
我也遇到这个问题,后来找了个网上的开源项目:https://www.codeproject.com/Articles/671407/Camera_Net-Library
by_封爱 版主 2016-05-06
  • 打赏
  • 举报
回复
换第三方组件 aforget.net 不知道是不是叫这个名字..我有代码 可以call我
qq_30317945 2016-05-05
  • 打赏
  • 举报
回复
引用 3 楼 qq_34896667 的回复:
多次启用 直至 有图像 不过只能暂时解决


我这个调用一次就灰色,无法再调用了
qq_34896667 2016-05-05
  • 打赏
  • 举报
回复
多次启用 直至 有图像 不过只能暂时解决
qq_30317945 2016-05-05
  • 打赏
  • 举报
回复
赵4老师 2016-05-05
  • 打赏
  • 举报
回复
设备管理器中启用摄像头?

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧