caizedong的专栏

千里之行始足下,万丈高楼平地起,恒心信念执行力

Vuforia应用之相机自动对焦功能

        在一些卡牌(卡片)游戏应用上,如果不能很好的对焦就会导致用户体验不佳,假若让系统相机去自动对焦这样出来的效果就会好很多,好的下面来说说——

说之前,先科普一下,不喜略过直接往下看哈~

        对焦也叫对光、聚焦。通过照相机对焦机构变动物距和相距的位置,使被拍物成像清晰的过程就是对焦。而对焦的方式有以下几种:自动对焦、手动对焦、多重对焦以及全息自动对焦、我们这里讲的就是自动对焦,传统相机上的傻瓜式对焦方法。以下是相关的代码实现:

        基于触摸屏,一种是直接将脚本文件挂载到ARCamera,一种是用NGUI、将其放到新建的GameManger底下,不过两种思路都是相同的,自动对焦的代码要写在start()方法里面,如图:(切记:using Vuforia;这个需要在一开始新建的脚本中引入)

              

接下来分析代码:

//自动对焦变量
    private string label;
    private float touchduration;
    private Touch touch;
Start()方法,初始化并进行自动对焦调用

void Start () {
        
        if (Input.touchCount > 0)
        {
            touchduration += Time.deltaTime;
            touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Ended && touchduration < 0.2f)
            {
                StartCoroutine("SingleOrDouble");
            }
        }
        else
        {
            touchduration = 0;
        }
    }
IEnumerator SingleOrDouble()
    {
        yield return new WaitForSeconds(0.3f);
        if (touch.tapCount == 1)
        {
            Debug.Log("Single");
            OnSingleTapped();
        }
        else if (touch.tapCount == 2)
        {
            StopCoroutine("SingleOrDouble");//否则会触发两次Double Touch
            Debug.Log("Double");
            OnDoubleTapped();
        }
    }

    private void OnSingleTapped()
    {
        TriggerAutoFocus();
        label = "Tap the Screen!";
    }

    private void OnDoubleTapped()
    {
        label = "Double Tap the Screen!";
    }
触发自动对焦方法:

public void TriggerAutoFocus()
    {
        StartCoroutine(TriggerAutoFocusAndEnableContinuousFocusIfSet());
    }

    private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet()
    {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
        yield return new WaitForSeconds(1.0f);
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    void OnGUI()
    {
        GUI.Label(new Rect(10, 0, 100, 100), "-&-C*-Z*-D*-&->" + label);
    }
最后在update()方法写上:

void Update () {

        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
}
有的童鞋,也将自动对焦的代码写在update()方法里面,即是start()方法空着,由于我这边的功能实现上还有别的,所以我做了这样的处理,希望大家多多交流哈,不吝赐教。

using UnityEngine;
using System.Collections;
using Vuforia;

public class AutoFocusControl : MonoBehaviour {

    private string label;
    private float touchduration;
    private Touch touch;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
        if (Input.touchCount > 0)
        {
            touchduration += Time.deltaTime;
            touch = Input.GetTouch(0);
            if(touch.phase == TouchPhase.Ended && touchduration < 0.2f)
            {
                StartCoroutine("SingleOrDouble");
            }
        }
        else
        {
            touchduration = 0;
        }
	}

    IEnumerator SingleOrDouble()
    {
        yield return new WaitForSeconds(0.3f);
        if (touch.tapCount == 1)
        {
            Debug.Log("Single");
            OnSingleTapped();
        }
        else if (touch.tapCount == 2)
        {
            StopCoroutine("SingleOrDouble");
            Debug.Log("Double");
            OnDoubleTapped();
        }
    }

    private void OnSingleTapped()
    {
        TriggerAutoFocus();
        label = "Tap the Screen!";
    }

    private void OnDoubleTapped()
    {
        label = "Double Tap the Screen!";
    }

    public void TriggerAutoFocus()
    {
        StartCoroutine(TriggerAutoFocusAndEnableContinuousFocusIfSet());
    }

    private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet()
    {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
        yield return new WaitForSeconds(1.0f);
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    void OnGUI()
    {
        GUI.Label(new Rect(10, 0, 100, 100), "---->" + label);
    }
}
最后的最后,说说一些综合的东西。

1.测试在手机上面实测,效果比较明显

2.关于vuforia sdk 3.x和4.x的代码异同,当然现在已经去到5.x版本了,不过拾人牙慧,以下上代码:

3.x版本的:

void Start () {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
	}
4.x版本的:

	void Start () {
        Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

	}
	


附录:相关文章  AR自动对焦



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