Unity使用WebCamTexture 实现手机前后摄像头调用

最近搞unity外部摄像头调用,参考了下网上的说法,发现有的已经是很早的写法了,这里自己尝试改了点,以作参考。

难点:获取摄像头

1,WebCamTexture个人认为是Unity封装好的摄像头传过来的图像信息;

2,将WebCamTexture的对象赋值给一个Texture(载体,即游戏物体,可以是GUITexture、Render.Material.Texture、RawImage.Texture等)

3,上面说到的载体Texture调用Play(),Pause(),Stop()即可完成简单的播放,暂停,停止。

4,WebCamTexture.devices属性代表当前的所有设备的引用,实例化WebCamTexture时根据devices中的名字即可代表是哪个摄像头。

下面是用RawImage显示的例子,用到的按钮等要自己创建。

以上是个人理解,如有错误欢迎指正!!!!

using UnityEngine;

using System.Collections;
using UnityEngine.UI;

public class WebCamera : MonoBehaviour {

//组件引用
private RawImage go_rawImageTexture;
public Button[] button;
public Text debugText;
public Image photoImg;

private WebCamTexture _webcamTexFront;
private WebCamTexture _webcamTexBack;
WebCamTexture webcamTexture;
//前后摄像头
public int m_devID = 0;
//宽高比
public float aspect = 9f / 16f;

private string m_deviceName;
public string m_photoName;
public string m_photoPath;

void Start()
{
Debug.Log(Application.persistentDataPath);
go_rawImageTexture = transform.Find("RawImage").GetComponent();

button[0].onClick.AddListener(Play);
button[1].onClick.AddListener(Stop);
button[2].onClick.AddListener(cameraSwitch);
button[3].onClick.AddListener(TakePhoto);
//webcamTexture = new WebCamTexture();

////如果有后置摄像头,调用后置摄像头
//for (int i = 0; i < WebCamTexture.devices.Length; i++)
//{
// if (!WebCamTexture.devices[i].isFrontFacing)
// {
// webcamTexture.deviceName = WebCamTexture.devices[i].name;
// break;
// }
//}
// go_rawImageTexture.texture = webcamTexture;
//webcamTexture.Play();

//WebCamDevice[] devices = WebCamTexture.devices;
//for( var i = 0 ; i < devices.Length ; i++ ){
// Debug.Log(devices[i].name);

// }
}

public delegate void onComplete(Sprite sprite);

public WebCamTexture webCamera
{
get
{
m_deviceName = WebCamTexture.devices[m_devID].name;

if (m_devID == 0)
{
if (_webcamTexBack == null)
{
// Checks how many and which cameras are available on the device
foreach (WebCamDevice device in WebCamTexture.devices)
{
if (!device.isFrontFacing)
{
m_deviceName = device.name;
_webcamTexBack = new WebCamTexture(m_deviceName, Screen.width, (int)(Screen.width * aspect));
}
}
}
return _webcamTexBack;
}
else
{
if (_webcamTexFront == null)
{
// Checks how many and which cameras are available on the device
foreach (WebCamDevice device in WebCamTexture.devices)
{
if (device.isFrontFacing)
{
m_deviceName = device.name;
_webcamTexFront = new WebCamTexture(m_deviceName, Screen.width, (int)(Screen.width * aspect));
}
}
}
return _webcamTexFront;
}
}
}
void Play()
{
go_rawImageTexture.texture = webCamera;
webCamera.Play();
//webcamTexture.Play();
}
void Stop()
{

webCamera.Stop();
//webcamTexture.Stop();
}
public void cameraSwitch()
{
// Checks how many and which cameras are available on the device
foreach (WebCamDevice device in WebCamTexture.devices)
{
if (m_deviceName != device.name)
{
webCamera.Stop();
m_devID++;
if (m_devID >= WebCamTexture.devices.Length) m_devID = 0;
webCamera.deviceName = device.name;

Debug.Log("m_devID" + m_devID);
Debug.Log("m_deviceName" + device.name);
debugText.text="ID:"+m_devID+"Name:"+device.name;
webCamera.Play();
break;
}
}
}
void TakePhoto()
{
takePicture((delegate (Sprite sp)
{
photoImg.sprite = sp;
}));
}

public void takePicture(onComplete callback)
{

StartCoroutine(GetTexture(callback));
}

//捕获照片
//获取截图
public IEnumerator GetTexture(onComplete callback)
{
webCamera.Pause();
yield return new WaitForEndOfFrame();

//save
Texture2D t = new Texture2D(Screen.width, (int)(Screen.width * aspect));
t.ReadPixels(new Rect(0, 0, Screen.width, (int)(Screen.width * aspect)), 0, 0, false);
t.Apply();
byte[] byt = t.EncodeToPNG();
m_photoName = Time.time + ".png";
m_photoPath = Application.persistentDataPath + "/" + m_photoName;
System.IO.File.WriteAllBytes(m_photoPath, byt);

//load image
WWW www = new WWW("file://" + m_photoPath);
yield return www;

Sprite sprite = Sprite.Create(www.texture, new Rect(0, 0, Screen.width, (int)(Screen.width * aspect)), new Vector2(0.5f, 0.5f));
//回调
callback(sprite);
}

// 录像
// 连续捕获照片
public IEnumerator SeriousPhotoes()
{
while (true)
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(400, 300, TextureFormat.RGB24, true);
t.ReadPixels(new Rect(0, 0, Screen.width, (int)(Screen.width * aspect)), 0, 0, false);
t.Apply();
print(t);
byte[] byt = t.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/MulPhotoes/" + Time.time.ToString().Split('.')[0] + "_" + Time.time.ToString().Split('.')[1] + ".png", byt);
// using System.Threading;
// Thread.Sleep(300);
}
}

}

下载地址:

链接:https://pan.baidu.com/s/1bo25P9P 密码:u9oj

你可能感兴趣的:(Unity,Camera)