美文网首页
Android 集成百度地图AR识别SDK(三)

Android 集成百度地图AR识别SDK(三)

作者: JYangkai | 来源:发表于2019-03-06 22:05 被阅读0次

    废话

    今天我们接着上一章的内容继续讲如何集成百度地图AR SDK

    上一章我讲到了配置MyApplication,那么这一章我就讲如何显示AR目标点,废话不多说,直接上代码。

    创建布局

    activity_ar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:id="@+id/ar_camera_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <RelativeLayout
            android:id="@+id/ar_poi_item_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>
    </RelativeLayout>
    

    可以看到AR的布局比较简单,里面就是两个RelativeLayout,一个用于显示相机,一个用于显示POI点

    创建完成之后我们还需创建一个FindArCamGLView,待会会用到

    layout_find_camera_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <map.baidu.ar.camera.find.FindArCamGLView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"/>
    

    创建Activity

    public class ARActivity extends AppCompatActivity implements ArPageListener {
    
        private RelativeLayout mCameraLayout;
    
        private RelativeLayout mArPoiItemLayout;
    
        private FindArCamGLView mCameraGLView;
    
        private SimpleSensor mSensor;
    
        private List<PoiInfoImpl> mPoiInfoList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ar);
            fbv();
            init();
            event();
        }
    }
    

    我们先重写onCreate方法,在里面做一些初始化操作

    fbv

    private void fbv() {
        mCameraLayout = findViewById(R.id.ar_camera_layout);
        mArPoiItemLayout = findViewById(R.id.ar_poi_item_layout);
        mCameraGLView = (FindArCamGLView) LayoutInflater.from(this).inflate(R.layout.layout_find_camera_view, null);
    }
    

    这个获取控件

    init

    private void init() {
        initData();
        initCamera();
        initSensor();
    }
    

    初始化各个功能

    initData
    private void initData(){
        mPoiInfoList= new ArrayList<>();
        ArLatLng arLatLng=new ArLatLng(22.8750032283,113.8906799763);
        ArPoiInfo poiInfo=new ArPoiInfo();
        poiInfo.name="测试AR点";
        poiInfo.location=arLatLng;
        PoiInfoImpl poiImpl=new PoiInfoImpl();
        poiImpl.setPoiInfo(poiInfo);
        mPoiInfoList.add(poiImpl);
    }
    

    这个是初始化数据点,也就是你要显示的AR目标点

    initCamera
    private void initCamera() {
        mCameraGLView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom == 0 || oldBottom == 0 || mCameraGLView == null) {
                    return;
                }
                RelativeLayout.LayoutParams params = TypeUtils.safeCast(
                        mCameraGLView.getLayoutParams(), RelativeLayout.LayoutParams.class);
                if (params == null) {
                    return;
                }
                params.height = bottom - top;
                mCameraGLView.requestLayout();
            }
        });
        mCameraLayout.addView(mCameraGLView);
    }
    

    这个是初始化相机

    initSensor
    private void initSensor() {
        if (mSensor == null) {
            mSensor = new SimpleSensor(this, new HoldPositionListenerImp());
        }
        mSensor.startSensor();
    }
    
    private class HoldPositionListenerImp implements SimpleSensor.OnHoldPositionListener {
        @Override
        public void onOrientationWithRemap(float[] remapValue) {
            if (mCameraGLView != null && mArPoiItemLayout != null) {
                if (mPoiInfoList.size() <= 0) {
                    mArPoiItemLayout.setVisibility(View.GONE);
                } else {
                    mCameraGLView.setFindArSensorState(remapValue, getLayoutInflater(),
                            mArPoiItemLayout, ARActivity.this, (ArrayList<PoiInfoImpl>) mPoiInfoList, ARActivity.this);
                    mArPoiItemLayout.setVisibility(View.VISIBLE);
                }
            }}
    }
    

    event

    private void event() {
    }
    

    暂时不需要绑定事件,这个方法先空着

    其他的方法

    @Override
    public void noPoiInScreen(boolean b) {
    }
    @Override
    public void selectItem(Object o) {
    }
    private void finishCamInternal(){
        if (mCameraGLView != null) {
            mCameraGLView.stopCam();
            mCameraLayout.removeAllViews();
            mCameraGLView = null;
        }
        if (mArPoiItemLayout != null) {
            mArPoiItemLayout.removeAllViews();
        }
        if (mSensor != null) {
            mSensor.stopSensor();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        finishCamInternal();
    }
    

    finishCamInternal是销毁时调用的,移除视图,防止内存泄漏等问题
    下面是ARActivity的全部源码

    ARActivity.java

    package com.yk.mchat.app.activity;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.RelativeLayout;
    
    import com.yk.mchat.R;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import map.baidu.ar.ArPageListener;
    import map.baidu.ar.camera.SimpleSensor;
    import map.baidu.ar.camera.find.FindArCamGLView;
    import map.baidu.ar.model.ArLatLng;
    import map.baidu.ar.model.ArPoiInfo;
    import map.baidu.ar.model.PoiInfoImpl;
    import map.baidu.ar.utils.TypeUtils;
    
    public class ARActivity extends AppCompatActivity implements ArPageListener {
    
        private RelativeLayout mCameraLayout;
    
        private RelativeLayout mArPoiItemLayout;
    
        private FindArCamGLView mCameraGLView;
    
        private SimpleSensor mSensor;
    
        private List<PoiInfoImpl> mPoiInfoList;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ar);
            fbv();
            init();
            event();
        }
    
        private void fbv() {
            mCameraLayout = findViewById(R.id.ar_camera_layout);
            mArPoiItemLayout = findViewById(R.id.ar_poi_item_layout);
            mCameraGLView = (FindArCamGLView) LayoutInflater.from(this).inflate(R.layout.layout_find_camera_view, null);
        }
    
        private void init() {
            initData();
            initCamera();
            initSensor();
        }
    
        private void initData(){
            mPoiInfoList= new ArrayList<>();
    
            ArLatLng arLatLng=new ArLatLng(22.8750032283,113.8906799763);
            ArPoiInfo poiInfo=new ArPoiInfo();
            poiInfo.name="测试AR点";
            poiInfo.location=arLatLng;
    
            PoiInfoImpl poiImpl=new PoiInfoImpl();
            poiImpl.setPoiInfo(poiInfo);
    
            mPoiInfoList.add(poiImpl);
        }
    
        private void initCamera() {
            mCameraGLView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View view, int left, int top, int right, int bottom,
                                           int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    if (bottom == 0 || oldBottom == 0 || mCameraGLView == null) {
                        return;
                    }
                    RelativeLayout.LayoutParams params = TypeUtils.safeCast(
                            mCameraGLView.getLayoutParams(), RelativeLayout.LayoutParams.class);
                    if (params == null) {
                        return;
                    }
                    params.height = bottom - top;
                    mCameraGLView.requestLayout();
                }
            });
            mCameraLayout.addView(mCameraGLView);
        }
    
        private void initSensor() {
            if (mSensor == null) {
                mSensor = new SimpleSensor(this, new HoldPositionListenerImp());
            }
            mSensor.startSensor();
        }
    
        private void event() {
    
        }
    
        private class HoldPositionListenerImp implements SimpleSensor.OnHoldPositionListener {
    
            @Override
            public void onOrientationWithRemap(float[] remapValue) {
                if (mCameraGLView != null && mArPoiItemLayout != null) {
                    if (mPoiInfoList.size() <= 0) {
                        mArPoiItemLayout.setVisibility(View.GONE);
                    } else {
                        mCameraGLView.setFindArSensorState(remapValue, getLayoutInflater(),
                                mArPoiItemLayout, ARActivity.this, (ArrayList<PoiInfoImpl>) mPoiInfoList, ARActivity.this);
                        mArPoiItemLayout.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    
        public LayoutInflater getLayoutInflater() {
            return LayoutInflater.from(ARActivity.this).cloneInContext(ARActivity.this);
        }
    
        @Override
        public void noPoiInScreen(boolean b) {
    
        }
    
        @Override
        public void selectItem(Object o) {
    
        }
    
        private void finishCamInternal(){
            if (mCameraGLView != null) {
                mCameraGLView.stopCam();
                mCameraLayout.removeAllViews();
                mCameraGLView = null;
            }
    
            if (mArPoiItemLayout != null) {
                mArPoiItemLayout.removeAllViews();
            }
    
            if (mSensor != null) {
                mSensor.stopSensor();
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            finishCamInternal();
        }
    }
    

    这个就是集成百度地图AR识别SDK的全部内容,如果有不懂的或者是出问题的,可以评论私信我,希望大家能够喜欢。

    相关文章

      网友评论

          本文标题:Android 集成百度地图AR识别SDK(三)

          本文链接:https://www.haomeiwen.com/subject/nirfpqtx.html