WebCamTexture

作者:追风剑情 发布于:2020-5-29 13:18 分类:Unity3d

WebCamTexture API详解 https://blog.csdn.net/it_yanghui/article/details/78615209

在使用ZXing库竖屏扫描二维码时,发现画面旋转了90度。

解决方案:使用下面的shader校正渲染到RawImage上的画面


Shader "Custom/WebCamShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //解决竖屏时WebCamTexture,画面旋转,反转问题
                fixed2 uv = fixed2(1 - i.uv.y, i.uv.x);
                fixed4 col = tex2D(_MainTex, uv);
                return col;
            }
            ENDCG
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 摄像头控制器
/// </summary>
public class WebCamController : MonoBehaviour
{
    public int deviceIndex = 0;
    public string deviceName;
    public int requestedWidth = 400;
    public int requestedHeight = 300;
    public int requestedFPS = 12;
    [SerializeField]
    private RawImage _rawImage;
    [SerializeField]
    private bool _playOnAwake = false;
    private WebCamTexture _webCamTexture;
    //摄像头启动成功
    public Action<WebCamTexture> OnWebCamStarted;
    //没有权限启动摄像头
    public Action OnNoPermission;
    //未找到摄像头
    public Action OnNotFoundCamera;

    private void Awake()
    {
        if (_playOnAwake)
            Startup();
    }

    //启动摄像头
    public void Startup()
    {
        if (_webCamTexture == null)
        {
            StartCoroutine(StartupCamera());
            return;
        }

        if (!_webCamTexture.isPlaying)
        {
            _webCamTexture.Play();
            OnWebCamStarted?.Invoke(_webCamTexture);
        }
    }

    //停止
    public void Stop()
    {
        if (_webCamTexture != null && _webCamTexture.isPlaying)
            _webCamTexture.Stop();
    }

    //暂停
    public void Pause()
    {
        if (_webCamTexture != null && _webCamTexture.isPlaying)
            _webCamTexture.Pause();
    }

    //启动摄像头
    private IEnumerator StartupCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            if (devices.Length == 0)
            {
                Debug.LogError("Not found camera.");
                OnNotFoundCamera?.Invoke();
                yield break;
            }
            deviceName = devices[deviceIndex].name;
            _webCamTexture = new WebCamTexture(deviceName, requestedWidth, requestedHeight, requestedFPS);
            _webCamTexture.Play();
            if (_rawImage != null)
                _rawImage.texture = _webCamTexture;
            OnWebCamStarted?.Invoke(_webCamTexture);
        }
        else
        {
            Debug.LogError("Unable not start the camera without permissions.");
            OnNoPermission?.Invoke();
        }
    }

    //复制摄像头画面
    public Texture2D CopyWebCamTexture()
    {
        if (_webCamTexture == null)
        {
            Texture2D test_tex = Resources.Load<Texture2D>("Texture/body_seg");
            return test_tex;
        }
            
        Texture2D tex = new Texture2D(_webCamTexture.width, _webCamTexture.height, TextureFormat.RGBA32, false);
        Color[] colors = _webCamTexture.GetPixels();
        tex.SetPixels(colors);
        tex.Apply();
        return tex;
    }

    private void OnDestroy()
    {
        Stop();
        if (_rawImage != null)
            _rawImage.texture = null;
        _webCamTexture = null;
        OnWebCamStarted = null;
        OnNoPermission = null;
    }

    public void ShowVideoImage()
    {
        if (_rawImage == null)
            return;
        _rawImage.gameObject.SetActive(true);
    }

    public void HideVideoImage()
    {
        if (_rawImage == null)
            return;
        _rawImage.gameObject.SetActive(false);
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号