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

[zc4wp_za2]

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.

[zc4wp_za2]

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

[zc4wp_za2]

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

5 comments

  1. Rob

    Reply

    Hi I’ve downloaded this and loaded the project into Xamarin. This told me that the ‘Camera’ command was obsolete. Please can you advise which version of Android you were running it under?

  2. Reply

    I ran the sample on Android 4 4.2 Kitkat and Android 6 Marshmallow. And it functions well, as seen on the screen shot at the end of the tutorial.
    What issue do you have precisely ? Your IDE sends you warning about the camera being deprecated ? the code is crashing ?

  3. Rob

    Reply

    Thank you.
    When building, the message the camera class is obsolete is displayed, followed by a lot of similar messages (using Nougat 7.1). The application is not built and therefor cannot run.

    Do you have plans to upgrade it to the later versions of Android? The Class for the Camera must have been replaced.

  4. Seylan

    Reply

    Hi Rob, did you by any chance manage to find an alternative ? This post was the only one I could find that does what I need 🙁

  5. Ripcoid

    Reply

    Hi Doumar, I keep getting this error when deploying your project to my device running Android 6):

    Unhandled Exception:

    System.NullReferenceException: Object reference not set to an instance of an object.

    The project does build successfully but when deploying the problem arises.
    Thanks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.