Unity调用安卓摄像头和采集卡
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UITest : MonoBehaviour {
    // 图片组件
    public RawImage rawImage;
 
    //图形组件父实体
    public RectTransform imageParent;
 
    //当前相机索引
    private int index = 0;
 
    //当前运行的相机
    private WebCamTexture currentWebCam;
    void Start()
    {
        StartCoroutine(Call());
    }
 
    public IEnumerator Call()
    {
        // 请求权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
 
 
        if (Application.HasUserAuthorization(UserAuthorization.WebCam) && WebCamTexture.devices.Length > 0)
        {
            // 创建相机贴图
            currentWebCam = new WebCamTexture(WebCamTexture.devices[index].name, Screen.width, Screen.height, 60);
            rawImage.texture = currentWebCam;
            currentWebCam.Play();
 
            //前置后置摄像头需要旋转一定角度,否则画面是不正确的,必须置于Play()函数后
            rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, -currentWebCam.videoRotationAngle);
        }
    }
 
    //切换前后摄像头
    public void SwitchCamera()
    {
        if (WebCamTexture.devices.Length < 1)
            return;
 
        if (currentWebCam != null)
            currentWebCam.Stop();
 
        index++;
        index = index % WebCamTexture.devices.Length;
 
        // 创建相机贴图
        currentWebCam = new WebCamTexture(WebCamTexture.devices[index].name, Screen.width, Screen.height, 60);
        rawImage.texture = currentWebCam;
        currentWebCam.Play();
 
        //前置后置摄像头需要旋转一定角度,否则画面是不正确的,必须置于Play()函数后
        rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, -currentWebCam.videoRotationAngle);
    }
}

这里建议不要通过代码去翻转rawImage,直接在Inspector里面直接反正成反方向

采集卡

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardControl : MonoBehaviour
{
     public RawImage rawImage;//相机渲染的UI
    public GameObject quad;//相机渲染的GameObject
    private WebCamTexture webCamTexture;
 
    void Start()
    {
        ToOpenCamera();
    }
 
    /// <summary>
    /// 打开摄像机
    /// </summary>
    public void ToOpenCamera()
    {
        StartCoroutine("OpenCamera");
    }
    public IEnumerator OpenCamera()
    {
        
        int maxl = Screen.width;
        if (Screen.height > Screen.width)
        {
            maxl = Screen.height;
        }
 
        // 申请摄像头权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            if (webCamTexture != null)
            {
                webCamTexture.Stop();
            }
 
            //打开渲染图
            if (rawImage != null)
            {
                rawImage.gameObject.SetActive(true);
            }
            if (quad != null)
            {
                quad.gameObject.SetActive(true);
            }
 
            // 监控第一次授权,是否获得到设备(因为很可能第一次授权了,但是获得不到设备,这里这样避免)
            // 多次 都没有获得设备,可能就是真没有摄像头,结束获取 camera
            int i = 0;
            while (WebCamTexture.devices.Length <= 0 && i < 300)
            {
                yield return new WaitForEndOfFrame();
                i++;
            }
            WebCamDevice[] devices = WebCamTexture.devices;//获取可用设备
            if (WebCamTexture.devices.Length <= 0)
            {
                Debug.LogError("没有摄像头设备,请检查");
            }
            else
            {
                string devicename = devices[1].name;
                webCamTexture = new WebCamTexture(devicename, maxl, maxl == Screen.height ? Screen.width : Screen.height, 30)
                {
                    wrapMode = TextureWrapMode.Repeat
                };
 
                // 渲染到 UI 或者 游戏物体上
                if (rawImage != null)
                {
                    rawImage.texture = webCamTexture;
                }
 
                if (quad != null)
                {
                    quad.GetComponent<Renderer>().material.mainTexture = webCamTexture;
                }
 
 
                webCamTexture.Play();
            }
 
        }
        else {
            Debug.LogError("未获得读取摄像头权限");
        }
    }
 
    private void OnApplicationPause(bool pause)
    {
        // 应用暂停的时候暂停camera,继续的时候继续使用
        if (webCamTexture !=null)
        {
            if (pause)
            {
                webCamTexture.Pause();
            }
            else
            {
                webCamTexture.Play();
            }
        }
        
    }
    
    
    private void OnDestroy()
    {
        if (webCamTexture != null)
        {
            webCamTexture.Stop();
        }
    }

}
上一篇
下一篇