If you find any errors, typos or have general feedback, select the text and click CTRL+ALT+ENTER.

Introduction

This tutorial shows how to create an AngularJS app that uses the Camera service.

The app will look like:
UI

Before you begin

Tutorial level: intermediate.

Prerequisites: an Appery.io account.

Apache Cordova (PhoneGap)

Apache Cordova (PhoneGap) is automatically included when you create a new project in Appery.io. The Camera component used in Appery.io is the Camera component from Apache Cordova.

To learn more about the component, and any other settings or options, please see the PhoneGap camera documentation page.

Creating the app

Design

1. Open your Appery.io Bootstrap & AngularJS or Ionic & AngularJS project or create a new one. You will get two default pages: index and Screen1.

2. Go to Screen1 and create the following UI consisting of:

  • The Button component. Configure its Text to Camera and ng-click to launchCam();
  • The Image component. Set ng-src {{im}}.

If you are new to using Bootstrap & AngularJS and/or Ionic & AngularJS UI components, read here and here respectively.

This is how it should look like:

UI1

Now, let’s create services and variables and bind them with UI components.

Scope

1. First, go to SCOPE and add a variable: im (for image).

2. Then, to add a Camera plugin for Android: in the builder, go to Project > CREATE NEW > From Plug-in.

3. In the new window, select Apperyio Camera Service and click “Import selected plug-ins”.

4. In the Project view, a related service (Camera_getPicture) and a JavaScript file appear.

5. After a native service has been added to the app, it can be called. Invoking a native service is very similar to invoking a REST service.

To call the added service, go to the SCOPE view of the Screen1 page, add a new scope function – launchCam and click “Edit”, opening the function editor.

6. Next, use the snippet Invoke service: click “Invoke service”, delete the text "service_name" in the code and  click <CTRL> + <SPACE> to select Camera_getPicture service.

Instead of invoking services in the code editor with subsequent auto completing, you can use a drag and drop feature to invoke services. Read here to learn how.

After auto completing, the service is added to the function code and you can map the service to the page. The click on the upper mapping button defines the service request, the lower one – the service response

7. Optionally, you can use the following parameters to create a request mapping:

  • quality – Quality of the saved image, expressed as a range of 0-100 (information about the camera’s resolution is unavailable).
  • destinationType – Choose the format of the return value.
  • sourceType – Set the source of the picture.
  • allowEdit – Allow simple editing of image before selection.
  • encodingType – Choose the returned image file’s encoding.
  • targetWidth – Width in pixels to scale image (aspect ratio remains constant).
  • targetHeight – Height in pixels to scale image (aspect ratio remains constant).
  • mediaType – Set the type of media to select from (PHOTOLIBRARY or SAVEDPHOTOALBUM).
  • correctOrientation – Rotate the image to correct for the orientation of the device during capture.
  • saveToPhotoAlbum – Save the image to the photo album on the device after capture.
  • popoverOptions – iOS-only options that specify popover location in iPad.
  • cameraDirection – Choose the camera to use (front- or back-facing).

Please find more here.

Here is a sample request mapping with the use of some camera service optional parameters:
Mapping_parameters

8. Now, click success “Mapping” button to map the service to the page and bind the added variable im to image in the service responce, then click “Save and return”:Mapping

You will have to notify AngularJS about the need to run update loop using scope method $scope.$apply(); as well. Please find more info about this here.

The result:

Since we’re invoking a native component, this app needs to be tested as a hybrid app, or installed on the device:
UI

Testing options:

Android

  1. Use the Appery.io Mobile Tester app. It’s an app that allows you to launch any app created in Appery.io as a hybrid app. You can find it in the Google Play Store.
  2. Build the Android binary and install it your device. When the build is completed, you’ll see a QR code. Scanning the QR code will download the app to your phone. You can also email the app to your device.

iOS

iOS options:

  1. Use the Appery.io Mobile Tester app. It’s an app that allows you to launch any app created in Appery.io as a hybrid app.
  2. Build the app and install it on your device.