手把手带你写AR应用--AR尺子预览

3,922 阅读6分钟

文章:

  1. AR尺子--简介
  2. AR尺子--预览

效果:预览并渲染数据

预览并渲染数据.gif

目录:

1. 前序

本篇文章较长,目的是可以在手机中预览数据。希望读者能够认真读下去。不管你是刚接触ARCore以及OpenGL的 程序员。还是马上要毕业,把该项目拿来用作毕设的学生。我相信只要你能坚持下去,一定会对你有所帮助。 该文章代码较多。部分解释都集中在代码的注释中。在最后还有项目的GitHub地址。希望大家耐着性子看下去。我会尽量写的详细,希望能帮到大家。当然也算是自己对知识的一种积累。

2. 集成ARCore

1. 向 manifest 添加 AR 选项。并声明Camera权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.jtl.arcoredemo">
          
    //声明Camera权限
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        
        //添加AR选项,在启动应用时会判断是否安装了ARCore
        <meta-data android:name="com.google.ar.core" android:value="required" />
    </application>

</manifest>

2. 添加依赖库
android {
    ...
    //要求JDK 1.8
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
//在APP Gradle中添加ARCore的依赖库(截至2019.5.4,最新版本为1.8.0)
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation "com.google.ar.sceneform:core:1.8.0"
}
3. 请求摄像机权限

这里引入一个PermissionHelper类

/**
 * 权限帮助类
 */
public final class PermissionHelper {
    private static final int CAMERA_PERMISSION_CODE = 0;
    private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;

    /**
     * 是否有相机权限
     * @param activity
     * @return
     */
    public static boolean hasCameraPermission(Activity activity) {
        return ContextCompat.checkSelfPermission(activity, CAMERA_PERMISSION)
                == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 请求相机权限
     * @param activity
     */
    public static void requestCameraPermission(Activity activity) {
        ActivityCompat.requestPermissions(
                activity, new String[]{CAMERA_PERMISSION}, CAMERA_PERMISSION_CODE);
    }
    
    /**
     * 展示申请权限的相应解释
     * @param activity
     * @return
     */
    public static boolean shouldShowRequestPermissionRationale(Activity activity) {
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, CAMERA_PERMISSION);
    }

    /**
     * 打开设置界面
     *
     * @param activity
     */
    public static void launchPermissionSettings(Activity activity) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        activity.startActivity(intent);
    }
    
    
}

在Activity中做相应的权限申请操作

    @Override
    protected void onResume() {
        super.onResume();
        // ARCore 申请相机权限操作
        if (!PermissionHelper.hasCameraPermission(this)) {
            PermissionHelper.requestCameraPermission(this);
            return;
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (!PermissionHelper.hasCameraPermission(this)) {
            Toast.makeText(this, "该应用需要相机权限", Toast.LENGTH_LONG)
                    .show();
            //弹出相应解释
            if (!PermissionHelper.shouldShowRequestPermissionRationale(this)) {
                // 直接跳至设置 修改权限
                PermissionHelper.launchPermissionSettings(this);
            }
            finish();
        }
    }

3. 初识Session

1. 什么是Session:

Session也称为会话,是ARCore最核心的一个类。他可以获取相机数据。并算出相应的锚点信息以及视矩阵投影矩阵等。这里的部分内容会在后面进行具体叙述。

2. 初始化Session

首先判断设备是否支持ARCore

@Override
    protected void onResume() {
        super.onResume();
        // ARCore 申请相机权限操作
        ...
        
        Exception exception =null;
        String msg =null;
        //初始化Session
        if (mSession==null){
            try {
                //判断是否安装ARCore
                switch (ArCoreApk.getInstance().requestInstall(this,!isInstallRequested)){
                    case INSTALL_REQUESTED:
                        isInstallRequested=true;
                        break;
                    case INSTALLED:
                        Log.i(TAG,"已安装ARCore");
                        break;
                }
                mSession=new Session(this);
            } catch (UnavailableArcoreNotInstalledException
                    | UnavailableUserDeclinedInstallationException e) {
                msg = "Please install ARCore";
                exception = e;
            } catch (UnavailableApkTooOldException e) {
                msg = "Please update ARCore";
                exception = e;
            } catch (UnavailableSdkTooOldException e) {
                msg = "Please update this app";
                exception = e;
            } catch (UnavailableDeviceNotCompatibleException e) {
                msg = "This device does not support AR";
                exception = e;
            } catch (Exception e) {
                msg = "Failed to create AR session";
                exception = e;
            }
            //有异常说明不支持或者没安装ARCore
            if (msg != null) {
                Log.e(TAG, "Exception creating session", exception);
                return;
            }
        }
        //该设备支持并且已安装ARCore
        try {
            //Session 恢复resume状态
            mSession.resume();
        } catch (CameraNotAvailableException e) {
            Log.e(TAG, "Camera not available. Please restart the app.");
            mSession = null;
            
            return;
        }
    }
3. 开始或恢复Session(会话)
@Override
    protected void onResume() {
        super.onResume();
        // ARCore 申请相机权限操作
        ...

        Exception exception =null;
        String msg =null;
        //初始化Session
        if (mSession==null){
            //判断是否支持ARCore
            ...
        }
        
        //该设备支持并且已安装ARCore
        try {
            //Session 恢复resume状态
            mSession.resume();
        } catch (CameraNotAvailableException e) {
            Log.e(TAG, "Camera not available. Please restart the app.");
            mSession = null;
            
            return;
        }
    }
4. 暂停关闭Session(会话)
    @Override
    protected void onPause() {
        super.onPause();
        if (mSession!=null){
            mSession.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSession!=null){
            mSession.close();
        }
    }

4.OpenGL渲染

1. 简单介绍:

首先这里只是简单介绍一下OpenGL,具体的会在后面进行具体叙述。 OpenGL是一个渲染协议。很多渲染引擎底层就是用OpenGL实现的。现在的移动手机都是用的OpenGL ES2.0,几乎涵盖了所有的苹果和Android手机。Android上有个叫做GLSurfaceView的控件。就是Google已经封装好的一个渲染控件。它的底层API都是Google 封装好的native方法,也就是俗称的JNI方法。他需要实现一个Render接口。这个接口有三个回调方法。每一个GLSurfaceView都会有一个相对应的GL线程,专门用来绘制。每个GL线程都有相应的resume和pause方法。用来resume和pause。

2. GLSurfaceView

xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.opengl.GLSurfaceView
        android:id="@+id/gl_main_show"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

初始化

public class MainActivity extends AppCompatActivity implements GLSurfaceView.Renderer {
    //用来使Session能够根据手机横竖屏,输出相应分辨率的数据
    private DisplayRotationHelper mDisplayRotationHelper;
    private GLSurfaceView mShowGLSurface;
    
    //初始化相应数据
    private void initData(){
        mShowGLSurface=findViewById(R.id.gl_main_show);
        mDisplayRotationHelper=new DisplayRotationHelper(this);

        // Set up renderer.
        mShowGLSurface.setPreserveEGLContextOnPause(true);
        mShowGLSurface.setEGLContextClientVersion(2);//OpenGL版本为2.0
        mShowGLSurface.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending.
        mShowGLSurface.setRenderer(this);//实现Render接口
        mShowGLSurface.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//RENDERMODE_CONTINUOUSLY渲染模式为实时渲染。
    }
}

实现 Render接口的三个回调方法

    /**
     * GLSurfaceView创建时被回调,可以做一些初始化操作
     * @param gl
     * @param config
     */
     
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置每一帧清屏颜色 传入参输为RGBA
        GLES20.glClearColor(0.1f,0.1f,0.1f,1.0f);
    }

    /**
     * GLSurfaceView 大小改变时调用
     * @param gl
     * @param width GLSurfaceView宽
     * @param height GLSurfaceView高
     */
     
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //改变视口 方便 OpenGLES做 视口变换
        GLES20.glViewport(0,0,width,height);
    }

    /**
     * GLSurfaceView绘制每一帧调用,此处不在主线程中,而是在GL线程中。
     * 部分跨线程数据,需要做线程同步。不能直接更新UI(不在主线程)
     * @param gl
     */
     
    @Override
    public void onDrawFrame(GL10 gl) {
        //清空彩色缓冲和深度缓冲  清空后的颜色为GLES20.glClearColor()时设置的颜色
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
    }

