从其他活动导航时不显示相机预览和 AR 项目

发布于 2024-10-31 08:14:16 字数 2294 浏览 2 评论 0原文

我正在尝试开发一个用于培训目的的 AR 应用程序。 有一个主要活动,其中包含具有两个视图的 FrameLayout:相机预览(实现 SurfaceHolder.Callback 的 SurfaceView)和另一个用于在相机预览上绘制文本的自定义表面。 此 FrameLayout 是在 MainActivity 的 onCreate 方法中创建的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    try{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Panel for AR items
        **panelSurface = new PanelSurface(this);**

        WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        int width = d.getWidth();
        int height = d.getHeight();
        panelSurface.setScreenSize(width, height);


        loadARItems();            

         //General frame
        FrameLayout rl = new FrameLayout(this);
         //Camera Surface
        **cv = new CustomCameraView(this);**
        rl.addView(cv, width, height);
        rl.addView(panelSurface, width, height);
        setContentView(rl);

        //Initialize sensors
        sensorMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    }
    catch(Exception e){
        Log.e(TAG, "Error creating main activity: " + e.getMessage());
    }
}

如果此活动是第一个被调用的活动:相机预览会正确显示,上面有我的 AR 项目。 然而,当我来自另一项活动时,问题出现了: 有一个包含多个按钮的菜单活动。其中一个按钮将调用主活动:

private OnClickListener mapListener = new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(MenuActivity.this, MainActivity.class);
        startActivity(intent);          
    }
}; 

从另一个活动导航时,不会显示相机!屏幕保持黑色。 AR 项目被绘制了一次,但没有刷新,它们被再次绘制,绘制所有屏幕,而之前没有清理它来显示运动!

为什么具有相同代码的相同活动在从另一个活动调用时无法正常工作???

调试我检查过的两种情况: 当MainActivity是第一个Activity时,日志: 04-08 13:43:03.498:INFO/ActivityManager(85):显示的活动 com.lagunitacrew.activities/.MainActivity:929 毫秒(总计 929 毫秒) 打开相机前会显示。 然而,在第二种情况下,当从菜单活动调用MainActivity时,这个跟踪是在打开相机后写入的

非常感谢!

I'm trying to develop an AR application for training purposes.
There is a main activity that contains a FrameLayout with two views: the camera preview (a SurfaceView implementing the SurfaceHolder.Callback) and another custom surface for drawing a text over the camera preview.
This FrameLayout is created within MainActivity's onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    try{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Panel for AR items
        **panelSurface = new PanelSurface(this);**

        WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        int width = d.getWidth();
        int height = d.getHeight();
        panelSurface.setScreenSize(width, height);


        loadARItems();            

         //General frame
        FrameLayout rl = new FrameLayout(this);
         //Camera Surface
        **cv = new CustomCameraView(this);**
        rl.addView(cv, width, height);
        rl.addView(panelSurface, width, height);
        setContentView(rl);

        //Initialize sensors
        sensorMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    }
    catch(Exception e){
        Log.e(TAG, "Error creating main activity: " + e.getMessage());
    }
}

If this activity is the first activity to be called: the camera preview is shown correctly with my AR Items over it.
Nevertheless, the problem appears when I come from another activity:
There is a menu activity that contains several buttons. One of this buttons will call to the Main Activity:

private OnClickListener mapListener = new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(MenuActivity.this, MainActivity.class);
        startActivity(intent);          
    }
}; 

When navigating from another activity, the camera is not shown!! The screen remains black.
And the AR items are drawn one time but aren't refreshed, they are drawn again painting all the screen whitout cleaning it before for showing the movement!

Why the same activity with the same code is not working correctly when it's called from another activity???

Debugging both scenarios I have checked:
When MainActivity is the first Activity, the log:
04-08 13:43:03.498: INFO/ActivityManager(85): Displayed activity com.lagunitacrew.activities/.MainActivity: 929 ms (total 929 ms)
is shown before opening the camera.
However, in the second scenario, when the MainActivity is called from the Menu activity, this traced is written after opening the camera

Many thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-11-07 08:14:16

当您的 Activity 转到后台时,您是否会尝试破坏当前屏幕?我认为问题是,当你返回时,camera.open() 函数不起作用。我建议你编写一个函数来在应用程序进入后台时销毁UI,并在onPause函数中调用该函数。

When your activity goes to the background, do you try to destroy the current screen? I think that the problem is camera.open() function doesn't work when you go back. I suggest that you will write a function to destroy UI when the application goes to the background and call this function in onPause function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文