【发布时间】:2016-11-02 14:38:20
【问题描述】:
我正在尝试在 Unity3D 中的飞机上进行相机展示。我正在使用 AForge 文档中的代码:http://www.aforgenet.com/framework/docs/html/f4d3c2ba-605c-f066-f969-68260ce5e141.htm 除了我通过 Unity 而不是 AForges 通常的方式插入网络摄像头,它想要制作 FilterInfoCollection。如果我没记错的话,Unity 需要这样做才能识别网络摄像头。
但是我的代码无法正常工作,网络摄像头启动(因为我的 webCam.Play()),但没有其他任何反应。通过调试,我发现程序没有到达我的 video_NewFrame 函数,我相信我在使用 Unity 时需要以某种方式初始化?
如何正确设置?
using UnityEngine;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging.Filters;
using AForge.Imaging;
using AForge;
public class LoadVideo : MonoBehaviour {
public VideoCaptureDevice videoSource;
WebCamTexture webCam;
void Start(){
webCam = new WebCamTexture();
webCam.Play();
}
// Update is called once per frame
object b;
public WebCamDevice wc;
public GameObject originalFeed;
void Update () {
videoSource = new VideoCaptureDevice(webCam.deviceName);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
originalFeed.GetComponent<Renderer>().material.mainTexture = originalFeedTexture;
}
public delegate void EventPrototype(System.EventArgs args);
Texture2D originalFeedTexture;
void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Debug.Log("you here mate");
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
MemoryStream memoryStream = new MemoryStream();
video.Save(memoryStream, ImageFormat.Jpeg);
byte[] bitmapRecord = memoryStream.ToArray();
originalFeedTexture.LoadImage(bitmapRecord);
}
}
【问题讨论】:
-
你使用的是什么版本的
AForge? -
@AlessandroD'Andria 2.2.5
-
我在使用
2.2.5时遇到问题并降级到2.2.0(在我的情况下,该事件从未引发)。 -
刚刚做了一个2.2.0降级的测试,完全相同的问题。
-
为什么要使用 AForge 访问相机帧?