您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 4浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity调用安卓(Android)手机摄像头

程序员正茂 发布时间:2019-06-13 12:45:20 ,浏览量:4

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);
    }
}

 

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0366s