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

Introduction

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.

Prerequisites

  • 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.

MainActivity.cs
  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.

MainActivity.cs

  1. public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)  
  2. {  
  3.     _camera = Camera.Open();  
  4.   
  5.     _textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);  
  6.   
  7.     try {  
  8.         _camera.SetPreviewTexture(surface);  
  9.         _camera.StartPreview();  
  10.   
  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.

MainActivity.cs

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

Step 9

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

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle)   
  2. {  
  3.     base.OnCreate(bundle);  
  4.   
  5.     _textureView = new TextureView(this);  
  6.     _textureView.SurfaceTextureListener = this;  
  7.   
  8.     SetContentView(_textureView);  
  9.   
  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.
 
 
 
Output

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

You will see the camera is streaming successfully.
 
 

Summary

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.


Similar Articles