Log in or register to post comments

Focusing and Flash on Camera

January 15, 2011 - 2:56am #1

Is there a way to focus the AR camera - also, for devices that come with a flash on camera (even video camera), is there a way to control the flash?

Focusing and Flash on Camera

February 22, 2018 - 8:39am #10

hi i am getting an error  the debugger says that C:\Users\\Documents\AR\Assets\flash.cs(10,10): Error CS0246: The type or namespace name 'CameraDevice' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp) pls help me

Re: Focusing and Flash on Camera

May 5, 2012 - 5:57pm #9

CameraDevice is used to access the features of the device camera that are exposed through the SDK. This is distinct from ARCamera, which is the scene camera. You can call CameraDevice from any of your scripts.

Take a look at the CameraDeviceMenu.cs script in the Scripts folder for examples of how to do this..



using UnityEngine;
using System.Collections;
using System;

public class CameraDeviceMenu : MonoBehaviour
{
    #region NESTED

    // Defines which menu to show.
    private enum MenuMode
    {
        MENU_OFF,            // Do not show a menu (default).
        MENU_CAMERA_OPTIONS, // Show camera device options.
        MENU_FOCUS_MODES     // Show focus mode menu.
    }

    #endregion // NESTED



    #region PRIVATE_MEMBER_VARIABLES

    // Currently active menu.
    private MenuMode mMenuToShow = MenuMode.MENU_OFF;

    // Check if a menu button has been pressed.
    private bool mButtonPressed = false;

    // Check if flash is currently enabled.
    private bool mFlashEnabled = false;

    // Contains the currently set auto focus mode.
    private CameraDevice.FocusMode mFocusMode =
        CameraDevice.FocusMode.FOCUS_MODE_NORMAL;

    // Contains the rectangle for the camera options menu.
    private Rect mAreaRect;

    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS

    public void Start()
    {
        // Setup position and size of the camera menu.
        computePosition();
    }


    public void OnApplicationPause(bool pause)
    {
        // Upon resuming reactivate the torch if it was active before pausing:
        if (!pause)
        {
            if (mFlashEnabled)
            {
                mFlashEnabled = CameraDevice.Instance.SetFlashTorchMode(true);
            }
        }
    }

    
    public void Update()
    {
        // If the touch event results from a button press it is ignored.
        if (!mButtonPressed)
        {
            // If finger is removed from screen.
            if (Input.GetMouseButtonUp(0))
            {
                // If menu is not rendered.
                if (mMenuToShow == MenuMode.MENU_OFF)
                {
                    // Show menu.
                    mMenuToShow = MenuMode.MENU_CAMERA_OPTIONS;
                }
                // If menu is already open.
                else
                {
                    // Close menu
                    mMenuToShow = MenuMode.MENU_OFF;
                }
            }
        }
        else
        {
            mButtonPressed = false;
        }
    }


    // Draw menus.
    public void OnGUI()
    {
        switch (mMenuToShow)
        {
            case MenuMode.MENU_CAMERA_OPTIONS:
                DrawMenu();
                break;

            case MenuMode.MENU_FOCUS_MODES:
                DrawFocusModes();
                break;

            default:
                break;
        }
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PRIVATE_METHODS

    // Draw menu to control camera device.
    private void DrawMenu()
    {
        computePosition();

        // Setup style for buttons.
        GUIStyle buttonGroupStyle = new GUIStyle(GUI.skin.button);
        buttonGroupStyle.stretchWidth = true;
        buttonGroupStyle.stretchHeight = true;

        GUILayout.BeginArea(mAreaRect);

        GUILayout.BeginHorizontal(buttonGroupStyle);

        // Turn flash on or off.
        if (GUILayout.Button("Toggle Flash", buttonGroupStyle))
        {
            if (!mFlashEnabled)
            {
                // Turn on flash if it is currently disabled.
                CameraDevice.Instance.SetFlashTorchMode(true);
                mFlashEnabled = true;
            }
            else
            {
                // Turn off flash if it is currently enabled.
                CameraDevice.Instance.SetFlashTorchMode(false);
                mFlashEnabled = false;
            }

            mMenuToShow = MenuMode.MENU_OFF;
            mButtonPressed = true;
        }

        // Triggers auto focus:
        if (GUILayout.Button("Autofocus", buttonGroupStyle))
        {
            if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO))
                mFocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;

            mMenuToShow = MenuMode.MENU_OFF;
            mButtonPressed = true;
        }

        // Choose focus mode.
        if (GUILayout.Button("Focus Modes", buttonGroupStyle))
        {
            mMenuToShow = MenuMode.MENU_FOCUS_MODES;
            mButtonPressed = true;
        }

        GUILayout.EndHorizontal();

