Godot+C#+OpenCV實(shí)現(xiàn)調(diào)用本機(jī)攝像頭
? ? ? ?在此記錄一下使用godot調(diào)用本地?cái)z像頭的實(shí)現(xiàn)方法:
1.使用Godot(Godot_v4.0.2-stable_mono_win64.exe)創(chuàng)建一個(gè)項(xiàng)目,
2.添加一個(gè)Sprite2D節(jié)點(diǎn)顯示畫(huà)面用
3.生成解決方案并用VS2022打開(kāi)
4.添加OpenCvSharp4.Windows和OpenCvSharp4.Extensions(來(lái)自NuGet)
5.核心實(shí)現(xiàn)函數(shù)(注意要套個(gè)線程執(zhí)行):
????????var?capture?=?new?VideoCapture(1,?VideoCaptureAPIs.DSHOW);//本機(jī)攝像頭
????????capture.XI_OffsetX?=?0;?//?以左上角為起點(diǎn)?坐標(biāo)X
????????capture.XI_OffsetY?=?0;?//?以左上角為起點(diǎn)?坐標(biāo)Y
????????capture.FrameWidth?=?640;?//?寬
????????capture.FrameHeight?=?480;?//?高
????????capture.AutoFocus?=?true;
????????var?image?=?new?Mat();
????????while?(true)
????????{
????????????capture.Read(image);//獲取圖像
????????????if?(image.Empty())//為空跳出
????????????????break;
???????????? var?bytes?=?image.ToBytes();//圖像轉(zhuǎn)byte
???????????? Godot.Image?img?=?new?Godot.Image();//實(shí)例化godot?Image節(jié)點(diǎn)
???????????? img.LoadPngFromBuffer(bytes);//裝載圖像
???????????? var?texture?=?new?ImageTexture();//實(shí)例化Texture
???????????? texture.SetImage(img);//裝載img
???????????? Sprite2D.Texture?=?texture;//賦值給Sprite2D顯示
????????}