随着AR的发展和电子设备的普及,人们在生活中使用AR技术的门槛降低,比如对于不方便测量的物体使用AR测量,方便又准确;遇到陌生的路段使用AR导航,清楚又便捷;网购时拿不准的物品使用AR购物,体验更逼真。
想要让虚拟物体和现实世界相融合,重要的一步就是将虚拟对象准确放置在现实场景中,当用户触摸电子屏幕上的任意地方时即可创建AR对象,达到良好的交互体验。
华为HMS Core AR Engine持续跟踪设备相对于周围环境的位置和姿态变化轨迹,建立虚拟数字世界和现实物理世界的统一几何空间,为您的应用提供虚实融合的交互基础平台。其中命中检测技术让用户可通过点击终端设备屏幕选中现实环境中的兴趣点,终端设备屏幕上的兴趣点映射为现实环境中的兴趣点,并以兴趣点为源发出一条射线连接到摄像头所在位置,返回这条射线贯穿的任何平面或特征点以及交叉位置在现实世界空间中的位置和姿态。命中检测与平面碰撞,获得碰撞点的位置及法向量,让用户可以自由选择环境中的物体或者与它们互动。

开发环境要求:
JDK 1.8.211及以上。
安装Android Studio 3.0及以上:
minSdkVersion 26及以上
targetSdkVersion 29(推荐)
compileSdkVersion 29(推荐)
Gradle 6.1.1及以上(推荐)
在华为终端设备上的应用市场下载AR Engine服务端APK(需在华为应用市场,搜索“华为AR Engine”)并安装到终端设备。
测试应用的设备:参见AREngine特性软硬件依赖表中环境Mesh支持设备列表。如果同时使用多个HMS Core的服务,则需要使用各个Kit对应的最大值。
华为提供了Maven仓集成方式的AR Engine SDK包,在开始开发前,需要将AR Engine SDK集成到您的开发环境中。
Android Studio的代码库配置在Gradle插件7.0以下版本、7.0版本和7.1及以上版本有所不同。请根据您当前的Gradle插件版本,选择对应的配置过程。
以7.0为例:
打开Android Studio项目级“build.gradle”文件,添加Maven代码库。
在“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。
1 2 3 4 5 6 7 | buildscript { repositories { google() jcenter() maven {url "8c9K9s2c8@1M7s2y4Q4x3@1q4Q4x3V1k6Q4x3V1k6V1k6i4k6W2L8r3!0H3k6i4u0Q4x3X3g2Z5N6h3q4%4k6h3W2Q4x3X3g2U0L8$3#2Q4x3V1k6J5k6i4m8G2i4K6u0r3" } }} |
打开项目级“settings.gradle”文件,配置HMS Core SDK的Maven仓地址
1 2 3 4 5 6 7 8 9 10 | dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { repositories { google() jcenter() maven {url "45fK9s2c8@1M7s2y4Q4x3@1q4Q4x3V1k6Q4x3V1k6V1k6i4k6W2L8r3!0H3k6i4u0Q4x3X3g2Z5N6h3q4%4k6h3W2Q4x3X3g2U0L8$3#2Q4x3V1k6J5k6i4m8G2i4K6u0r3" } } }} |
1 2 3 | dependencies { implementation 'com.huawei.hms:arenginesdk:{version}} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public class WorldRenderManager implementsGLSurfaceView.Renderer{ //此方法构造函数传递上下文 public WorldRenderManager(Activity activity, Context context) { mActivity = activity; mContext = context; …}//此方法设置ARSession,它将更新并获取OnDrawFrame中的最新数据。public void setArSession(ARSession arSession) { if (arSession == null) { LogUtil.error(TAG, "setSession error, arSession is null!"); return; } mSession = arSession; }//设置ARWorldTrackingConfig,获取配置模式。public void setArWorldTrackingConfig(ARWorldTrackingConfig arConfig) { if (arConfig == null) {LogUtil.error(TAG, "setArWorldTrackingConfig error, arConfig is null!"); return; } mArWorldTrackingConfig = arConfig; }//实现onDrawFrame方法@Overridepublic void onDrawFrame(GL10 unused) { mSession.setCameraTextureName(mTextureDisplay.getExternalTextureId());ARFrame arFrame = mSession.update();ARCamera arCamera = arFrame.getCamera();…….}//命中结果输出private ARHitResult hitTest4Result(ARFrame frame, ARCamera camera, MotionEvent event) { ARHitResult hitResult = null; List<ARHitResult> hitTestResults = frame.hitTest(event);//确定命中点是否在平面多边形内。ARHitResult hitResultTemp = hitTestResults.get(i); if (hitResultTemp == null) { continue; }ARTrackable trackable = hitResultTemp.getTrackable(); //确定点云是否被单击,以及点是否面向相机。 boolean isPointHitJudge = trackable instanceof ARPoint&& ((ARPoint) trackable).getOrientationMode() == ARPoint.OrientationMode.ESTIMATED_SURFACE_NORMAL;//优先选择平面上的点。if (isPlanHitJudge || isPointHitJudge) { hitResult = hitResultTemp; if (trackable instanceof ARPlane) { break; } }return hitResult;}} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | public class WorldActivity extends BaseActivity { private ARSession mArSession;private GLSurfaceView mSurfaceView;private ARWorldTrackingConfig mConfig;@Overrideprotected void onCreate(Bundle savedInstanceState) { LogUtil.info(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.world_java_activity_main); mWorldRenderManager = new WorldRenderManager(this, this);mWorldRenderManager.setDisplayRotationManage(mDisplayRotationManager);mWorldRenderManager.setQueuedSingleTaps(mQueuedSingleTaps) }@Overrideprotected void onResume() { if (!PermissionManager.hasPermission(this)) { this.finish(); } errorMessage = null; if (mArSession == null) { try { if (!arEngineAbilityCheck()) { finish(); return; } mArSession = new ARSession(this.getApplicationContext()); mConfig = new ARWorldTrackingConfig(mArSession); refreshConfig(ARConfigBase.LIGHT_MODE_ENVIRONMENT_LIGHTING | ARConfigBase.LIGHT_MODE_ENVIRONMENT_TEXTURE); } catch (Exception capturedException) { setMessageWhenError(capturedException); } if (errorMessage != null) { stopArSession(); return; }}@Override protected void onPause() { LogUtil.info(TAG, "onPause start."); super.onPause(); if (mArSession != null) { mDisplayRotationManager.unregisterDisplayListener(); mSurfaceView.onPause(); mArSession.pause(); } LogUtil.info(TAG, "onPause end."); }@Override protected void onDestroy() { LogUtil.info(TAG, "onDestroy start."); if (mArSession != null) { mArSession.stop(); mArSession = null; } if (mWorldRenderManager != null) { mWorldRenderManager.releaseARAnchor(); } super.onDestroy(); LogUtil.info(TAG, "onDestroy end."); }…..} |
了解更多详情>>
访问华为开发者联盟官网
获取开发指导文档
华为移动服务开源仓库地址:GitHub、Gitee
关注我们,第一时间了解 HMS Core 最新技术资讯~