public class CameraAPI
{
public bool IsAvailable { get; set; }
[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, long lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, long lParam);
[System.Runtime.InteropServices.DllImport("user32")]
public static extern bool DestroyWindow(IntPtr hWnd);
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_PREVIEWFORMAT = WM_USER + 45;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
public const int WM_CAP_EDIT_COPY = (WM_CAP_START + 30);
private IntPtr hWnd;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
public CameraAPI(IntPtr handel, int width, int height)
{
mControlPtr = handel; //handle of video dom
mWidth = width; //video width
mHeight = height; //video height
}
public void StartPreviewWebcam()
{
if (hWnd != null)
DestroyWindow(hWnd);
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 0);
hWnd = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, 0, 0))
{
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 100, 0);
SendMessage(hWnd, WM_CAP_SET_PREVIEW, true, 0);
IsAvailable = true;
}
else
{
IsAvailable = false;
}
}
public void CloseWebcam()
{
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
IsAvailable = false;
}
public void SavePictureByPath(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWnd, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());
}
public byte[] TakePicture()
{
byte[] imgByteArray = null;
var path = Application.StartupPath + @"\Image.png";
try
{
DeleteExist(path);
SavePictureByPath(path);
if (File.Exists(path))
{
using (Image img = Image.FromFile(path))
{
imgByteArray = ImageToByteArray(img);
}
}
}
catch (Exception exp)
{
var a = 1;
}
finally
{
DeleteExist(path);
}
return imgByteArray;
}
public void DeleteExist(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
}
using AForge.Video.DirectShow;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ConnectToCamera();
}
void ConnectToCamera()
{
var pictureBox = new PictureBox()
{
Width = 640,
Height = 480
};
// add pictureBox to the form
this.Controls.Add(pictureBox);
// connects to the camera and display video on the form (using AForge)
var items = new FilterInfoCollection(FilterCategory.VideoInputDevice);
var videoSource = new VideoCaptureDevice(items[0].MonikerString);
videoSource.NewFrame += (s, e) =>
{
pictureBox.Image = (Bitmap)e.Frame.Clone();
};
videoSource.Start();
}
}
}
3条答案
按热度按时间dzhpxtsq1#
有几种方法可以与摄像头进行通信。
我用这个代码和摄像头联系。
ukqbszuj2#
你的问题对这个网站来说有点太广泛了。答案是“是的,这是可能的”,但这并不像用C#写几行代码那么简单。您将需要某种工具包/库/框架,允许您连接到相机并显示视频。他们有很多,每一个都有优点和缺点,有些简单,有些复杂。
如果您使用的是Visual Studio,请在解决方案资源管理器中右键单击您的项目名称并选择“管理Nuget包”。在打开的窗口中选择“浏览”,然后在搜索框中键入“相机”。您将看到许多提供使用相机支持的软件包。
j2cgzkjk3#
我努力寻找一个令人满意的答案来解决这个问题,最终找到了AForge。视频: