您的位置:首页 > 移动开发 > Unity3D

Vuforia设置摄像头自动聚焦和分辨率的问题

2017-10-21 10:26 417 查看
在使用Vuforia进行AR开发时,如果使用的是具有高分辨率和对焦功能的摄像头进行识别,需要我们注意进行相关参数的设置。

以unity5.6.0和6.2.6的Vuforia SDK为环境进行开发。

(1)设置连续自动对焦功能。

这一功能的设置在官网有说明,使用SDK时进行重载就可以了。原文链接地址在这里

在ARCamera直接加载FrameRateSettings.cs脚本,在这个脚本中进行设置:

void Start ()

    {
VuforiaBehaviour.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
VuforiaBehaviour.Instance.RegisterOnPauseCallback(OnPaused);

    }

    private void OnVuforiaStarted ()

    {

        // Try register camera image format

        if (CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGBA8888, true))

        {

            Debug.Log("Successfully registered pixel format " + Image.PIXEL_FORMAT.RGBA8888.ToString());

        }

        else

        {

            Debug.LogError(

                "\nFailed to register pixel format: " + Image.PIXEL_FORMAT.RGBA8888.ToString() +

                "\nThe format may be unsupported by your device." +

                "\nConsider using a different pixel format.\n");

          //  mFormatRegistered = false;

        }

        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

        // Query Vuforia for recommended frame rate and set it in Unity

        int targetFps = VuforiaRenderer.Instance.GetRecommendedFps(VuforiaRenderer.FpsHint.NONE);

       

        // By default, we use Application.targetFrameRate to set the recommended frame rate.

        // Google Cardboard does not use vsync, and OVR explicitly disables it. If developers 

        // use vsync in their quality settings, they should also set their QualitySettings.vSyncCount

        // according to the value returned above.

        // e.g: If targetFPS > 50 --> vSyncCount = 1; else vSyncCount = 2;

        if (Application.targetFrameRate != targetFps)

        {

            Debug.Log("Setting frame rate to " + targetFps + "fps");

            Application.targetFrameRate = targetFps;

        }

    }

private void OnPaused(bool paused)

    {

        if (!paused) // resumed

        {

            // Set again autofocus mode when app is resumed

            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

        }

    }

(2)设置摄像头分辨率

根据这篇文章中的介绍,有了思路。这一个Vuforia也留给了我们接口文件,直接按照格式对应的相机名称进行修改或添加对应的相机分辨率就可以了。

这个接口文件名称是profiles.xml,位置在Vuforia\Editor\WebcamProfiles中,打开会大吃一惊,各种常用的网络摄像头都有,但是默认的分辨率都是640*480,

这里提供WebcamProfiles文件的下载,请点击这里下载

但是并不需要更改这个文件夹的位置,保持其目录不变,修改完毕即可。

同时读取Image参数可以发现分辨率已经更改了。

void Start (){

VuforiaBehaviour.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);

}

void OnTrackablesUpdated()

    {

        if (true)

        {

            if (true)

            {

                Vuforia.Image image = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);

                if (image != null)

                {

  

                    Debug.Log(

                        "\nImage Format: " + image.PixelFormat +

                        "\nImage Size:   " + image.Width + "x" + image.Height +

                        "\nBuffer Size:  " + image.BufferWidth + "x" + image.BufferHeight +

                        "\nImage Stride: " + image.Stride + "\n"+

                        "w:" + VuforiaRenderer.Instance.VideoBackgroundTexture.texelSize.x+"\n"+

                        "H:" + VuforiaRenderer.Instance.VideoBackgroundTexture.texelSize.y+"\n"

                    );

                    byte[] pixels = image.Pixels;

                    if (pixels != null && pixels.Length > 0)

                    {

                        Debug.Log(

                            "\nImage pixels: " +

                            pixels[0] + ", " +

                            pixels[1] + ", " +

                            pixels[2] + ", ...\n"

                        );

                    }

                }

            }

        }

    }

需要注意一定要用对应的摄像头名称,如果不确定摄像头名称用程序读出来。

using UnityEngine;

using System.Collections;

public class webCamFetch : MonoBehaviour {
WebCamTexture webcamtexture;
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length > 0) {
Debug.Log ("DevicesName" + devices [0].name);
}
}

}

好了,运行发现,摄像头分辨率变得更清晰了,可识别的距离也增大了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sdk 摄像头 unity Vuforia
相关文章推荐