仅使用默认库的 C# 相机捕获

Lem*_*sPC 4 .net c# camera default capture

我正在尝试为 Win 10 设备开发一个应用程序,该应用程序将在出厂重置的设备上运行(使用 Windows 10 build 15063)。这个应用程序必须能够访问硬件(所以我不能使用 UWP,因为 UWP 在沙盒模式下运行)并且还必须能够使用所有相机设备(使用某种相机选择器)拍照。我的主要问题是我只能使用内置的 .NET 库来安装和启动应用程序,所以我不能使用任何 3'rd 方框架来简化相机捕获(出于安全原因,整个应用程序也必须包含在单个 .exe 文件),而 Windows.media.capture 仅适用于 UWP,这对我来说毫无用处。

谁能知道我可以使用哪个默认库来创建足够简单的应用程序以具有相机捕获功能?

Pet*_*var 6

您可能会使用它WIN32API来实现您正在寻找的东西,但这有点复杂。

capCreateCaptureWindowW从图书馆看看avicap32.dll

尝试修改此代码以满足您的需求(它可能有一些错误,但这是一个好的开始。

public class Camera
{
 [DllImport("kernel32.dll")]
 private static extern void Sleep(int dwMilliseconds);

 [DllImport("kernel32.dll")]
 private static extern int Beep(int dwFreq, int dwDuration);

 [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowW")]
 private static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent
 private const int WS_VISIBLE = 0x10000000;
 private const int WS_CHILD = 0x40000000;

 [DllImport("user32.dll", EntryPoint = "SendMessageW")]
 private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

 [DllImport("user32.dll", EntryPoint = "SendMessageW")]
 private static extern int SendMessageFL(int hwnd, int wMsg, int wParam, string lParam);

 [DllImport("user32.dll", EntryPoint = "SendMessageW")]
 private static extern int SendMessageSD(int hwnd, int wMsg, string wParam, int lParam);

 private const int WM_USER = 0x400;
 private const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
 private const int WM_CAP_START = WM_USER;
 private const int WM_CAP_FILE_SAVEDIBA = (WM_CAP_START + 25);
 private const int WM_CAP_SET_SCALE = WM_USER + 53;
 private const int WM_CAP_SET_PREVIEW = WM_USER + 50;
 private const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
 private const int WM_CAP_FILE_SAVEDIB = WM_USER + 25;
 private const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;

 private string _camtitle;   // a pointer to HDCAM
 private int hWebcam;
 private const int nDevice = 0;
 private const int nFPS = 50;
 private string _filename;   // image.bmp filename



 public int getCAM(string cam_title, int cam_x, int cam_y, int cam_width, int cam_height, IntPtr HWNDparent, int cam_ID)
 {
     _camtitle = cam_title;
     hWebcam = capCreateCaptureWindow(cam_title, WS_VISIBLE + WS_CHILD, cam_x, cam_y, cam_width, cam_height, HWNDparent.ToInt32(), cam_ID);
     return hWebcam;
 }

 public string NewFileNAME()
 {
     DateTime DT = new DateTime();
     DT.Date.ToString();
     return "file-" + DT.Date.ToString();

 }
 public void startCAM()
 {
     SendMessage(hWebcam, WM_CAP_DRIVER_CONNECT, nDevice, 0);
     SendMessage(hWebcam, WM_CAP_SET_SCALE, 1, 0);
     SendMessage(hWebcam, WM_CAP_SET_PREVIEWRATE, nFPS, 0);
     SendMessage(hWebcam, WM_CAP_SET_PREVIEW, 1, 0);
 }

 public void captureCAM(string BMPfilename)
 {
     _filename = BMPfilename;
     SendMessageFL(hWebcam, WM_CAP_FILE_SAVEDIBA, 0, _filename);
 }

 public void stopCAM()
 {
     SendMessageSD(hWebcam, WM_CAP_DRIVER_DISCONNECT, _camtitle, 0);
 }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用Windows 10 API和 从 Windows 窗体/WPF 应用程序调用它们。我相信这个参考会帮助你

更新(有用的参考)

网络摄像头、多线程和 VFW

代码片段 - 在 C# 中捕获网络摄像头图像

如何创建网络摄像头捕获 (VB .NET)

从 CSharp 中的网络摄像头捕获