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
-
- using Android.Hardware;
-
- Camera _camera;
- TextureView _textureView;
Step 7
In this step Create OnSurfaceTextureAvailable() Method. And write the following code.
MainActivity.cs
- public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)
- {
- _camera = Camera.Open();
-
- _textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);
-
- try {
- _camera.SetPreviewTexture(surface);
- _camera.StartPreview();
-
- } catch (Java.IO.IOException ex) {
- Console.WriteLine(ex.Message);
- }
- }
Step 8
In this step Create Public bool OnSurfaceTextureDestoryed() Method. And write the following code.
MainActivity.cs
- public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface)
- {
- _camera.StopPreview();
- _camera.Release();
-
- return true;
- }
Step 9
In this step Go to the MainActivity.cs page and write the following code between OnCreate() Method.
MainActivity.cs
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- _textureView = new TextureView(this);
- _textureView.SurfaceTextureListener = this;
-
- SetContentView(_textureView);
-
- }
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.