WPF实现摄像头镜像翻转
之前的项目需要镜像翻转摄像头视频,使用Aforge.Net来处理视频。
一开始考虑直接从Aforge.Net读取没一帧视频图片,然后复制给WPF的Image控件,这种方法基本很卡,所以放弃了。
考虑到Aforge.net 返回的图片是Bitmap的,所以打算直接在WPF中嵌入Winform的picturebox来实现视频。
【注意】如果在WPF中嵌入Winform窗口是不可透明的。
【Xaml】
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowState="Normal" Background="Black"
WindowStyle="None" BorderThickness="0"
Left="0" Top="0" WindowStartupLocation="Manual"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
>
<wfi:WindowsFormsHost x:Name="video" >
<winform:PictureBox x:Name="pb" SizeMode="StretchImage" BackColor="Black" ForeColor="Black"/>
</wfi:WindowsFormsHost>
</Window>
【c#代码】
#region << Field >>
private VideoCaptureDevice VCD;
private static double VideoResolutionWidth = 1280;
#endregion
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern long GetWindowLong(IntPtr handle, int style);
public const int GWL_STYLE = -16;
public const int GWL_EXSTYLE = -20;
public const long WS_CAPTION = 0x00C00000L;
public const long WS_CAPTION_2 = 0X00C0000L;
private static int VideoClipXPos = 0;
private static int VideoClipWidth = 0;
public static System.Drawing.Bitmap CurrentPhoto = null;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
StopCamera(true);
}
private void StopCamera(bool flag)
{
if (flag)
VCD.Stop();
else
VCD.Start();
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Width = 1080;
this.Height = 1920;
this.grid.Width = 809;
this.grid.Height = 1082;
SetPhotoView();
//this.pb.Width = 3413;
//this.pb.Height = 1920;
// this.video.Margin = new Thickness(-666, 0, 0, 0);
// this.Left = -(3413 - 1080) / 2;
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
VCD = new VideoCaptureDevice(videoDevices[0].MonikerString);
foreach (var item in VCD.VideoCapabilities)
{
if (item.FrameSize.Width == VideoResolutionWidth)
{
VCD.VideoResolution = item;
VCD.NewFrame += new AForge.Video.NewFrameEventHandler(videoSource1_NewFrame);
VCD.VideoSourceError += new AForge.Video.VideoSourceErrorEventHandler(videoSource1_VideoSourceError);
VCD.Start();
break;
}
}
}
///设置窗口无边框
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
long oldstyle = GetWindowLong(windowHandle, GWL_STYLE);
SetWindowLong(windowHandle, GWL_STYLE, (int)(oldstyle & (~(WS_CAPTION | WS_CAPTION_2))));
}
private void SetPhotoView()
{
VideoClipWidth = 536;
VideoClipXPos = (1280 - VideoClipWidth) / 2;
}
void videoSource1_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
bool isGood = true;
System.Drawing.Bitmap img = (System.Drawing.Bitmap)eventArgs.Frame.Clone(new System.Drawing.Rectangle(VideoClipXPos, 0, VideoClipWidth,
eventArgs.Frame.Height),
System.Drawing.Imaging.PixelFormat.Undefined);
this.Dispatcher.BeginInvoke(new Action(() =>
{
try
{
img.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX);
}
catch (Exception e)
{
isGood = false;
}
if (isGood)
{
if (this.pb.Image != null)
{
this.pb.Image.Dispose();
this.pb.Image = null;
}
this.pb.Image = img;
CurrentPhoto = img;
}
else
{
img.Dispose();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}));
}
void videoSource1_VideoSourceError(object sender, AForge.Video.VideoSourceErrorEventArgs eventArgs)
{
}
}
上诉代码是实现特定区域的视频截取,这种方法在1080p,和1920*2 1080 的分辨率下不卡,(测试机器是i5,4G内存的)
为了不卡,肯定会丢帧,但是还是能接受的。
其实最好的办法是将窗体的handle,传给c++来进行视频绘制。 但一般情况下,我觉得这种方法够用了。
WPF实现摄像头镜像翻转的更多相关文章
- 【Android】android镜像翻转
Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样.这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的 ...
- wpf mediakit 摄像头截图
原文:wpf mediakit 摄像头截图 在用VideoCaptureElement的过程中,不知道怎么获得摄像头的截图,纠结了整整一天, 最终在下面的网站上找到了答案,哈哈.(困的都不清醒的大脑, ...
- WPF成长之路------翻转动画
先介绍一下RenderTransform类,该类成员如下: TranslateTransform:能够让某对象的位置发生平移变化. RotateTransform:能够让某对象产生旋转变化,根据中心点 ...
- WPF调用摄像头
添加程序集:WPFMediaKit.dll 更关键代码如下: 界面设计代码如下: <Window x:Class="摄像头调用.MainWindow" xmlns=" ...
- C#开发PACS医学影像处理系统(十七):2D处理之影像旋转和翻转
1.任意角度旋转 在XAML设计器中,设置RotateTransform属性 <InkCanvas x:Name="ToolInkCanvas" UseCustomCurso ...
- Android平台摄像头/屏幕/外部数据采集及RTMP推送接口设计描述
好多开发者提到,为什么大牛直播SDK的Android平台RTMP推送接口怎么这么多?不像一些开源或者商业RTMP推送一样,就几个接口,简单明了. 不解释,以Android平台RTMP推送模块常用接口, ...
- 二叉树的镜像(Python实现)
题目 给定一棵二叉树,要求输出其左右翻转后二叉树的中序遍历. 例: 翻转前: 翻转后: 1 | 1 / \ | / \ 2 3 | 3 2 / \ | / \ 4 5 | 5 4 解析 两个步骤: 镜 ...
- 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释
本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含图像平移.图形 ...
- Android相机开发那些坑
版权声明:本文由王梓原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/168 来源:腾云阁 https://www.qclou ...
随机推荐
- Keil MDK入门---从新建一个工程开始
熟悉Keil C51的朋友对于Keil MDK上手应该比较容易,毕竟界面是很像的.但ARM内核毕竟不同于51内核,因此无论在设置上还是在编程思想上,都需要下番功夫研究的.本文以MDK V4.03为例, ...
- web.py simpletodo 例子
一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...
- Linux下安装软件心得
1 软件安装方法: 源代码编译安装:tar.gz等压缩格式,需要经过手动编译,./configure,make ,make install ,然后进行配置操作 二进制安装:tar.gz等压缩格式,解压 ...
- HDOJ 2071 Max Num
Problem Description There are some students in a class, Can you help teacher find the highest studen ...
- 数学概念——F 概率(经典问题)birthday paradox
F - 概率(经典问题) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- 差别不在英语水平,而在汉语水平If you do not leave me, we will die together.
为什么高考语文要提高到180分,英语降到100,差别不在英语水平,而在汉语水平.看下面例句的译法: If you do not leave me, we will die together. 你如果不 ...
- KMP算法,Boyer-Moore算法
http://www-igm.univ-mlv.fr/~lecroq/string/node19.html 首先看普通的串的模式匹配算法开始讲起,才能更深入的了解KMP算法及其优点. 1. 普通的串的 ...
- 简单的闭包运算(Closure)演示程序
/* * 该程序用于计算某个产生式的闭包 * RexfieldVon * 2013年8月9日16:01:38 */ #include <stdio.h> #include <stdl ...
- 作业.把c语言输出的基础差不多都概括了!
// (1)1英里=1.60931公里,从键盘上输入英里数输出公里数 #include "stdio.h" #define PI 3.1415926 #include " ...