解决unity调用WebCam显示的拉伸

这个拉伸可能只是我有的问题。。。先记录一下

首先检测有没有摄像机的权限
安卓平台:Permission.HasUserAuthorizedPermission(Permission.Camera)
ios:Application.HasUserAuthorization(UserAuthorization.WebCam)

但是这里我有个问题,哪位大神能告诉我下,为啥安卓检查的这个方法要是IEnumerator,我 void就不行。。。 也没啥返回值啊

         string deviceName = cameraDevices[0].name;
         
            camTexture = new WebCamTexture(deviceName);
            showImage.canvasRenderer.SetTexture(camTexture);
            camTexture.Play();

            webCamTexture = camTexture;

问题就是showImage的大小 不能是ui自适应的屏幕大小,应该根据相机的尺寸去设置。


float  cameraSizeRatio = (float)camTexture.width / (float)camTexture.height;
float screenRatio = (float)UnityEngine.Screen.width / (float)UnityEngine.Screen.height;

if (cameraSizeRatio < screenRatio)
{
	showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width, UnityEngine.Screen.height * screenRatio / cameraSizeRatio);
}
else
{
	showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width / screenRatio * cameraSizeRatio, UnityEngine.Screen.height);
}

就不拉伸了


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Android;
public class WebCameraController : MonoBehaviour
{
    WebCamTexture camTexture;
    public RawImage showImage;

    /// <summary>
    /// 已开启摄像头
    /// </summary>
    /// <returns></returns>
    private bool hasUseCamera = false;

    private void Update()
    {

        if (!hasUseCamera)
        {
#if UNITY_IOS
    StartCoroutine(CallCameraIOS());

#elif UNITY_ANDROID
            // CallCameraAndriod();
            StartCoroutine(CallCameraAndriod());
#else
    //  CallCameraAndriod();  
     StartCoroutine(CallCameraIOS());
#endif
        }
    }




    public WebCamTexture webCamTexture;

    ///??? void 就不行  IEnumerator 就可以?????
    IEnumerator CallCameraAndriod()
    {

        Debug.Log("CallCameraAndriod");
        yield return null;

        if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
        {
            Permission.RequestUserPermission(Permission.Camera);
        }

        if (Permission.HasUserAuthorizedPermission(Permission.Camera) && !hasUseCamera)
        {
            SetwebCam();
        }
    }

    IEnumerator CallCameraIOS()
    {

        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam) && !hasUseCamera)
        {

            SetwebCam();
        }
    }


    private void SetwebCam()
    {
        Debug.Log("SetwebCam");
        hasUseCamera = true;
        if (camTexture != null)
            camTexture.Stop();

        WebCamDevice[] cameraDevices = WebCamTexture.devices;

        if (cameraDevices.Length > 0)
        {
            string deviceName = cameraDevices[0].name;
            Debug.Log(deviceName);

            camTexture = new WebCamTexture(deviceName);
            showImage.canvasRenderer.SetTexture(camTexture);
            camTexture.Play();

            webCamTexture = camTexture;

            // Debug.Log("camTexture.width " + camTexture.width + "         camTexture.height " + camTexture.height);

            float showImageHeight = showImage.GetComponent<RectTransform>().sizeDelta.y;
            float showImageWidth = showImage.GetComponent<RectTransform>().sizeDelta.x;

            // Debug.Log("showImageWidth " + showImageWidth + "   showImageHeight" + showImageHeight);
            // Debug.Log("UnityEngine.Screen.width " + UnityEngine.Screen.width + "     UnityEngine.Screen.height " + UnityEngine.Screen.height);

            // showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.height * camTexture.width / camTexture.height, UnityEngine.Screen.height);
            showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width, UnityEngine.Screen.width / (float)camTexture.width * (float)camTexture.height);


        }
        else
        {
            Debug.LogError(" 没有连接的摄像头 ");
        }
    }

    private void OnDestroy()
    {
        camTexture.Stop();

    }


    public WebCamTexture GetCamTexture()
    {
        return camTexture;
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值