Kiran George
Kiran George

Reputation: 63

How to use SurfaceView in ARCore to get the Pose of the camera

I'm trying to create a camera application which saves the camera position along with each image captured. I'm new to Android Studio and ARCore.

I created a camera app using Android Studio tutorial, then tried to use the Pose class inside the app. But it gives me an error saying that it requires and OpenGL context. I then realized that I have to use SurfaceView instead of PreviewView for Pose to work. But I don't get how to use it properly even after reading the documentation. Please help me out.

Upvotes: 1

Views: 641

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58553

I've found an example demonstrating ARCore texture's reading app that implements GLSurfaceView and its renderer. Due to the fact that the code takes almost 300 lines, I'm posting a link to it.

class MainActivity extends AppCompatActivity implements GLSurfaceView.Renderer {

    private GLSurfaceView surfaceView;
    private Session session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = (GLSurfaceView) findViewById(R.id.surfaceview);

        // some code...

        surfaceView.setRenderer(this);
        surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (session == null) {
            if (!CameraPermissionHelper.hasCameraPermission(this)) {
                CameraPermissionHelper.requestCameraPermission(this);
                return;
            }
            session = new Session(/* some context= */ this);
        }
    }
}

Upvotes: 1

Related Questions