首页 / 浏览问题 / 移动GIS / 问题详情
使用AR模型浏览的代码,手机不显示场景
30EXP 2023年08月16日


public class ArActivity extends AppCompatActivity {
    private Workspace workspace;
    //工作空间信息连接类
    private WorkspaceConnectionInfo workspaceConnectionInfon;
    //数据路径
    private String sdcard=android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    private String path= sdcard+"/SuperMap/R/R.sxwu";
    //三维空间
    private SceneControl sceneControl;
    //
    private SeekBar seekbarw;
    private SeekBar seekbarh;

    private SeekBar seekbarz;
    private double crossw = 300;
    private double crossh = 300;
    private double crossz = 1;
    private Point3D point3d=null;
    private Point2D point2d=null;
    private Layer3Ds layer3Ds;
    private int layerCount;
    private Layer3DOSGBFile layer3DOSGBFile;
    //三维AR控件
    private ARControl arControl;
    //打开场景标志量
    private boolean isFirst=false;
    //打开场景后再去剖切
    private boolean isOpenScene=false;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollectorUtil.addActivity(ArActivity.this);
        //动态权限
        CameraPermissionHelper.requestCameraPermission(this);
        //全屏显示
        FullScreenHelper.setFullScreenOnWindowFocusChanged(this,true);
        Environment.initialization(this);
        SceneControl.sceneType= SceneType.ST_NONEARTH;
        setContentView(R.layout.activity_ar);
        sceneControl=findViewById(R.id.sceneControl);
        arControl=new ARControl(this,sceneControl);
        seekbarw=findViewById(R.id.seekbar1);
        seekbarh=findViewById(R.id.seekbar2);
        seekbarz=findViewById(R.id.seekbar3);
        sceneControl.sceneControlInitedComplete(new SceneControl.SceneControlInitedCallBackListenner() {
            @Override
            public void onSuccess(String success) {
                //开启AR
                arControl.setARState(true);

            }
        });
        arControl.setAnchorOnClickListener(new ARControl.AnchorOnClickListener() {

            @Override
            public void onSuccess() {
                if(!isFirst && isLicenseAvailable() ){
                    openLocalScene();

                    //设置一个数据放缩,范例数据在室内,默认放缩到0.005,就是室外。
                    arControl.setLayerScale(0,0.005,0.005,0.005);
                    point3d=new Point3D(51.7309421285678 ,16.400098261876 , 11.4698015927305 );
                    isFirst=true;
                }
            }
        });

        //剖切 w设置
        seekbarw.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if(progress==0){
                    return;
                }
                if(!isOpenScene){
                    Toast.makeText(ArActivity.this, "请先识别平面后,击中屏幕中锚点,打开场景后,再去进行剖切", Toast.LENGTH_LONG).show();
                    return;
                }
                crossw=progress/1.0;
                point2d=new Point2D(crossw, crossh);
                for(int i=0;i<layerCount;i++){
                    layer3DOSGBFile=(Layer3DOSGBFile)layer3Ds.get(i);
                    layer3DOSGBFile.setCustomClipCross(point3d, point2d, 0, 0, 0, crossz);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //剖切h设置
        seekbarh.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if(progress==0){
                    return;
                }
                if(!isOpenScene){
                    Toast.makeText(ArActivity.this, "请先识别平面击中屏幕中锚点打开场景后再去进行剖切", Toast.LENGTH_LONG).show();
                    return;
                }
                crossh=progress/1.0;
                point2d=new Point2D(crossw, crossh);
                for(int i=0;i<layerCount;i++){
                    layer3DOSGBFile=(Layer3DOSGBFile)layer3Ds.get(i);
                    layer3DOSGBFile.setCustomClipCross(point3d, point2d, 0, 0, 0, crossz);

                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //剖切z设置
        seekbarz.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if(progress==0){
                    return;
                }
                if(!isOpenScene){
                    Toast.makeText(ArActivity.this, "请先识别平面击中屏幕中锚点打开场景后再去进行剖切", Toast.LENGTH_LONG).show();
                    return;
                }
                crossz=progress/1.1;
                Log.v("lzw","z="+crossz);
                point2d=new Point2D(crossw, crossh);
                for(int i=0;i<layerCount;i++){
                    layer3DOSGBFile=(Layer3DOSGBFile)layer3Ds.get(i);
                    layer3DOSGBFile.setCustomClipCross(point3d, point2d, 0, 0, 0, crossz);

                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
    //打开本地场景
    private  void openLocalScene() {
        if (workspace == null) {
            workspace = new Workspace();
            workspaceConnectionInfon = new WorkspaceConnectionInfo();
        }
        workspaceConnectionInfon.setServer(path);
        workspaceConnectionInfon.setType(WorkspaceType.SXWU);
        sceneControl.getScene().setWorkspace(workspace);
        if (workspace.open(workspaceConnectionInfon)){
            sceneControl.getScene().open(workspace.getScenes().get(0));
            layerCount=sceneControl.getScene().getLayers().getCount();
            layer3Ds=sceneControl.getScene().getLayers();
            isOpenScene=true;
        }
    }

    // 判断许可是否可用
    private boolean isLicenseAvailable() {
        LicenseStatus licenseStatus = Environment.getLicenseStatus();
        if (!licenseStatus.isLicenseExsit()) {
            Toast.makeText(ArActivity.this, "许可不存在,场景打开失败,请加入许可", Toast.LENGTH_LONG).show();
            return false;
        } else if (!licenseStatus.isLicenseValid()) {
            Toast.makeText(ArActivity.this, "许可过期,场景打开失败,请更换有效许可", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollectorUtil.removeActivity(ArActivity.this);
    }
}

1个回答

您好,如果是黑屏请确认下手机是否给到应用相机调用权限,如果相机权限也给了,建议查看下手机是否支持ar,具体支持列表可以参考该地址

最全ARCore支持的设备(手机和平板电脑的详细列表)实时更新_arcore支持机型_AIRX三次方的博客-CSDN博客

3,685EXP 2023年08月16日
...