通过Aforge的免费开源库可以方便的实现摄像头的操作功能 , 然后利用gdi对图片的各种格式化, 就可以灵活的实现摄像头的各种骚操作.
得益于aardio对C#功能的大力改进, 现在可以非常方便的对c#程序集进行各种调用, 这就意味着c#创建的各种dll都可以被aardio所用.
下面我对AForge进行了简单的封装, 让我们在aardio中非常简单的使用摄像头操作.
当然, 别的帖子里我也提供了很多种办法来实现对摄像头的采集和镜像功能 , 这篇文章, 主要是对C#程序集调用的学习.
本文首发:https://www.htmlayout.cn/t/21369https://www.htmlayout.cn/t/21369
欢迎大家来踩, 程序打包文件也在那里.
AForgeVideo.aardio 库代码:
//AForge摄像头库
import dotNet
import System.Drawing;
import System.Windows.Forms;
class AForgeVideo{
ctor( winform ){
this.pictureBox = ..System.Windows.Forms.CreateEmbed("PictureBox",winform);
this.pictureBox.BackColor = ..System.Drawing.Color.Black;
this.pictureBox.SizeMode = ..System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.carema = ..dotNet.load("\res\AForge.dll");
this.AForgeCamera = this.carema.import("AForge.Video");
this.flip = 0;
this.videoDevice = null;
..table.gc(this,"stop");
};
getNameList = function(){
var videoDevices = this.AForgeCamera.DirectShow.FilterInfoCollection(this.AForgeCamera.DirectShow.FilterCategory.VideoInputDevice);
var retTab = {};
if(videoDevices.Count>0){
for(i=0;videoDevices.Count-1;1){
//压入摄像头设备号名称列表
..table.push(retTab , videoDevices.Item[i].MonikerString);
}
return retTab;
}
return null;
}
getVideoCapabilitiesList = function(deviceName){
if(deviceName!=null){
var videoDevice = this.AForgeCamera.DirectShow.VideoCaptureDevice(deviceName);
var videoCapabilitiesEx = videoDevice.VideoCapabilities;
var retTab = {};
if(videoCapabilitiesEx.LongLength>0){
for(i=0;videoCapabilitiesEx.LongLength-1;1){
..table.push(retTab,{
["Width"] = videoCapabilitiesEx.Item[i].FrameSize.Width,
["Height"] = videoCapabilitiesEx.Item[i].FrameSize.Height,
["FrameRate"] = videoCapabilitiesEx.Item[i].FrameRate,
});
}
return retTab;
}
return null;
}
return null;
}
start = function(DeviceIndex=1,index=1){
if(this.videoDevice){
this.videoDevice.Stop();
}
var namelist = this.getNameList();
if(DeviceIndex <= #namelist){
name = namelist[DeviceIndex];
}else {
//超出摄像头数量,不开启
return false;
}
//设定帧率和分辨率
var VideoCapabilities = this.getVideoCapabilitiesList(name);
this.videoDevice = this.AForgeCamera.DirectShow.VideoCaptureDevice(name);
if(VideoCapabilities!=null){
if(index>#VideoCapabilities){
index = 1;
}
this.videoDevice.DesiredFrameRate = VideoCapabilities[index].FrameRate;
this.videoDevice.DesiredFrameSize = ..System.Drawing.Size(VideoCapabilities[index].Width,VideoCapabilities[index].Height);
this.videoDevice.DesiredSnapshotSize = ..System.Drawing.Size(VideoCapabilities[index].Width,VideoCapabilities[index].Height);
}
this.videoDevice.NewFrame = function(sender, NewFrameEventArgs){
var temp = NewFrameEventArgs.Frame.Clone();
select(this.flip) {
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 (this.pictureBox.Image != null)
{
this.pictureBox.Image.Dispose();
}
this.pictureBox.Image = temp;
}
this.videoDevice.Start();
return true;
}
stop = function(){
if(this.videoDevice){
this.videoDevice.Stop();
this.videoDevice = null;
}
}
flipLoop = function(){
select(this.flip) {
case 0 {
this.flip=1;
}
case 1 {
this.flip=2;
}
case 2 {
this.flip=3;
}
case 3 {
this.flip=0;
}
else {
}
}
}
}
//库代码由popdes发布
/**intellisense()
AForgeVideo = AForge摄像头库
AForgeVideo(.(winform) = 创建 摄像头 界面
AForgeVideo() = !AVideo.
end intellisense**/
/**intellisense(!AVideo)
getNameList() = 获取摄像头设备硬件DeviceName名称列表
getVideoCapabilitiesList(.(DeviceName) = 获取指定设备的 分辨率和帧率 列表,如果不存在那么返回 null
start(.(DeviceIndex,CapabilitiesIndex) = 开启摄像头\n DeviceIndex:设备序号 , CapabilitiesIndex:分辨率和帧率表序号
stop() = 关闭摄像头
flipLoop() = 循环镜像图像,调用一次改变一次镜像方向
end intellisense**/
使用的时候非常简单, 如果你不需要设置其他的东西, 那么调用代码如下
下面是 打开两个摄像头并分别镜像的功能代码示例:
import win.ui;
/*DSG{{*/
mainForm = win.form(text="AForge 摄像头调用-aardio示例";right=1209;bottom=556)
mainForm.add(
button={cls="button";text="start";left=16;top=519;right=127;bottom=545;db=1;dl=1;z=2};
button2={cls="button";text="flip";left=142;top=519;right=254;bottom=545;db=1;dl=1;z=3};
button3={cls="button";text="stop";left=265;top=519;right=381;bottom=545;db=1;dl=1;z=4};
button4={cls="button";text="start";left=833;top=518;right=944;bottom=544;db=1;dr=1;z=6};
button5={cls="button";text="flip";left=959;top=518;right=1071;bottom=544;db=1;dr=1;z=7};
button6={cls="button";text="stop";left=1082;top=518;right=1198;bottom=544;db=1;dr=1;z=8};
custom={cls="custom";text="自定义控件";left=0;top=0;right=601;bottom=496;bgcolor=12639424;db=1;dl=1;dr=0.5;dt=1;z=1};
custom2={cls="custom";text="自定义控件";left=609;top=0;right=1210;bottom=496;bgcolor=12639424;db=1;dl=0.5;dr=1;dt=1;z=5}
)
/*}}*/
import AForgeVideo;
var video = AForgeVideo(mainForm.custom);
mainForm.button.oncommand = function(id,event){
//默认开启序号1摄像头,分辨率为序号1的分辨率和帧率
video.start();
}
mainForm.button2.oncommand = function(id,event){
//摄像头1镜像
video.flipLoop();
}
mainForm.button3.oncommand = function(id,event){
//摄像头1停止
video.stop();
}
//定义2号摄像头
var video2 = AForgeVideo(mainForm.custom2);
mainForm.button4.oncommand = function(id,event){
//开启序号2摄像头,分辨率为序号1的分辨率和帧率
video2.start(2);
}
mainForm.button5.oncommand = function(id,event){
//摄像头2镜像
video2.flipLoop();
}
mainForm.button6.oncommand = function(id,event){
//摄像头2停止
video2.stop();
}
mainForm.show();
return win.loopMessage();