Creating A Camera 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 Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

TextureView is used to display a stream from the camera, using a TextureView.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below, are required to be followed in order to create a Camera TextureView in 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).

Xamarin

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.

Xamarin

Step 3

In this step, delete Main.axml file. In this app, there is no need of Main.axml file.

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

Xamarin

Step 4

In this step, open String.xml page. Go to Solution Explorer-->Resource-->values-->String.xml.

Xamarin

Step 5

After opening String.xml file, write XML code, mentioned below.

String.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="ApplicationName">Textureviewdemo</string>  
  4.     <string name="no_camera">This app requires a camera</string>  
  5. </resources>  
Xamarin

Step 6

In this step, go to MainActivity.cs page in Solution Explorer. Add one Namespace, which is called Hardware.
and write the sub class TextureView.ISurfaceTextureListener. Also, create two variables.

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

Step 7

Now, write the code, mentioned below from MainActivity.cs page.

MainActivity.cs
  1. public class MainActivity: Activity, TextureView.ISurfaceTextureListener {  
  2.     Camera _camera;  
  3.     TextureView _textureView;  
  4.     protected override void OnCreate(Bundle bundle) {  
  5.         base.OnCreate(bundle);  
  6.         _textureView = new TextureView(this);  
  7.         _textureView.SurfaceTextureListener = this;  
  8.         SetContentView(_textureView);  
  9.     }  
  10.     public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height) {  
  11.         if (Camera.NumberOfCameras == 0) {  
  12.             Toast.MakeText(this, Resource.String.no_camera, ToastLength.Long).Show();  
  13.             return;  
  14.         }  
  15.         _camera = Camera.Open();  
  16.         if (_camera == null)  
  17.             _camera = Camera.Open(0);  
  18.         var previewSize = _camera.GetParameters().PreviewSize;  
  19.         _textureView.LayoutParameters =  
  20.             new FrameLayout.LayoutParams(previewSize.Width, previewSize.Height, GravityFlags.Center);  
  21.         try {  
  22.             _camera.SetPreviewTexture(surface);  
  23.             _camera.StartPreview();  
  24.         } catch (Java.IO.IOException ex) {  
  25.             Console.WriteLine(ex.Message);  
  26.         }  
  27.         // this is the sort of thing TextureView enables  
  28.         _textureView.Rotation = 45.0 f;  
  29.         _textureView.Alpha = 0.5 f;  
  30.     }  
  31.     public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface) {  
  32.         _camera.StopPreview();  
  33.         _camera.Release();  
  34.         return true;  
  35.     }  
  36.     public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height) {  
  37.         // camera takes care of this  
  38.     }  
  39.     public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface) {}  
  40. }  
Xamarin

Step 8

In this step, give the required permissions in your app.

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

Xamarin

Step 9

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

Xamarin

Step 10

In this step, add the hardware feature in your app.

Now, open the AndroidManifest.xml page. Go to the Solution Explorer-->Appname-->Properties-->AndroidManifest.xml.

Xamarin

Setp 11

After opening the AndroidManifest.xml page, write the feature, mentioned below in your app.

AndroidManifest.xml

<uses-feature android:name="android.hardware.camera" />

Xamarin

Step 12

If you have an Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

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.

Xamarin

Output

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

You will see the Camera TextureView is successful

Xamarin

Summary

This was the process to create a Camera TextureView in Xamarin Android app, using Visual Studio 2015. 


Similar Articles