Unity3D调用摄像头时的最高分辨率

怎样知道一款网络摄像头支持的最高分辨率呢?

1. 我们根据该网络摄像头的型号来查其具体参数,我手上有罗技C310和罗技C920,C310支持1280x720,C920支持1920x1080,这样在构造WebCamTexture时就可以指定其支持的最大分辨率。

测试用的C#脚本如下:

using UnityEngine;
using System.Collections;

public class CameraTest : MonoBehaviour {

	private string deviceName;
	private WebCamTexture tex;
	private Vector2 resSize = new Vector2(1920,1080);
	//private Vector2 resSize = new Vector2(640,480);
	public GameObject plane;

	// Use this for initialization
	void Start () 
	{
		StartCoroutine(InitCamera());
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	protected IEnumerator InitCamera()
	{
		//获取授权
		yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
		if (Application.HasUserAuthorization(UserAuthorization.WebCam))
		{
			WebCamDevice[] devices = WebCamTexture.devices;
			Debug.Log( "devices.Length " + devices.Length );
			if( devices.Length>0 )
			{
				deviceName = devices[0].name;
				Debug.Log( "deviceName " + deviceName );
				tex = new WebCamTexture(deviceName,(int)resSize.x, (int)resSize.y, 30);
				Debug.Log( "tex.width " + tex.width );
				Debug.Log( "tex.height " + tex.height );
				plane.renderer.material.mainTexture = tex;
				tex.Play();
				Debug.Log( "tex.width " + tex.width );
				Debug.Log( "tex.height " + tex.height );
			}
		}
	} 
}

2. 如果是不知型号的摄像头,我们可以在WebCamTexture.Play()后,输出WebCamTexture.width() 和 height(),经测试和查看了下官方文档,如果构造函数时输入的分辨率过大,比如我在用C310时,构造函数却指定1920x1080,这样子它就会用最接近的分辨率,其实这时候最接近的分辨率就是其支持的最大分辨率了。实际上C310支持1280x960,超过官方给的数据。

 参考资料:http://docs.unity3d.com/ScriptReference/WebCamTexture-ctor.html




你可能感兴趣的:(Unity3D)