Android 手机相机自动对焦

Android上相机的自动对焦,挂在场景中的相机上即可

using System.Collections;

using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 自动对焦
/// </summary>
public class AutoFocus : MonoBehaviour
{
    private bool autoFoucusSet;


    private void Awake()
    {
        autoFoucusSet = false;
    }
    public static bool EnableAutoFocus()
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaClass metaioSDKAndroid = new AndroidJavaClass("com.metaio.sdk.jni.IMetaioSDKAndroid");
        object[] args = { currentActivity };
        AndroidJavaObject camera = metaioSDKAndroid.CallStatic<AndroidJavaObject>("getCamera", args);
        if (camera != null)
        {
            AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
            object[] focusMode = { cameraParameters.GetStatic<string>("FOCUS_MODE_CONTINUOUS_PICTURE") };
            cameraParameters.Call("setFocusMode", focusMode);
            object[] newParameters = { cameraParameters };
            camera.Call("setParameters", newParameters);
            return true;
        }
        else
        {
            return false;
        }
    }
void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))
        {
            Application.Quit();
        }
        if(Time.time>1f&&!autoFoucusSet)
        {
            autoFoucusSet = EnableAutoFocus();
        }
}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36215526/article/details/79973439
个人分类: Unity
上一篇二维码的生成与识别
想对作者说点什么? 我来说一句

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

关闭
关闭