SAP Mobile App Development

How to implement Camera functionality in SAPUI5 hybrid mobile App?

                This is a step by step tutorial showing how to implement device camera functionality in  SAPUI5 Hybrid Mobile App with SAP Web IDE and hybrid app toolkit.
                 We will make use of cordova camera plugin in this tutorial.This plugin provides an API for taking pictures and choosing images device gallery.This is continuation of our previous tutorial How to Create SAPUI5 Hybrid Mobile App with SAP Web IDE?


  1. Windows system setup with SAP Mobile Platform SDK installed and Hybrid App Toolkit configured.(If not setup already visit my  blog post Windows System Setup for Developing Mobile Apps with SAPWebIDE and SAP Mobile SDK )
  2. Created basic SAPUI5 Hybrid Mobile App(If you haven't created one yet follow previous blog How to Create SAPUI5 Hybrid Mobile App with SAP Web IDE).

  1. Start HAT Connector ,Open SAP Web IDE and check HAT connectity is avialable.If not familiar follow pre-requisites 2 section.
  2. Right click on project folder and choose Project Settings.Go to Hybrid App Toolkit->Hybrid App Configuration.Under Plugins section tick mark cordova Camera and Access File plugins and press save button.
  3. Time to code.Open View1.view.xml file(Your file name may be different if you have changed name of view).In the view add two sap.m.Button controls.One for capturing using device Camera and one for selecting image from device library.Then add one sap.m.Image control to show the captured or selected image.My view code is below.The button's press event is used for calling the Camera methods of cordova Camera api.

  4. Open View1.controller.js.Both onCapture and onSelect method will code getPicture method of cordova camera but with different parameters.Below is my controller logic.In the success handler set the Image control's src attribute by using setSrc() method.

  5. To test the App Right Click ->Run->Run on Local Device/Simulator->Android Device.Testing on emulator may not work properly because of lack of camera in emulator.
  6. See my App in action.Screenshot taken from my redmi note3.
In my next tutorial I will be showing how to upload this image to a server using cordova File Transfer plugin
If you enjoyed this post, I’d be grateful if you’d help it spread by sharing.Thank you! See you soon


Post a Comment

Powered by Blogger.