        GUILayout.EndArea();
    }


    // Draw menu to let user choose a focus mode.
    private void DrawFocusModes()
    {
        CameraDevice.FocusMode newMode;
        newMode = EnumOptionList(mFocusMode);

        // We set the new value only if the mode has changed.
        if (newMode != mFocusMode)
        {
            if (CameraDevice.Instance.SetFocusMode(newMode))
                mFocusMode = newMode;

            mMenuToShow = MenuMode.MENU_OFF;
            mButtonPressed = true;
        }
    }


    // Helper function to automatically create an option list of an enum object.
    private static CameraDevice.FocusMode EnumOptionList(
                                    CameraDevice.FocusMode setMode)
    {
        Type modeType = setMode.GetType();

        // Get possible enum values.
        CameraDevice.FocusMode[] modes =
            (CameraDevice.FocusMode[])Enum.GetValues(modeType);

        // Setup style for list.
        GUIStyle optionListStyle = new GUIStyle(GUI.skin.button);
        optionListStyle.stretchHeight = true;
        optionListStyle.stretchWidth = true;

        // Setup style for toggles.
        // We use "button" style as template because default toggles are too
        // small.
        GUIStyle toggleStyle = new GUIStyle(GUI.skin.button);
        toggleStyle.stretchHeight = true;
        toggleStyle.stretchWidth = true;
        toggleStyle.normal.textColor = Color.gray;
        toggleStyle.onNormal.textColor = Color.gray;
        toggleStyle.focused.textColor = Color.gray;
        toggleStyle.onFocused.textColor = Color.gray;
        toggleStyle.active.textColor = Color.gray;
        toggleStyle.onActive.textColor = Color.gray;
        toggleStyle.hover.textColor = Color.gray;
        toggleStyle.onHover.textColor = Color.gray;

        // Setup style for active toggle.
        // Setting active values for the toggle Style does not work so we create
        // another style.
        GUIStyle activeToggleStyle = new GUIStyle(toggleStyle);
        activeToggleStyle.normal.textColor = Color.white;
        activeToggleStyle.onNormal.textColor = Color.white;
        activeToggleStyle.focused.textColor = Color.white;
        activeToggleStyle.onFocused.textColor = Color.white;
        activeToggleStyle.active.textColor = Color.white;
        activeToggleStyle.onActive.textColor = Color.white;
        activeToggleStyle.hover.textColor = Color.white;
        activeToggleStyle.onHover.textColor = Color.white;


        CameraDevice.FocusMode newMode = setMode;

        // We render the menu over the full screen.
        GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));

        GUILayout.BeginVertical();

        foreach (CameraDevice.FocusMode mode in modes)
        {
            if (mode == setMode)
            {
                GUILayout.Toggle(true, mode.ToString(), activeToggleStyle);
            }
            else
            {
                if (GUILayout.Toggle(false, mode.ToString(), toggleStyle))
                {
                    newMode = mode;
                }
            }
        }

        GUILayout.EndVertical();

        GUILayout.EndArea();

        return newMode;
    }


    /// Compute the coordinates of the menu depending on the current orientation.
    private void computePosition()
    {
        int areaWidth = Screen.width;
        int areaHeight = (Screen.height / 5) * 2;
        int areaLeft = 0;
        int areaTop = Screen.height - areaHeight;
        mAreaRect = new Rect(areaLeft, areaTop, areaWidth, areaHeight);
    }

    #endregion // PRIVATE_METHODS
}

Re: Focusing and Flash on Camera

May 5, 2012 - 3:18pm #8

In the beta version, you could just basically call a method like SetCameraFocus ... is it possible to do the same now?

Re: Focusing and Flash on Camera

May 5, 2012 - 1:05am #7

I still can't quite figure this out! There should only be one instance of the ARcamera... do you just reference that's SetFocusMode ?

Re: Focusing and Flash on Camera

April 14, 2012 - 7:00pm #6

You can set the Flash torch mode and Focus modes using the CameraDevice singleton. This is a thread safe approach.

e.g.

CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO)

See the CameraDeviceMenu script on the ARCamera in the ImageTargets sample for examples of how to manipulate these features.

Re: Focusing and Flash on Camera

April 14, 2012 - 5:30pm #5

I'm not sure how to access the focus feature in the new version of qcar..

can I set this method to static in CameraDevice to be able to just call focus directly from another unity script?

CameraDevice.SetFocusMode(CameraDeviceMode.FOCUS_MODE_TRIGGERAUTO);

Is it possible to control the camera flash?

Re: Focusing and Flash on Camera

July 8, 2011 - 12:59pm #4

Camera Focus does not seem to have the same effect on all devices - for some, it seems the focus needs to be ramped up a few level, especially compared to the focus that the cam's native app can do.. Are there other ways to optimize the focus?

Re: Focusing and Flash on Camera

May 9, 2011 - 1:11am #3

Camera focus is possible in the latest SDK release.
See my thread on how to get it to work in Unity3D:
http://ar.qualcomm.at/node/2000457

Re: Focusing and Flash on Camera

January 15, 2011 - 7:58am #2

There isn't currently, but we have had a few requests for this feature. Hopefully it will be added in a later update.

- Kim

Log in or register to post comments