调用AForge.video.DirectShow免费实现摄像头采集和上下左右镜像功能
By
admin
at 20 天前 • 1人收藏 • 245人看过
这次我们使用开源免费的AForge.video.DirectShow实现摄像头采集和上下左右镜像功能
前面我分享了一个收费的sharpCamera.dll程序集实现此功能, 它是个收费控件, 虽然费用也不高. 但是我们能先用免费的就不用收费的原则 , 下面来探讨下AForge的使用方法.
二楼有写好的AForge封装库下载
和sharpCamera那个类似, AForge也是异步使用picturebox来实现
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio工程2";right=959;bottom=564) mainForm.add( button={cls="button";text="start";left=307;top=527;right=418;bottom=553;db=1;dl=1;z=2}; button2={cls="button";text="stop";left=430;top=527;right=542;bottom=553;db=1;dl=1;dr=1;z=3}; button3={cls="button";text="flip";left=553;top=527;right=669;bottom=553;db=1;dr=1;z=4}; combobox={cls="combobox";left=53;top=527;right=286;bottom=553;db=1;dl=1;edge=1;items={};mode="dropdownlist";z=5}; custom={cls="custom";text="自定义控件";left=0;top=0;right=960;bottom=520;bgcolor=12639424;db=1;dl=1;dr=1;dt=1;z=1} ) /*}}*/ import dotNet import System.Drawing; import System.Windows.Forms; var pictureBox1 = System.Windows.Forms.CreateEmbed("PictureBox",mainForm.custom); pictureBox1.BackColor = System.Drawing.Color.Black; pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; dotNet.reference({ "AForge" = "\res\AForge.dll"; }) var carema = dotNet.load("AForge"); var AForgeCamera = carema.import("AForge.Video"); var videoDevices = AForgeCamera.DirectShow.FilterInfoCollection(AForgeCamera.DirectShow.FilterCategory.VideoInputDevice); //默认取第一个序号的摄像头 var videoDevice = AForgeCamera.DirectShow.VideoCaptureDevice(videoDevices.Item[0].MonikerString); //摄像头包含的可用帧率和分辨率 var videoCapabilitiesEx = videoDevice.VideoCapabilities; for(i=0;videoCapabilitiesEx.LongLength-1;1){ mainForm.combobox.add("分辨率: "++videoCapabilitiesEx.Item[i].FrameSize.Width++"*"++videoCapabilitiesEx.Item[i].FrameSize.Height ++ " 帧率: " ++ videoCapabilitiesEx.Item[i].FrameRate); } if(mainForm.combobox.count){ mainForm.combobox.selIndex = 1; } var flip1 = 0; videoDevice.NewFrame = function(sender, NewFrameEventArgs){ var temp = NewFrameEventArgs.Frame.Clone(); select(flip1) { case 0 { } case 1 { temp.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX); } case 2 { temp.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipXY); } case 3 { temp.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); } else { } } if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } pictureBox1.Image = temp; } mainForm.button.oncommand = function(id,event){ videoDevice.Stop(); if(mainForm.combobox.count){ //设定帧率和分辨率 videoDevice.DesiredFrameRate = videoCapabilitiesEx.Item[mainForm.combobox.selIndex-1].FrameRate; videoDevice.DesiredFrameSize = videoCapabilitiesEx.Item[mainForm.combobox.selIndex-1].FrameSize; videoDevice.DesiredSnapshotSize = videoCapabilitiesEx.Item[mainForm.combobox.selIndex-1].FrameSize; videoDevice.Start(); } } mainForm.button2.oncommand = function(id,event){ videoDevice.Stop(); } mainForm.button3.oncommand = function(id,event){ select(flip1) { case 0 { flip1=1; } case 1 { flip1=2; } case 2 { flip1=3; } case 3 { flip1=0; } else { } } } mainForm.onClose = function(hwnd,message,wParam,lParam){ videoDevice.Stop(); } mainForm.show(); return win.loopMessage();
完整测试工程如下:
3 个回复 | 最后更新于 17 天前
回复#2 @aunox :
就是用了很多中摄像头ocx和dll控件后, 才发现这种方式延迟小. 灵活性高.
很多国外的ocx里面包含了杂七杂八的功能, 导致有些usb摄像头延迟非常大, 或者出现水纹 , 国内的摄像头ocx好很多, 但是收费也不便宜 , 这种.net的控件只专注于采集图像, 然后输出给你, 至于怎么处理是让你自己考虑, 所以少了很多环节 ,
对比我之前所发的所有关于摄像头的 , 目前我测试这种方式貌似最好.
如果你摄像头延迟大, 你用顶楼那个程序测试下, 里面可以选[ 分辨率和帧率 ] 理论上帧率高延迟就小, 如果你摄像头不支持高帧率, 那就没办法了.
备注:
界面里面所示的[ 分辨率和帧率 ] 是从usb设备上通信获得的, 不是随意输入的, 是硬件所支持的硬指数.
登录后方可回帖
简单封装了个AForgeVideo摄像头库:
使用的时候:
完整示例工程和库代码如下: