简体   繁体   English

从IP摄像机源(Unity3d + c#)捕获和显示视频

[英]Capture and Show Video from IP camera source (Unity3d + c#)

Need your help. 需要你的帮助。 I am making simple application (and I'm a new to Unity3d also), which takes video from IP-camera and displays it onto Texture2D. 我正在做一个简单的应用程序(也是U​​nity3d的新手),它可以从IP摄像机获取视频并将其显示在Texture2D上。 Video format is MJPG. 视频格式为MJPG。 For simple jpg-images the code below works fine, but when I try to display MJPG I just get gray screen. 对于简单的jpg图片,下面的代码可以正常工作,但是当我尝试显示MJPG时,我只会得到灰屏。

Did I make a mistake in code? 我在代码中犯了错误吗?

public class testVid : MonoBehaviour {

    //public string uri = "http://24.172.4.142/mjpg/video.mjpg"; //url for example
    public Texture2D cam;
    public void Start() {
        cam = new Texture2D(1, 1, TextureFormat.RGB24, false);
        StartCoroutine(Fetch());
    }

    public IEnumerator Fetch() {
        while(true) {
            Debug.Log("loading... " + Time.realtimeSinceStartup);
            WWWForm form = new WWWForm();
            WWW www = new WWW("http://24.172.4.142/mjpg/video.mjpg");
            yield return www;
            if(!string.IsNullOrEmpty(www.error))
                throw new UnityException(www.error);
            www.LoadImageIntoTexture(cam);
        }
    }
    public void OnGUI() {
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), cam);
    }
}

I used this plugin https://www.assetstore.unity3d.com/en/#!/content/15580 我使用了这个插件https://www.assetstore.unity3d.com/en/#!/content/15580

  • Add the script to a game object 将脚本添加到游戏对象
  • Set the URL of the video in the script 在脚本中设置视频的URL
  • Create a new material of Unlit 2D 创建Unlit 2D的新材质
  • Add this material to the movie script in the inspector 将此材料添加到检查器中的电影脚本中
  • Then assign the same material to the game object you 然后将相同的材质分配给您的游戏对象
    want to display the video (eg a quad) 要显示视频(例如,四边形)

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM