Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Phonegap Camera Capture Example
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"/>
<script type="text/javascript" charset="utf-8" src="js/phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoFileSuccess(imageData) {
// Get image handle
console.log(JSON.stringify(imageData));
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhotoWithData() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
}
function capturePhotoWithFile() {
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhotoWithData();">Capture Photo With Image Data</button> <br>
<button onclick="capturePhotoWithFile();">Capture Photo With Image File URI</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
@paddy02rockzz

This comment has been minimized.

Show comment Hide comment
@paddy02rockzz

paddy02rockzz Jul 22, 2013

Thank you so much. Very helpful for beginners. Awesome example of camera in phonegap. :)

Thank you so much. Very helpful for beginners. Awesome example of camera in phonegap. :)

@abenhadjhammouda

This comment has been minimized.

Show comment Hide comment
@abenhadjhammouda

abenhadjhammouda Aug 15, 2013

thank you very helpful

thank you very helpful

@ibrahimmumcu

This comment has been minimized.

Show comment Hide comment
@ibrahimmumcu

ibrahimmumcu Feb 18, 2014

Is this code really enough access camera?

I just wrote html & css code for my project. Then if I add this code to my project can I access the camera?

Is this code really enough access camera?

I just wrote html & css code for my project. Then if I add this code to my project can I access the camera?

@waelou

This comment has been minimized.

Show comment Hide comment
@waelou

waelou Feb 27, 2014

thanks man I hope you send me the phonegap.js file I can't find it

waelou commented Feb 27, 2014

thanks man I hope you send me the phonegap.js file I can't find it

@waelou

This comment has been minimized.

Show comment Hide comment
@waelou

waelou Feb 27, 2014

thanks man I hope you send me the phonegap.js file I can't find it

waelou commented Feb 27, 2014

thanks man I hope you send me the phonegap.js file I can't find it

@avishayhajbi

This comment has been minimized.

Show comment Hide comment
@avishayhajbi

avishayhajbi Mar 29, 2014

it doesn't work in my project do you know why?

it doesn't work in my project do you know why?

@dheeraj85

This comment has been minimized.

Show comment Hide comment
@dheeraj85

dheeraj85 Aug 13, 2014

How can i get phonegap.js file?

How can i get phonegap.js file?

@john-ufmedia

This comment has been minimized.

Show comment Hide comment
@john-ufmedia

john-ufmedia Sep 3, 2014

Just include phonegap.js, you don't require the actual file, this will be handled by the build process.

Just include phonegap.js, you don't require the actual file, this will be handled by the build process.

@joannalimbaga

This comment has been minimized.

Show comment Hide comment
@joannalimbaga

joannalimbaga Sep 15, 2014

how to include phonegap.js?

how to include phonegap.js?

@avinashinde2020

This comment has been minimized.

Show comment Hide comment
@avinashinde2020

avinashinde2020 Oct 14, 2014

fi you cannot find phonegap.js then include cordova.js. It works with cordova.js

fi you cannot find phonegap.js then include cordova.js. It works with cordova.js

@adamapps

This comment has been minimized.

Show comment Hide comment
@adamapps

adamapps Nov 24, 2014

Awesome, thanks bro! Been looking for ever for a tutorial that worked but seemed like I kept getting ones with older api's, this worked perfectly, thanks again!

Awesome, thanks bro! Been looking for ever for a tutorial that worked but seemed like I kept getting ones with older api's, this worked perfectly, thanks again!

@Marykai

This comment has been minimized.

Show comment Hide comment
@Marykai

Marykai Dec 1, 2014

Why this isn't working for me...?
Tells:
Uncaught ReferenceError: Camera is not defined at file:///android_asset/www/index.html:72

Marykai commented Dec 1, 2014

Why this isn't working for me...?
Tells:
Uncaught ReferenceError: Camera is not defined at file:///android_asset/www/index.html:72

@spaceless00

This comment has been minimized.

Show comment Hide comment
@spaceless00

spaceless00 Dec 22, 2014

very good thanks

very good thanks

@rocky1138

This comment has been minimized.

Show comment Hide comment
@rocky1138

rocky1138 Jan 9, 2015

This is great. What's the license on this code?

This is great. What's the license on this code?

@zaheer12

This comment has been minimized.

Show comment Hide comment
@zaheer12

zaheer12 Mar 11, 2015

Iam new in phonegap
when trying to use phonegap camera plugins get error.
Uncaught TypeError: cannot call method 'getpicture' of undefine at filename(index.html).
(using cordova-2.9.0)

Iam new in phonegap
when trying to use phonegap camera plugins get error.
Uncaught TypeError: cannot call method 'getpicture' of undefine at filename(index.html).
(using cordova-2.9.0)

@Sarbasish

This comment has been minimized.

Show comment Hide comment
@Sarbasish

Sarbasish Mar 13, 2015

Not working in Android

Not working in Android

@AmitLomte1

This comment has been minimized.

Show comment Hide comment
@AmitLomte1

AmitLomte1 Mar 17, 2015

I want to upload captured photo to server But that photo will be send from same page not again need to browse that pic from gallary and upload it please help me soon I am working on Phonegap desktop app

I want to upload captured photo to server But that photo will be send from same page not again need to browse that pic from gallary and upload it please help me soon I am working on Phonegap desktop app

