仅使用默认库的 C# Camera Capture
C# Camera Capture using only default library
我正在尝试为 Win 10 设备开发一个应用程序,它将 运行 在恢复出厂设置的设备上运行(使用 windows 10 build 15063)。这个应用程序必须能够访问硬件(所以我不能使用 UWP,因为 UWP 运行s 在沙盒模式下)并且还必须能够使用所有相机设备拍照(使用某种相机选择器)。我的主要问题是我只被允许使用内置的 .NET 库来安装和启动应用程序,所以我不能使用任何第 3 方框架来简化相机捕获(出于安全原因,整个应用程序也必须包含在单个 .exe 文件)和 Windows.media.capture 仅适用于对我无用的 UWP。
谁能知道我可以使用哪个默认库来创建足够简单的应用程序以具有相机捕获功能?
您可以使用 WIN32API
来实现您想要的,但这有点复杂。
从图书馆 avicap32.dll
查看 capCreateCaptureWindowW
。
尝试修改此代码以满足您的需要(它可能有一些错误,但这是一个好的开始。)
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#
我正在尝试为 Win 10 设备开发一个应用程序,它将 运行 在恢复出厂设置的设备上运行(使用 windows 10 build 15063)。这个应用程序必须能够访问硬件(所以我不能使用 UWP,因为 UWP 运行s 在沙盒模式下)并且还必须能够使用所有相机设备拍照(使用某种相机选择器)。我的主要问题是我只被允许使用内置的 .NET 库来安装和启动应用程序,所以我不能使用任何第 3 方框架来简化相机捕获(出于安全原因,整个应用程序也必须包含在单个 .exe 文件)和 Windows.media.capture 仅适用于对我无用的 UWP。
谁能知道我可以使用哪个默认库来创建足够简单的应用程序以具有相机捕获功能?
您可以使用 WIN32API
来实现您想要的,但这有点复杂。
从图书馆 avicap32.dll
查看 capCreateCaptureWindowW
。
尝试修改此代码以满足您的需要(它可能有一些错误,但这是一个好的开始。)
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#