In Focus

Take Picture With Android Camera Using Xamarin Native Application

Reading this article, you will learn how to create camera application in Xamarin application for Android Platform with XAML and Visual C# in cross platform application development.

Introduction

Reading this article, you will learn how to create a camera application in Xamarin for Android platform.

Tools

We are using Visual Studio 2017 and Windows 10 Operating System.

Step 1

Go to Visual Studio 2017.

Click File —> New —> Project or shortcuts (Ctrl+Shift+N).

Step 2

Go to Visual C#  --> Android –> Blank App (Android); change your application name and click the "OK" button.

Step 3

After the process completes, the Xamarin dashboard appears. At the top right, you will see Solution Explorer. There is your application solution.

Step 4

Select application name; extract to resources. The Layout folder appears in which there is Main.axml* file. Double click to open Main.axml.

Step 5

I have given XAML code already. You can use this code or you can write your own code.

XAML Code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="10">  
  3.     <ImageView android:id="@+id/imageView" android:layout_weight="70" android:layout_width="match_parent" android:layout_height="fill_parent" />  
  4.     <Button android:layout_weight="1" android:id="@+id/btnCamera" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=".!CLICK_ON_CAMERA!." />   
  5. </LinearLayout>  
Step 6

Double click on MainActivity.cs file. You will see the camera application. Protected override content including a file of //SetContentView (resource.layout.Main) should be there.

Step 7

I have given C# code. You can use this code or you can use your own code. But be careful; all the code for your MainActivity should be here.

C# Code

  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Content;  
  5. using Android.Provider;  
  6. using Android.Runtime;  
  7. using Android.Graphics;  
  8. namespace Camera_Application {  
  9.     [Activity(Label = "Camera_Application", MainLauncher = true, Icon = "@drawable/icon")]  
  10.     public class MainActivity: Activity {  
  11.         ImageView imageView;  
  12.         protected override void OnCreate(Bundle bundle) {  
  13.             base.OnCreate(bundle);  
  14.             // Set our view from the "main" layout resource  
  15.             SetContentView(Resource.Layout.Main);  
  16.             var btnCamera = FindViewById < Button > (Resource.Id.btnCamera);  
  17.             imageView = FindViewById < ImageView > (Resource.Id.imageView);  
  18.             btnCamera.Click += BtnCamera_Click;  
  19.         }  
  20.         protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) {  
  21.             base.OnActivityResult(requestCode, resultCode, data);  
  22.             Bitmap bitmap = (Bitmap) data.Extras.Get("data");  
  23.             imageView.SetImageBitmap(bitmap);  
  24.         }  
  25.         private void BtnCamera_Click(object sender, System.EventArgs e) {  
  26.             Intent intent = new Intent(MediaStore.ActionImageCapture);  
  27.             StartActivityForResult(intent, 0);  
  28.         }  
  29.     }  
  30. }  
Step 8

Just create a page application name and button. This is only for demo app; you can use more codes using design in this application.

Step 9

Then, build this application; wait for a few seconds; all calling functions will get connected.

Step 10

Calling function and all processes will complete for this page. Failure is 0 and success is 1, so building process is completed.

Step 11

Select a device; I have used emulator only so I selected “5’ KitKat (4.4) Application program interface (API) 19”.

Step 12

Emulator screen appears but this emulator needs 2GB RAM so you need a minimum of 4GB RAM in your machine along with an i3 Third Generation processor.

Step 13

Access your mobile's camera.

Step 14

Take a selfie and it will be saved.



SUMMARY

In this article, you learned how to create a camera app using Android native application development feature of Xamarin with .NET Standard libraries.

If you have any questions/ feedback/ issues, please write in the comment box.