@gobisankar11

This comment has been minimized.

Show comment Hide comment
@gobisankar11

gobisankar11 May 7, 2015

smallImage.src =imagedata1;

        alert("data:image/jpeg;base64," + imageData);

        var sig="gobi";

        $.ajax("http://182.74.207.232:8080/timetracking/customerInfoStoringAndroid.do?cmd=saveclientdetails1&imagedata="+smallImage+"&sigdata="+sig).done(function(data) {
                                                                                                                                                          });


    }

alert("data:image/jpeg;base64," + imageData); not working and not getting string format for send server

smallImage.src =imagedata1;

        alert("data:image/jpeg;base64," + imageData);

        var sig="gobi";

        $.ajax("http://182.74.207.232:8080/timetracking/customerInfoStoringAndroid.do?cmd=saveclientdetails1&imagedata="+smallImage+"&sigdata="+sig).done(function(data) {
                                                                                                                                                          });


    }

alert("data:image/jpeg;base64," + imageData); not working and not getting string format for send server

@daksamedia

This comment has been minimized.

Show comment Hide comment
@daksamedia

daksamedia Jul 31, 2015

is this works on phonegap build??

is this works on phonegap build??

@NikitaPatel1

This comment has been minimized.

Show comment Hide comment
@NikitaPatel1

NikitaPatel1 Aug 10, 2015

Hi ,
I took this code and executed it on Ripple Simulator and it gives me an error phonegap.js not found(404 error)

I then replaced it to include Cordova.js and then it gives error Cordova_Plugin.js not found.

Can anyone please guide me.

Thanks in Advance.

Hi ,
I took this code and executed it on Ripple Simulator and it gives me an error phonegap.js not found(404 error)

I then replaced it to include Cordova.js and then it gives error Cordova_Plugin.js not found.

Can anyone please guide me.

Thanks in Advance.

@ngopal

This comment has been minimized.

Show comment Hide comment
@ngopal

ngopal Aug 30, 2015

@NikitaPatel1 Have you added the camera plugin to your project? The command is "cordova plugin add cordova-plugin-camera" (as found in the docs: http://docs.phonegap.com/en/edge/cordova_camera_camera.md.html)

ngopal commented Aug 30, 2015

@NikitaPatel1 Have you added the camera plugin to your project? The command is "cordova plugin add cordova-plugin-camera" (as found in the docs: http://docs.phonegap.com/en/edge/cordova_camera_camera.md.html)

@sankari123

This comment has been minimized.

Show comment Hide comment
@sankari123

sankari123 Sep 11, 2015

I am having orientation problem. The captured photo is titlted towards right -90 deg

I am having orientation problem. The captured photo is titlted towards right -90 deg

@Ganeshakumar

This comment has been minimized.

Show comment Hide comment
@Ganeshakumar

Ganeshakumar Feb 4, 2016

Can anybody tell me, how to use zoom functionality here ?

Can anybody tell me, how to use zoom functionality here ?

@mrwebninja

This comment has been minimized.

Show comment Hide comment
@mrwebninja

mrwebninja Apr 13, 2016

I am absolutely new to phonegap, not know how to use this camera.html
I am trying this with phonegap desktop
what I try put camera.html in www directory & in index.html give link to camera.html then open app in mobile using phonegap mobile app & by clicking link I go to camera.html, there is 4 buttons & none of them work
can anyone help me with this
Thanks

I am absolutely new to phonegap, not know how to use this camera.html
I am trying this with phonegap desktop
what I try put camera.html in www directory & in index.html give link to camera.html then open app in mobile using phonegap mobile app & by clicking link I go to camera.html, there is 4 buttons & none of them work
can anyone help me with this
Thanks

@zanurano

This comment has been minimized.

Show comment Hide comment
@zanurano

zanurano May 18, 2016

get image data not show after capture..
why?

get image data not show after capture..
why?

@Yjaballi

This comment has been minimized.

Show comment Hide comment
@Yjaballi

Yjaballi Jun 3, 2016

Is this code really enough for acces camera.i just copied the code in index.html,is that it?

Yjaballi commented Jun 3, 2016

Is this code really enough for acces camera.i just copied the code in index.html,is that it?

@henriqueweiand

This comment has been minimized.

Show comment Hide comment
@henriqueweiand

henriqueweiand Dec 16, 2016

is possible choice photo on my library or catch a new image on camera with the same button?

is possible choice photo on my library or catch a new image on camera with the same button?

@SerhiiOst

This comment has been minimized.

Show comment Hide comment
@SerhiiOst

SerhiiOst Oct 15, 2017

It just doesn't work. I'm clicking on button and nothing happens. On browser it says:
Uncaught TypeError: Cannot read property 'getPicture' of undefined
at capturePhotoWithFile (index.html:74)
at HTMLButtonElement.onclick (index.html:94)

I installed cordova-plugin-camera using console command but it doesn't work anyway. Please help

It just doesn't work. I'm clicking on button and nothing happens. On browser it says:
Uncaught TypeError: Cannot read property 'getPicture' of undefined
at capturePhotoWithFile (index.html:74)
at HTMLButtonElement.onclick (index.html:94)

I installed cordova-plugin-camera using console command but it doesn't work anyway. Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment