Monday, 6 October 2014

HB Blog 25: How to take picture from camera programmatically using phonegap/cordova and javascripts?

The mobile application development landscape is filled with many ways to build a mobile app.
Among the most popular are:
    Native iOS,
    Native Android,
    PhoneGap,
    PhoneGap Android Cordova Library,
    Appcelerator Titanium,
    and many more.

PhoneGap Android Cordova Library:- 
Refer the below link for starting with phonegap:-
http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html

Step by step guide for integrating camera using cordova library:-

 

Step I :- Download and add cordova library into libs folder.
Step II :- You need few javascript and other supportive files to support cordova library. can download them from below link.
Download Support Files
Step III :- Add the supportive files www name folder into your project assets folder.
Step IV :- Observe index.html file. It is your main file for implementing camera functionality.

Refer the below link for complete sample code:- 
Download Sample Code
Download Apk File

Have a look on few code snippets,

index.html 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Take picture using device camera and retrieve image as base64-encoded string
 function capturePhoto() {
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        destinationType: destinationType.DATA_URL });
    }

// Retrieve image file location from specified source
function getPhoto(source) {
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
        destinationType: destinationType.FILE_URI,
        sourceType: source });
    }

Step V :- Load index.html file into your activity java class which should extend DroidGap class of cordova library as shown below

MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// extend DroidGap class of coirdova library
public class MainActivity extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty("splashscreen", R.drawable.ic_launcher);
        super.loadUrl("file:///android_asset/www/index.html");
    }
} 

No comments:

Post a Comment