调用c#的SharpCamera.dll摄像头程序集,实现摄像头的上下左右镜像功能
By
admin
at 2022-07-21 • 0人收藏 • 873人看过
摄像头我一般是用的ocx或者dll ,之前从来没想过这么干, 现在aardio调用c#是如此简单, 那么就可以一试了.
提醒:
现在有免费方案了, 用AForge.video也可以实现同样的功能, 具体看https://www.chengxu.xyz/t/21369
我们这里采用深果智能提供的SharpCamera来实现
官方网址: http://sharpcamera.zzsgzn.com/
它的演示程序里是c#采用异步更新picturebox的办法来实现, 所以,我们在aardio里也给c#窗体添加个picturebox , 然后利用异步回调更新图片.
这里需要注意:
这个控件是收费的, 在key里写入Test会进入试用模式, 试用模式有10分钟试用限制, 时间到后会自动关闭摄像头并输出版权蒙版 , 然后你再次打开就再次计时开始 , 有需要的可以去官方购买.
import win.ui; /*DSG{{*/ mainForm = win.form(text="aardio工程4";right=1206;bottom=515) mainForm.add( button={cls="button";text="open_1";left=114;top=466;right=258;bottom=507;db=1;dl=1;z=3}; button2={cls="button";text="Flip";left=1053;top=464;right=1197;bottom=505;db=1;dr=1;z=5}; button3={cls="button";text="Flip";left=268;top=466;right=412;bottom=507;db=1;dl=1;z=7}; button4={cls="button";text="open_2";left=901;top=466;right=1045;bottom=507;db=1;dr=1;z=8}; custom={cls="custom";text="自定义控件";left=0;top=0;right=600;bottom=463;bgcolor=15780518;db=1;dl=1;dr=0.5;dt=1;z=1}; custom2={cls="custom";text="自定义控件";left=607;top=0;right=1207;bottom=463;bgcolor=15780518;db=1;dl=0.5;dr=1;dt=1;z=2}; edit={cls="edit";text="0";left=21;top=474;right=99;bottom=502;db=1;dl=1;edge=1;z=4}; edit2={cls="edit";text="1";left=807;top=470;right=885;bottom=498;db=1;dr=1;edge=1;z=6} ) /*}}*/ import console console.open() import fsys; if(!io.exist(fsys.appdata("\popdes\SGSupport.dll"))){ string.save(fsys.appdata("\popdes\SGSupport.dll"),$"\res\SGSupport.dll"); } if(!io.exist(fsys.appdata("\popdes\SharpCamera.dll"))){ string.save(fsys.appdata("\popdes\SharpCamera.dll"),$"\res\SharpCamera.dll"); } 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; var pictureBox2 = System.Windows.Forms.CreateEmbed("PictureBox",mainForm.custom2); pictureBox2.BackColor = System.Drawing.Color.Black; pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; var carema = dotNet.load(fsys.appdata("\PLC\SharpCamera.dll")); var SharpCamera = carema.import("SharpCamera"); SharpCamera.KeyMgr.SetKey("Test"); var cameraMgr = SharpCamera.CameraMgr(); var NameList = cameraMgr.GetCameraNameList(); for(i=0;NameList.Count-1;1){ console.dump(NameList.Item[i]) } var curCamera,curCamera2; var flip1,flip2 = 0,0; mainForm.button.oncommand = function(id,event){ try{ curCamera = cameraMgr.ChooseCamera((tonumber(mainForm.edit.text))); curCamera.OnVideoFrameCaptrue = function(img){ var temp = img.Clone(System.Drawing.Rectangle(0, 0, img.Width, img.Height), img.PixelFormat); 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; } curCamera.Open(); } catch(e){ mainForm.msgboxErr("出现错误,打开失败!") } } 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.button4.oncommand = function(id,event){ try{ curCamera2 = cameraMgr.ChooseCamera((tonumber(mainForm.edit2.text))); curCamera2.OnVideoFrameCaptrue = function(img){ var temp = img.Clone(System.Drawing.Rectangle(0, 0, img.Width, img.Height), img.PixelFormat); select(flip2) { 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 (pictureBox2.Image != null) { pictureBox2.Image.Dispose(); } pictureBox2.Image = temp; } curCamera2.Open(); } catch(e){ mainForm.msgboxErr("出现错误,打开失败!") } } mainForm.button2.oncommand = function(id,event){ select(flip2) { case 0 { flip2=1; } case 1 { flip2=2; } case 2 { flip2=3; } case 3 { flip2=0; } else { } } } mainForm.show(); return win.loopMessage();
完整测试工程:
登录后方可回帖