之前的项目需要镜像翻转摄像头视频,使用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实现摄像头镜像翻转的更多相关文章

  1. 【Android】android镜像翻转

    Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样.这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的 ...

  2. wpf mediakit 摄像头截图

    原文:wpf mediakit 摄像头截图 在用VideoCaptureElement的过程中,不知道怎么获得摄像头的截图,纠结了整整一天, 最终在下面的网站上找到了答案,哈哈.(困的都不清醒的大脑, ...

  3. WPF成长之路------翻转动画

    先介绍一下RenderTransform类,该类成员如下: TranslateTransform:能够让某对象的位置发生平移变化. RotateTransform:能够让某对象产生旋转变化,根据中心点 ...

  4. WPF调用摄像头

    添加程序集:WPFMediaKit.dll 更关键代码如下: 界面设计代码如下: <Window x:Class="摄像头调用.MainWindow" xmlns=" ...

  5. C#开发PACS医学影像处理系统(十七):2D处理之影像旋转和翻转

    1.任意角度旋转 在XAML设计器中,设置RotateTransform属性 <InkCanvas x:Name="ToolInkCanvas" UseCustomCurso ...

  6. Android平台摄像头/屏幕/外部数据采集及RTMP推送接口设计描述

    好多开发者提到,为什么大牛直播SDK的Android平台RTMP推送接口怎么这么多?不像一些开源或者商业RTMP推送一样,就几个接口,简单明了. 不解释,以Android平台RTMP推送模块常用接口, ...

  7. 二叉树的镜像(Python实现)

    题目 给定一棵二叉树,要求输出其左右翻转后二叉树的中序遍历. 例: 翻转前: 翻转后: 1 | 1 / \ | / \ 2 3 | 3 2 / \ | / \ 4 5 | 5 4 解析 两个步骤: 镜 ...

  8. 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含图像平移.图形 ...

  9. Android相机开发那些坑

    版权声明:本文由王梓原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/168 来源:腾云阁 https://www.qclou ...

随机推荐

  1. 设计模式之观察者模式(Observer Pattern)

    一.什么是观察者模式? 把现实世界中的报纸与订阅者的关系抽象出来就是观察者模式,一种报纸对应多个订阅者,订阅者可以随时解除订阅,未订阅的读者也可以随时开始订阅.一旦有新报纸发布,所有的订阅者都会收到新 ...

  2. MySql的rpm安装

    MySQL 5.5(rpm格式)在Linux 上安装 Linux系统上安装MySQL 5.5prm 1.准备工作 从MySQL官网上分别下载mysql服务器端于客户端包. 下载步骤:Downloads ...

  3. 修改css

    .content{ height: 100%; } .con{ border: 1px solid #eeeeee; display: inline-block; width:86.8%; ##修改这 ...

  4. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板

    <pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...

  5. Git基本操作(Windows下)

    在开始使用Git之前,我觉得是很有必要了解下Git与其他版本控制系统的差异与文件在Git中的三种状态.可以到下面这个网站看下:Git详解之一 Git起步,了解之后,可以对Git的基本操作有一个更清晰的 ...

  6. 实战weblogic集群之创建节点和集群

    一.启动weblogic,访问控制台 weblogic的domain创建完成后,接下来就可以启动它,步骤如下: $ cd /app/sinova/domains/base_domain/bin $ . ...

  7. Android 子线程请求ASP.NET后台

    首先定义布局文件,及点击事件 public class MainActivity extends Activity { private final int MSG_HELLO = 0; private ...

  8. 原生JavaScript拖动div兼容多种浏览器

    说句题外话,虽然博客园嵌入式氛围不行,Web前端氛围还是很好的.我又从 chinaunix 回来了. <html> <head> <script type="t ...

  9. osg

    智能指针使用: osg::Geode* geode=new osg::Geode;//新建Geode指针 osg::ref_ptr<osg::Geode>geodePtr=geode;// ...

  10. 算法对比:Prim算法与Dijskra算法

    在图论中,求MST的Prim算法和求最短路的Dijskra算法非常像.可是我一直都对这两个算法处于要懂不懂的状态,现在,就来总结一下这两个算法. 最小生成树(MST)—Prim算法: 算法步骤: •将 ...