Wiring the video Camera for Augmented Reality on MonoGame for Android

I may finally have an opportunity to give back to the MonoGame community!

I’m trying to get the Android Camera to present on the MonoGame surface for developers to create Augmented Reality experiences.

I can indeed connect the camera to the Surface by modifying MonoGameAndroidGameView.cs in the following manner:

 public delegate void OnSurfaceCreatedExternalCallBackFunc(Surface surface);
    public static OnSurfaceCreatedExternalCallBackFunc OnSurfaceCreatedExternal;
    void ISurfaceHolderCallback.SurfaceCreated(ISurfaceHolder holder)
    {
        if (OnSurfaceCreatedExternal != null)
            OnSurfaceCreatedExternal(holder.Surface);

        SurfaceCreated(holder);
  
        
        Android.Util.Log.Debug("MonoGame", "MonoGameAndroidGameView.SurfaceCreated: surfaceFrame = " + holder.SurfaceFrame.ToString());
        _isSurfaceChanged = false;
    }

Then, in the basic GameActivity I set that delegate:

var gameView = (View)game.Services.GetService(typeof(View));
        SetContentView(gameView);
        Microsoft.Xna.Framework.MonoGameAndroidGameView.OnSurfaceCreatedExternal = OnSurfaceCreated;
        game.Run();

And when my delegate runs, after the surface has been initialized by MonoGame, I connect the video camera.

Unfortunately this yields a

BufferQueue(161): [SurfaceView] connect: already connected (cur=1, req=4)

Which leads me to believe that only one object can be connected to the SurfaceView.

I’m having trouble finding how to either circumvent or fix this issue (in the case that I should be able to connect more than one), or, the best method to insert another, and have it behind the MonoGame surface and perform efficiently.

Any help is appreciated

I wrote up how to do this on my blog

http://www.infinitespace-studios.co.uk/general/getting-started-on-argumented-reality-using-xamarin-android-and-monogame/

It looks like there have been some changes to the framework, the Window type on the Game class is now a GameWindow and the code below can no longer be used.

var g = new Game1 ();
g.Window.SurfaceFormat = Android.Graphics.Format.Rgba8888;

I’m trying to figure out a different way to render the MonoGame view transparently over the top of camera but not having much luck so far. Anybody know how to do this with the latest MonoGame version?

Yup :slight_smile:

var v = g.Services.GetService<View> ();
var gv = v.JavaCast<AndroidGameView>();
gv.SurfaceFormat = Android.Graphics.Format.Rgba8888;
1 Like

Thanks! I’ve got it working now with that code. I just had to add a reference to the OpenTK library so that I could see the AndroidGameView.

Can we get the camera texture and assign it to the shader instead of render monogame on top of the surface?