解决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
    评论
Unity调用海康SDK是指在Unity游戏引擎中使用海康威视的软件开发工具包(SDK)进行开发和集成。海康SDK是海康威视公司提供的一套软件开发工具,用于实现视频监控、视频分析等功能。 在Unity调用海康SDK需要以下步骤: 1. 下载和安装海康SDK:从海康威视的官方网站或开发者中心下载海康SDK,安装到本地开发环境中。 2. 创建Unity项目:打开Unity游戏引擎,创建一个新的项目或打开现有的项目。 3. 导入海康SDK:将海康SDK的相关文件和资源导入到Unity项目中。可以将SDK的相关脚本、插件和资源文件拖放到Unity的资源管理器中,确保文件正确导入。 4. 编写代码:在Unity中编写代码调用海康SDK的功能。通过使用C#或Unity脚本语言,可以通过SDK提供的接口实现视频监控、视频播放、图像抓拍等功能。 5. 构建和测试:在Unity中进行完成后,可以进行项目的构建和测试。根据需要,可以构建为PC、移动设备或其他平台的应用程序,并在相应的设备上进行测试和调试。 6. 集成和发布:完成测试后,可以将Unity项目集成到目标平台中,并进行发布。根据需要,可以将项目发布为独立的应用程序、在线游戏或其他形式的应用。 总之,Unity调用海康SDK是一种在Unity游戏引擎中利用海康威视提供的SDK进行视频监控、视频播放等功能开发和集成的方式。通过上述步骤,开发人员可以在Unity项目中轻松使用海康SDK,并实现各种视频相关的应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值