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

文章:

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

效果:预览并渲染数据

预览并渲染数据.gif

目录:

1. 前序

本篇文章较长,目的是能够在手机中预览数据。但愿读者可以认真读下去。无论你是刚接触ARCore以及OpenGL的 程序员。仍是立刻要毕业,把该项目拿来用做毕设的学生。我相信只要你能坚持下去,必定会对你有所帮助。 该文章代码较多。部分解释都集中在代码的注释中。在最后还有项目的GitHub地址。但愿你们耐着性子看下去。我会尽可能写的详细,但愿能帮到你们。固然也算是本身对知识的一种积累。php

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类java

/** * 权限帮助类 */
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中作相应的权限申请操做android

@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最核心的一个类。他能够获取相机数据。并算出相应的锚点信息以及视矩阵投影矩阵等。这里的部份内容会在后面进行具体叙述。git

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绘制。github

2. GLSurfaceView

xml布局session

<?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>
复制代码

初始化app

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接口的三个回调方法ide

/** * 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的知识会很难。估计我要写一阵了。若是有大神看到这篇文章中的错误,烦请及时指出。但愿你们能共同进步,各位感兴趣的加油吧!

相关文章
相关标签/搜索