Camera Integration In Xamarin

Introduction

 
Most mobile applications have the feature of accessing the camera to capture photos and videos. The integration of camera and accessing permissions for the camera is discussed in this article.
 

Permissions

 
Permission is asked at the time of accessing the camera to store the images or videos. For this, external storage permission is required. To record audio, audio permission is required. The android. hardware. camera feature is used to access the device camera and flash package is used to access flash while taking pictures.
 

Configuring the Emulator

 
The development of the application is performed in an emulator and the configuration is set to access the camera. 
 
Select the main menu and open the android emulator manager.
 
Pick the emulator used for development and select edit.
 
Select the type of camera as front or back camera in the edit AVD dialog.
 

Data Service

 
The camera is used to capture images and the captured picture need to be stored in a location if the device and data service is not required to store the image in a certain location. Data service deletes the POI and also deletes the corresponding POI image. The below code is used to add data services:
  1. public interface IPOIDataService    
  2. {    
  3.  string GetImageFilename ( int id );    
  4. }    

Implementing GetImageFilename()

 
After the image is captured it is stored in a particular location with a unique file name. The below function is used to store the image in a location with a unique file name.
  1. public string GetImageFilename ( int id )  
  2. {  
  3.  return Path.Combine (_storagePath, "poiimage" + id.ToString () + ".jpg" );  
  4. }  
To delete an image that corresponds to POI, the DeletePOI method is used and the below code is used to delete the image that corresponds to the POI if it exists.
  1. pubic void DeletePOI ( PointOfInterest  poi )  
  2. {  
  3.  if ( File.Exists (GetFilename (poi.Id)) )  
  4.  File.Delete (GetFilename (poi.Id) );  
  5.   
  6.  if ( File.Exists (GetImageFilename (poi.Id )))  
  7.  File.Delete (GetImageFilename (poi.Id ));  
  8.   
  9.  _pois.Remove (poi);  
  10. }  

POIDetailActivity

 
Capturing the image from POIDetailActivity has some tasks shown below, step by step.
 
Add a new user interface widgets to initiate capturing and displaying the photo.
 
Build a photo intent to navigate to the external camera app to capture the photo.
 
Processing the result of photo intent and displaying the photo if the picture is captured successfully.
 

Adding User Interface Elements

 
There are many user interface elements to capture the picture and displaying the component to display the captured image. Add the new imageButton element and add the Image view element to the screen. The sample code is shown below to display the image view on the screen.
  1. < /TableRow>  
  2. < /TableLayout>  
  3. < ImageView  
  4.  p1 : src = "@android : drawable/ic_menu_gallery"  
  5.  p1 : layout_width = "wrap_content"  
  6.  p1 : layout_height = "wrap_content"  
  7.  p1 : padding = "10dp"  
  8.  p1 : id = "@+id/poiImageView"  
  9.  p1 : layout_gravity = "center _ horizontal "  
  10.  p1 : scaleType = "fitCenter" />  
  11. < LinearLayout>  

Intent

 
The intent class is used to start the external camera app to capture the images and the Mediastore.ActionImageCapture action shows the android platform to capture a photo and to use the existing app. The below line is used to create an intent with image capture action.
  1. Intent cameraIntent = new intent ( MediaStore.ActionImageCapture );  
The startActivityForResult is a method work in conjunction with a callback to OnActivityResult to communicate the result of the intent. It is an int value named request code that returns a parameter in the callback to OnActivityResult and helps identify for launching the intent. The below line is used to start the intent.
  1. startActivityForResult ( cameraIntent , CAPTURE_PHOTO ) ;    

Displaying Image in POIDetailActivity

 
Select the same POI, the previously captured image is not displayed and the onCreate method is used to load the image if an ID for POI was passed in the intent.
  1. if (Intent.HasExtra ("poiId"))  
  2. {  
  3.  int poiId = Intent.GetIntExtra ("poiId" , -1);  
  4.  _poi = POIData.Service.GetPOI (poiId);  
  5.  Bitmap PoiImage = POIDatta.GetImageFile (_poi.Id.Value);  
  6.  _poiImageView.SetImageBitmap (poi Image);  
  7. }  
  8. else  
  9.  _poi = new PointOfInterest ();