仅使用默认库的C#Camera Capture

时间:2018-07-06 06:56:42

标签: c# .net camera default capture

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

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

1 个答案:

答案 0 :(得分:3)

您可以使用WIN32API来实现所需的功能,但这有点复杂。

从库capCreateCaptureWindowWavicap32.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);
 }
}

或者您可以使用Windows 10 API并从Windows Form / WPF应用程序中调用它们。我相信reference会为您提供帮助

更新(有用的参考)

Webcamera, Multithreading and VFW

Code Snippets – Capture WebCam Image in C#

How To Create Webcam Capture (VB .NET)

Capture from webcam in CSharp