1.通过nuget安装OpenCvSharp4 ,下面四个都要安装否则会出现错误!
2.运行摄像头代码
windows_loaded中的代码
Thread threadA = new Thread(run_cap);
threadA.Start();
摄像头图像代码
void run_cap()
{
Mat src = new Mat();
FrameSource frame = Cv2.CreateFrameSource_Camera(0);
while (true)
{
frame.NextFrame(src);
System.Drawing.Bitmap bitmap = BitmapConverter.ToBitmap(src);
Action action1 = () =>
{
iCamera.Source = BitmapToBitmapImage(bitmap);
};
this.Dispatcher.BeginInvoke(action1);
}
}
bitmap to image 代码
public System.Windows.Media.Imaging.BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
System.Windows.Media.Imaging.BitmapImage bit3 = new System.Windows.Media.Imaging.BitmapImage();
bit3.BeginInit();
bit3.StreamSource = ms;
bit3.EndInit();
return bit3;
}
xaml文件中显示摄像头内容的Image控件
<Grid Grid.Column="2">
<Image x:Name="iCamera" Margin="0,0,0,0" Width="120" Height="120" Source="Assets/Images/redo.png" VerticalAlignment="Top"/>
</Grid>