Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android overlaying GLSurfaceView with camera's SurfaceView

I'm looking to put an openGL model (via GLSurfaceView) on top of the camera, which has its own SurfaceView. Surely this must be possible, apps like Layar do this.

Here is what I am trying:

public class MainActivity extends Activity {


    private CameraSurfaceView mCameraSurfaceView; 
    private GLSurfaceView mCubeGLSurfaceView; 
    private FrameLayout mFrameLayout; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mFrameLayout = new FrameLayout(this); 

        mCameraSurfaceView = new CameraSurfaceView(this);

        mCubeGLSurfaceView = new GLSurfaceView(this);
        mCubeGLSurfaceView.setRenderer(new CubeRenderer()); 
        //mCubeGLSurfaceView.setZOrderMediaOverlay(true);

        mFrameLayout.addView(mCameraSurfaceView); 
        mFrameLayout.addView(mCubeGLSurfaceView); 

        setContentView(mFrameLayout); 

    }

}

Right now: just the camera shows up. I've read online about the SetZOrderMediaOverlay attribute for GLSurfaceView; setting it TRUE in the above code has the 3D cube show up with a black background (i.e. no camera preview shown).

References:

  • Here is the Cube OpenGL code I'm using: http://intransitione.com/blog/create-a-spinning-cube-with-opengl-es-and-android/
  • Here is the CameraSurfaceView code: https://github.com/davefp/android-camera-api-example/blob/master/src/com/example/cameraexample/CameraSurfaceView.java

Other tests I've tried: changing the addView order of the respective surfaceviews doesn't help; it only shows the camera (I read somewhere changing the addView order may help).

Thanks for any help!

like image 645
JDS Avatar asked Jul 09 '26 09:07

JDS


1 Answers

Since you state that only the camera preview is currently visible it may be that you have not ordered your views correctly. This is what I have done to create what you desire.

In the Activity create instance of MySurfaceView derived from GLSurfaceView instance of MyCameraPreview derived from SurfaceView

in my Activity's onCreate method I do the following:

mySurfaceView = new MySurfaceView(this);
addContentView(mySurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
myCameraPreview = new MyCameraPreview(this, myCamera);
addContentView(myCameraPreview, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Please note that using this method you do not use a layout xml.

This will create your two views on top of each other with the camera preview in the background.

Now you need to get the MySurfaceView to draw a transparent background so the camera preview will show behind the objects you render. If you are using OpenGL ES 2.0 add this to the constructor of your MySurfaceView class.

setEGLContextClientVersion(2);
setEGLConfigChooser(8,8,8,8,16,0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

I just worked through this process myself so I may have missed some details.

like image 57
Rodney Lambert Avatar answered Jul 11 '26 23:07

Rodney Lambert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!