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).
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
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.
Step 4
In this step, open String.xml page. Go to Solution Explorer-->Resource-->values-->String.xml.
Step 5
After opening String.xml file, write XML code, mentioned below.
String.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="ApplicationName">Textureviewdemo</string>
- <string name="no_camera">This app requires a camera</string>
- </resources>
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 -
- using Android.Hardware;
-
- Camera _camera;
- TextureView _textureView;
Step 7
Now, write the code, mentioned below from MainActivity.cs page.
MainActivity.cs
- public class MainActivity: Activity, TextureView.ISurfaceTextureListener {
- Camera _camera;
- TextureView _textureView;
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- _textureView = new TextureView(this);
- _textureView.SurfaceTextureListener = this;
- SetContentView(_textureView);
- }
- public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height) {
- if (Camera.NumberOfCameras == 0) {
- Toast.MakeText(this, Resource.String.no_camera, ToastLength.Long).Show();
- return;
- }
- _camera = Camera.Open();
- if (_camera == null)
- _camera = Camera.Open(0);
- var previewSize = _camera.GetParameters().PreviewSize;
- _textureView.LayoutParameters =
- new FrameLayout.LayoutParams(previewSize.Width, previewSize.Height, GravityFlags.Center);
- try {
- _camera.SetPreviewTexture(surface);
- _camera.StartPreview();
- } catch (Java.IO.IOException ex) {
- Console.WriteLine(ex.Message);
- }
-
- _textureView.Rotation = 45.0 f;
- _textureView.Alpha = 0.5 f;
- }
- public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface) {
- _camera.StopPreview();
- _camera.Release();
- return true;
- }
- public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height) {
-
- }
- public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface) {}
- }
Step 8
In this step, give the required permissions in your app.
Go to Solution Explorer--> properties-->Right click-->Open.
Step 9
After opening the properties options, select Android Manifest-->Required Permissions-->check CAMERA.
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.
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" /> 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.
Output
After a few seconds, the app will start running on your phone.
You will see the Camera TextureView is successful
Summary This was the process to create a Camera TextureView in Xamarin Android app, using Visual Studio 2015.