GLSurfaceView的 resume和pause

@Override
    protected void onResume() {
        super.onResume();
        //ARCore的相应初始化操作
        ...
        
        //GLSurfaceView onResume
        mShowGLSurface.onResume();
        mDisplayRotationHelper.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        if (mSession!=null){
            //由于GLSurfaceView需要Session的数据。所以如果Session先pause会导致无法获取Session中的数据
            mShowGLSurface.onPause();//GLSurfaceView onPause
            mDisplayRotationHelper.onPause();
            mSession.pause();
        }
    }

3. 渲染数据

这里引入了一个BackgroundRenderer。它才是抽离出来的真正的用来渲染的类。具体写法以及用途将在下一章介绍。

  private BackgroundRenderer mBackgroundRenderer;
    /**
     * GLSurfaceView创建时被回调,可以做一些初始化操作
     * @param gl
     * @param config
     */
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置每一帧清屏颜色 传入参输为RGBA
        GLES20.glClearColor(0.1f,0.1f,0.1f,1.0f);

        mBackgroundRenderer=new BackgroundRenderer();
        mBackgroundRenderer.createOnGlThread(this);
    }

    /**
     * GLSurfaceView 大小改变时调用
     * @param gl
     * @param width GLSurfaceView宽
     * @param height GLSurfaceView高
     */
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //方便 OpenGLES做 视口变换
        GLES20.glViewport(0,0,width,height);
        mDisplayRotationHelper.onSurfaceChanged(width,height);
    }

    /**
     * GLSurfaceView绘制每一帧调用,此处不在主线程中,而是在GL线程中。
     * 部分跨线程数据,需要做线程同步。不能直接更新UI(不在主线程)
     * @param gl
     */
    @Override
    public void onDrawFrame(GL10 gl) {
        //清空彩色缓冲和深度缓冲  清空后的颜色为GLES20.glClearColor()时设置的颜色
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
        if (mSession==null){
            return;
        }
        //设置纹理ID
        mSession.setCameraTextureName(mBackgroundRenderer.getTextureId());
        //根据设备渲染Rotation,width,height。session.setDisplayGeometry(displayRotation, viewportWidth, viewportHeight);
        mDisplayRotationHelper.updateSessionIfNeeded(mSession);
        try {
            Frame frame=mSession.update();//获取Frame数据
            mBackgroundRenderer.draw(frame);//渲染frame数据
        } catch (CameraNotAvailableException e) {
            e.printStackTrace();
        }
    }

5. GitHub地址

  1. ARRuler 本教程版本
  2. ARRuler 已完成版
  3. 简书地址
  4. 掘金地址

6. 后续:

这里的ARCore调用逻辑,来源Google官方的ARCore示例。 渲染部分的代码,有的没说清的,后续都会补上。OpenGL的知识会很难。估计我要写一阵了。如果有大神看到这篇文章中的错误,烦请及时指出。希望大家能共同进步,各位感兴趣的加油吧!