【问题标题】:How to Display WebcamTexture as Full Screen on Android Portrait Mode?如何在 Android 纵向模式下将 WebcamTexture 显示为全屏?
【发布时间】:2016-06-28 09:31:22
【问题描述】:

我正在尝试在 android Portrait 模式下显示全屏 Webcamtexture。 但是当我在我的设备上构建和测试它时,它的旋转和 videoRotationAngle 是 90

我使用 RawImage 作为纹理。我在一些帖子上看到您必须旋转变换并获得正确的角度。但问题是,如果我旋转 RawImage UI,它将不再是全屏视图。

  var camImage:UnityEngine.UI.RawImage;
  var baseRotation:Quaternion;
  var webcamTexture:WebCamTexture;
  var rotation:UnityEngine.UI.Text;


function Start () {

         webcamTexture = new WebCamTexture(Screen.width,Screen.height);
         camImage.texture=webcamTexture;
         camImage.material.mainTexture = webcamTexture;
         webcamTexture.Play();
         rotation.text= webcamTexture.videoRotationAngle.ToString(); // to check the angle


}

【问题讨论】:

  • 您可以在执行任何旋转之前获取相机的宽度和高度,并使用它来将纹理拉伸到所需的尺寸。我使用的是正交相机,所以我的代码可能不适用于您的实现。

标签: unity3d unity3d-gui


【解决方案1】:

您有两个选择:您可以选择纵向或横向并阻止屏幕旋转,或者您可以自动旋转对象并根据屏幕边界拉伸它。有关详细信息,请参阅 Screen.height 和 Screen.length。

【讨论】:

  • 我的 android 版本只设置为纵向模式。变换旋转的问题是它会拉伸太多并且看起来不太好。
  • 如果您无法以不同的分辨率接收网络摄像头纹理,那么据我所知,它不可能在不变形或调整大小的情况下全屏显示。
  • 当您以纵向模式打开相机应用程序时,您看不到失真,这意味着它不是硬件限制。我正在寻找类似的东西。也许它需要为此构建插件
【解决方案2】:

您需要翻转和旋转它。如果你喜欢 CameraCaptureKit (https://www.assetstore.unity3d.com/en/#!/content/56673),你会发现有不同的方法可以实现你想要的。在 CameraCaptureKit 中,有一个自定义的 RawImage 组件,它本质上是摆弄 RawImahge 的 UV 坐标,而不是旋转它。这样,您可以将图像旋转 90 度或 270 度并翻转它,而无需担心手机是否以横向模式运行。

【讨论】:

  • 这不是解决方案。这是一个广告。这是一款 30 美元的付费软件,用于解决任何试图使用实时摄像头视图的 Android 应用程序中最古老、最著名的摄像头问题之一。解决方案不是作者的革命性或专有创造。
【解决方案3】:

(mCamera.videoRotationAngle + 90) % 360 将正确旋转纹理,然后分配一个 localScale 交换纹理的高度和宽度以固定大小。

【讨论】:

    【解决方案4】:

    我知道这是迟到的回复,但可能会对面临类似问题的人有所帮助。

    如果您使用 RawImage 作为纹理,下面的代码应该可以帮助您在 Potrait 和 Landscape 模式下实现渲染。

    只需确保将原始图像的“Aspect Ratio Fitter”中的“Aspect Mode”设置为“Width Controls Height”或“Height Controls Width”(根据方向以较大者为准)

    代码片段

    using UnityEngine;
    using UnityEngine.UI;
    using System.Linq;
    using System.Collections;
    
    public class DeviceCameraController : MonoBehaviour
    {
        public RawImage image;
        public AspectRatioFitter imageFitter;
    
        //set it to either FRONT or BACK
        string myCamera = "BACK";
    
        // Device cameras
        WebCamDevice frontCameraDevice;
        WebCamDevice backCameraDevice;
        WebCamDevice activeCameraDevice;
    
        WebCamTexture frontCameraTexture;
        WebCamTexture backCameraTexture;
        WebCamTexture activeCameraTexture;
    
        // Image rotation
        Vector3 rotationVector = new Vector3(0f, 0f, 0f);
    
        // Image uvRect
        Rect defaultRect = new Rect(0f, 0f, 1f, 1f);
        Rect fixedRect = new Rect(0f, 1f, 1f, -1f);
    
        // Image Parent's scale
        Vector3 defaultScale = new Vector3(1f, 1f, 1f);
        Vector3 fixedScale = new Vector3(-1f, 1f, 1f);
    
        void Start()
        {
            // Check for device cameras
            if (WebCamTexture.devices.Length == 0)
            {
                Debug.Log("No devices cameras found");
                return;
            }
    
            // Get the device's cameras and create WebCamTextures with them
            frontCameraDevice = WebCamTexture.devices.Last();
            backCameraDevice = WebCamTexture.devices.First();
    
            frontCameraTexture = new WebCamTexture(frontCameraDevice.name);
            backCameraTexture = new WebCamTexture(backCameraDevice.name);
    
            // Set camera filter modes for a smoother looking image
            frontCameraTexture.filterMode = FilterMode.Trilinear;
            backCameraTexture.filterMode = FilterMode.Trilinear;
    
            // Set the camera to use by default
            if (myCamera.Equals("FRONT"))
                SetActiveCamera(frontCameraTexture);
            else if (myCamera.Equals("BACK"))
                SetActiveCamera(backCameraTexture);
            else // default back
                SetActiveCamera(backCameraTexture);
        }
    
        // Set the device camera to use and start it
        public void SetActiveCamera(WebCamTexture cameraToUse)
        {
            if (activeCameraTexture != null)
            {
                activeCameraTexture.Stop();
            }
    
            activeCameraTexture = cameraToUse;
            activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device =>
                device.name == cameraToUse.deviceName);
    
            image.texture = activeCameraTexture;
            image.material.mainTexture = activeCameraTexture;
    
            activeCameraTexture.Play();
        }
    
        // Make adjustments to image every frame to be safe, since Unity isn't 
        // guaranteed to report correct data as soon as device camera is started
        void Update()
        {
            // Skip making adjustment for incorrect camera data
            if (activeCameraTexture.width < 100)
            {
                Debug.Log("Still waiting another frame for correct info...");
                return;
            }
    
            // Rotate image to show correct orientation 
            rotationVector.z = -activeCameraTexture.videoRotationAngle;
            image.rectTransform.localEulerAngles = rotationVector;
    
            // Set AspectRatioFitter's ratio
            float videoRatio =
                (float)activeCameraTexture.width / (float)activeCameraTexture.height;
            imageFitter.aspectRatio = videoRatio;
    
            // Unflip if vertically flipped
            image.uvRect =
                activeCameraTexture.videoVerticallyMirrored ? fixedRect : defaultRect;
    
        }
    }

    如果您遇到任何问题,请告诉我。

    【讨论】:

    • 在 Unity v2019.2.19f1 (iOS) 上为我解决了这个问题
    • 在 Unity v2019.2.19f1 (Android) 上为我解决了这个问题
    • 完美运行,谢谢! (统一2019.4)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2015-01-31
    • 1970-01-01
    相关资源
    最近更新 更多