归海一啸
管理员
管理员
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 喜欢达人
  • 原创写手
阅读:6154回复:3

Vuforia开发技巧二-自动对焦

楼主#
更多 发布于:2015-07-16 23:36
Vuforia SDK自带的Sample中有对焦的源码,这里就是把这些源码提取出来。

在Vuforia的demo中,添加SceneManager可以实现对焦。如果不需要或者需要添加自己的SplashScreen,那么删除这个,按照上面的内容设置自动对焦程序。
C++:
bool focusModeSet = QCAR::CameraDevice::getInstance().setFocusMode(  QCAR::CameraDevice::FOCUS_MODE_CONTINUOUSAUTO);if (!focusModeSet) {    LOG("Failed to set focus mode (unsupported mode).");}
JAVA:
boolean focusModeSet = CameraDevice.getInstance().setFocusMode(  CameraDevice.FOCUS_MODE.FOCUS_MODE_CONTINUOUSAUTO);if (!focusModeSet) {    Log.d("Sample Log Tag", "Failed to set focus mode (unsupported mode).");}
Unity3d C#:
bool focusModeSet = CameraDevice.Instance.SetFocusMode(     CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);if (!focusModeSet) {    Debug.Log("Failed to set focus mode (unsupported mode).");

在Unity3d中,自动对焦的可以使用异步方法。并且该方法最好再start和Update中都实现,并且有时候为了效率,在Update中实现时,不需要每帧都检测对焦,可以选择间隔N帧才对焦。


另外,该对焦方法对于部分手机,可能作用不是特别大,比如小米4。



下面贴上一份通过触屏控制对焦的代码,仅供参考:
using UnityEngine;
using System.Collections;
using Vuforia;
 
public class TestControl : 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) {
                        //this coroutine has been called twice. We should stop the next one here otherwise we get two double tap
                        StopCoroutine ("singleOrDouble");
                        //Debug.Log ("Double");
                        OnDoubleTapped();
                }
        }
 
        private void OnSingleTapped ()
        {
                TriggerAutoFocus ();
                label = "Tap the Screen!";
        }
 
        private void OnDoubleTapped ()
        {
                label = "Double Tap the Screen!";
        }
 
        //SingleTap Gestures are captured by AppManager and calls this method for TapToFocus
        public void TriggerAutoFocus ()
        {
                StartCoroutine (TriggerAutoFocusAndEnableContinuousFocusIfSet ());
        }
     
        /// <summary>
        /// Activating trigger autofocus mode unsets continuous focus mode (if was previously enabled from the UI Options Menu)
        /// So, we wait for a second and turn continuous focus back on (if options menu shows as enabled)
        /// </returns>
        private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet ()
        {
                /*
        //triggers a single autofocus operation 
        if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO)) {
            this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;
        }
         
        yield return new WaitForSeconds(1.0f);
         
        //continuous focus mode is turned back on if it was previously enabled from the options menu
        if(this.View.mAutoFocusSetting.IsEnabled)
        {
            if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO)) {
                this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO;
            }
        }
         
        Debug.Log (this.View.FocusMode);
*/
                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, 10, 100, 100), "----> " + label);
        }
}

最新喜欢:

xxhhxxhh daihe111214daihe1... shao_mingshao_m... dayelongshedayelo...
AR学院(www.arvrschool.com),从这里感触未来!

欢迎分享

buptidsuper
骑士
骑士
  • UID33
  • 粉丝0
  • 关注0
  • 发帖数13
  • 社区居民
沙发#
发布于:2015-10-19 10:15
楼主威武!!!完美解决了我的问题 多谢多谢~~~
walkerwong
骑士
骑士
  • UID517
  • 粉丝1
  • 关注0
  • 发帖数43
  • 忠实会员
板凳#
发布于:2015-11-26 22:02
bool focusModeSet = CameraDevice.Instance.SetFocusMode(     CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);if (!focusModeSet) {    Debug.Log("Failed to set focus mode (unsupported mode).");

教程里的这个代码行不通,渲染的物件会出现,可是背景全是黑屏。。为什么呢?求教。。。。
The story is never end
归海一啸
管理员
管理员
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 喜欢达人
  • 原创写手
地板#
发布于:2015-12-15 19:12
walkerwong:bool focusModeSet = CameraDevice.Instance.SetFocusMode(     CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);if (!focus...回到原帖
背景全黑是你摄像头没显示出来,但是有物体出现,说明已经获取画面了,摄像头打开没问题。背景黑了检查下Camera设置,或者你这个代码加的位置有问题
AR学院(www.arvrschool.com),从这里感触未来!
游客

返回顶部