How To Display A Stream From The Camera Using A TextureView In Xamarin Android App Using Visual Studio 2015


Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

How to display a stream from the camera using a TextureView.


  • Visual Studio 2015 Update 3
The steps, given below, are required to be followed in order to Display a stream from the camera Using a TextureView in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Step 3

Next go to the solution explorer. In the solution explorer you have the all files and sources in your project. Next Select Resource-->Layout-->double click to open main.axml page. You want select source to write the xaml code. If you want to design it,  choose the designer window so you can design your app.

Step 4

After opening, the main.axml file will open the main page designer. In this page choose which type you want so you can design this page.
Step 5

In this step Delete the Main.axml file. Because in this app there is no need for the Main.axml file.

Now, Go to Solution Explorer-->Resource-->Layout-->Main.axml-->Right click-->Delete.

Step 6

In this step go to the MainActivity.cs page in Solution Explorer. Add one Namespace, its name is called Hardware.

Write the sub class TextureView.ISurfaceTextureListener, and also create two variables.

  1. //namespace   
  2. using Android.Hardware;   
  3. //variables   
  4. Camera _camera;   
  5. TextureView _textureView;  

Step 7

In this step Create OnSurfaceTextureAvailable() Method. And write the following code.


  1. public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)  
  2. {  
  3.     _camera = Camera.Open();  
  5.     _textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);  
  7.     try {  
  8.         _camera.SetPreviewTexture(surface);  
  9.         _camera.StartPreview();  
  11.     } catch (Java.IO.IOException ex) {  
  12.         Console.WriteLine(ex.Message);  
  13.     }  
  14. }   

Step 8

In this step Create Public bool OnSurfaceTextureDestoryed() Method. And write the following code.


  1. public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface)  
  2. {  
  3.     _camera.StopPreview();  
  4.     _camera.Release();  
  6.     return true;  
  7. }  

Step 9

In this step Go to the MainActivity.cs page and write the following code between OnCreate() Method.

  1. protected override void OnCreate(Bundle bundle)   
  2. {  
  3.     base.OnCreate(bundle);  
  5.     _textureView = new TextureView(this);  
  6.     _textureView.SurfaceTextureListener = this;  
  8.     SetContentView(_textureView);  
  10. }   
Step 10

In this step Give Required Permissions in your app.

Go to the Solution Explorer--> properties-->Right click-->Open.

Step 11

After opening the properties options, select Android Manifest-->Required Permissions-->Check CAMERA.

Step 12

If you have Android Virtual device, run the app on it. Otherwise, connect your Android phone and run the app in that. Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

After a few seconds, the app will start running on your phone.

You will see the camera is streaming successfully.


So, this was the process of how to create a display and stream from the camera using TextureView in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all