android camera xamarin

Hi everyone. A few days ago I was willing to implement camera functionalities in my Xamarin.Android application, as usual I went around to get info how to implement this on the internet, and here is a small sample which demonstrate how to build a simple camera app with Xamarin.Android which captures an image and saves it on your mobile device. Most of the knowledge I used in building this sample is gotten from this documentation.

Here is the code for this app.

Subscribe to the mailing list

Cool Coders

How we will proceed.

  • Create the project and set it up for the app.
  • Add permissions.
  • Implement the camera functionality.
  • Polish up the sample application.

Create the project and set it up

  • Using Visual Studio, create a new Android project.
  • Add a FrameLayout to your Main.axml file.

  • Create a new fragment, name CameraFeedFragment
  • Create a view for this fragment and attach this view to the fragment created in my case I named it CameraFragmentLayout.axml .
  • Inside the Fragment’s View add the following views.

  • Inside the onCreate method of the MainActivity class, add the camera fragment to the frame layout as follows.

Adding permissions to the manifest file

To access certain functionalities of our mobile phones, we need permissions as we know all. These permissions are requested in the manifest of our application. This is done as follows.

Here, we are requesting permissions to access the camera, read and write to the phone’s external storage.

Cool Coders

Implementing the Camera functionality

Camera preview

We are going to create a View which will display the images directly seen from the camera into our app. This view will inherit from the SurfaceView class. This view will also serve as call-back when the view is created, destroyed …

Here is its implementation

Picture callback

After taking a picture with the camera, there is a class which we will implement which will serve as callback and it is in tehis calss that the picture taken will be saved. This class will inherit from Camera.IPictureCallback. Here is its implementation.

The camera fragment

Let’s come back to the camera fragment where every thign is coordinated, we will add a button to the layout of this fragment in my case, this button has an id of snapButton. This button when pressed will take the picture.

  • Add the Camera Preview we created to the frame layout in the fragment’s layout. this is done as follows :

  • When the snap button is clicked, here is the code which should be executed

    Here is the complete code for the Camera fragment.

    Let’s handle a few more issues

    You may have noticed while running the app that the camera causes a bug when the app is paused, or the fragment  is destroyed, this is because the  camera should be released and  this is done as follows.

  • Here is the code for this app.

Here is what the app should look like.

Screen shot of the sample app

Cool Coders

IIf you liked this post, or it was useful to you, please ? like it, share it on twitter, facebook or other social media… in case you want to get updated on any new useful post, follow me on twitter  and like my page on facebook.

Want to know how to build a smart android app which can understand natural language with LUIS and Xamarin.Android ? Check This Post

Want to know how to create a cross platform mobile app for your bot ? Check This Post 

Follow me on social media and stay updated
%d bloggers like this: