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 ...
随机推荐
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(六)
1. 正则表达式特殊符号.[:alnum:]:代表英文大小写字符及数字:[:alpha:]:代表英文大小写字符:[:blank:]:代表空格键与[Tab]键:[:cntrl:]:代表键盘上的控制键,即 ...
- 「Poetize3」导弹防御塔
描述 Description Freda控制着N座可以发射导弹的防御塔.每座塔都有足够数量的导弹,但是每座塔每次只能发射一枚.在发射导弹时,导弹需要T1秒才能从防御塔中射出,而在发射导弹后,发射这枚导 ...
- 线程调用UpdateData函数出错
在尝试线程更新界面时,在线程中调用UpdateData(FALSE)后出现如下错误: 原因: MFC对象不支持多线程操作,不能供多个线程进程使用.子线程调用pDlg-> UpdateData(F ...
- Android数据加密解密
最近项目在维护过程中,估计这一周都会没有什么事情做了.于是开始打量自己做完的这个项目,项目在展示方面乏善可陈,然后仔细的想了想,这个项目的亮点无非就在数据加密和解密这一块了.因为是银行的项目,所以对数 ...
- 从字符串总分离文件路径、命名、扩展名,Substring(),LastIndexOf()的使用;替换某一类字符串,Replace()的用法
一:从字符串总分离文件路径.命名.扩展名,上图 二:代码 using System; using System.Collections.Generic; using System.ComponentM ...
- Java---实力弹弹球,弹弹弹
直接上代码了. 微调按钮加画布画几个圆,再实现监听... package cn.hncu.threadDemo.thread2; import java.awt.Canvas; import java ...
- C++ 路径中\\与/
windows默认使用\\ linux默认使用/ 可以都用/
- java中字节流和字符流的区别
流分类: 1.Java的字节流 InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先.2.Java的字符流 Reader是所有读取字符串输入流的祖先,而 ...
- java项目使用Echarts 做柱状堆叠图,包含点击事件
基础知识请自行百度查看,以下直接贴出实现代码: <%@ page pageEncoding="UTF-8"%><!DOCTYPE html><html ...
- 如何修改WAMP中mysql数据库账号和密码
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...