Unity3D中的IP摄像头

时间:2014-07-02 09:39:45

标签: unity3d http-status-code-401

我试图通过IP摄像头在Unity3D中获取视图。只有当我使用URL通过脚本访问摄像机时(见下文),它才会出现401 Unauthorized错误。现在我需要使用密码和用户名登录,均为admin。

但我不确定如何将其放入网址,对此有何帮助?请参阅下面的代码,了解我目前的情况。

//#pragma strict

// http://docs.unity3d.com/Documentation/ScriptReference/WWW.LoadImageIntoTexture.html

var url = "http://192.168.1.30/admin/view.cgi?profile=2&language=en";

function Start () {
    // Create a texture in DXT1 format

    // NOTE: you may want to experiment with different texture formats, specially in a web context
    // https://docs.unity3d.com/Documentation/ScriptReference/TextureFormat.html
    renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false); 
    Debug.Log(adress);
    Debug.Log(url);
    while(true) {
        // Start a download of the given URL
        var www = new WWW(url);
        // wait until the download is done
        yield www;
        // assign the downloaded image to the main texture of the object
        www.LoadImageIntoTexture(renderer.material.mainTexture);
    }
}

============================================== < / p>

所以现在我正在使用WWWForm.Headers。然而,当我把它放在我的代码中时,它给了我一个&#34;一个字段初始值设定项不能引用字段方法或属性&#34;

代码是:

公共课SEMTEX:MonoBehaviour {

// Use this for initialization
//void Start() {
    public WWWForm form = new WWWForm ();
    public string headers = form.headers;
    public byte[] rawData = form.data;
    public string url = "http://192.168.1.101/snapshot.cgi";
    public WWW www = new WWW (url, rawData, headers);
//}

IEnumerator Update (){

    form.AddField ("name", "value");
    headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes ("admin:admin"));
    yield return www;
}

知道发生了什么事吗?

2 个答案:

答案 0 :(得分:2)

使用WWWForm.headersThe example in the docs正在进行HTTP基本身份验证。

另外,如果您经常计划更新图像,那么使用像TextureFormat.RGB24这样的未压缩纹理应该会快得多。


统一文档已经过时了,所以这里有完整的脚本,可以不断更新网络摄像头并将其绘制在屏幕上:

using UnityEngine;
using System.Collections;

public class HttpWebcam : MonoBehaviour {

    public string uri;
    public string username;
    public string password;


    Texture2D cam;


    public void Start() {
        cam=new Texture2D(1, 1, TextureFormat.RGB24, false);
        StartCoroutine(Fetch());
    }


    public IEnumerator Fetch() {
        while(true) {
            Debug.Log("fetching... "+Time.realtimeSinceStartup);

            WWWForm form = new WWWForm();
            form.AddField("dummy", "field");    // required by WWWForm
            WWW www = new WWW(uri, form.data, new System.Collections.Generic.Dictionary<string,string>() {  // using www.headers is depreciated by some odd reason
                {"Authorization", "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username+":"+password))}
            });
            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);
    }

}

运行只需拖动到GameObject并提供包含PNG或JPEG的任何URI。它还可以访问不受密码保护的资源(然后忽略密码)。例如,您可以使用:http://www.sudftw.com/imageppc.php

答案 1 :(得分:1)

添加Krzysztof的答案,将public GameObject displayPlane; public void OnGUI() { displayPlane.GetComponent<Renderer>().material.mainTexture = cam; } 方法替换为将相机Feed叠加到游戏内对象(例如游戏中的电视屏幕)上,而不是将其绘制在整个屏幕。

{{1}}