Access Device Camera using Sencha Touch

1 .

    source: ‘camera’,
    destination: ‘file’,

    success: function(url) {
        //show the newly captured image in a full screen Ext.Img component:
        Ext.create(‘Ext.Img’, {
            src: url,
            fullscreen: true


success: function(image) {
quality: 75,
width: 200,
height: 200,
destination: ‘data’

3. <input type=”file” id=”take-picture” accept=”image/*”> :


1. Use the HTML 5 API for accessing camera. : Adv. Then we can provide a web interface of the app too… and also single code
2. Use the Native OS API to access the camera using phonegap…DisAdv : will have to write seperate code for seperate OSs.

In option 1 , i checked that we can’t use WEBRTC ( as its not implemented in Chrome or Firefox or Safari.It works only on Chrome Dextop for now.

Instead try using  : or

Comparison of getUserMedia behaviour in browsers as of February 2013
Firefox 18  Opera 12    Chrome 24
Requires vendor prefix  Yes (moz)   No  Yes (webkit)
Triggered with autoplay attribute   No  Yes Yes
Requires enabling by user   Yes 1   No  No
Firing of playing event Repeatedly  Once    Once
Supports file:// protocol   Yes Yes No
Tab playing notification    None    Icon    Animated icon
Permission request  Each page load  First page load only    Each page load
getUserMedia has to be enabled in Firefox by setting the media.peerconnection.enabled option to true in about:config.


Sencha Ext.device.Camera.capture

Till now i have tried this in Sencha :

function a() {
    // Grab elements, create settings, etc.
    var canvas = document.getElementById(“canvas”),
    context = canvas.getContext(“2d”),
    video = document.getElementById(“video”),
    videoObj = { “video”: true },
    errBack = function (error) {console.log(“Video capture error: “, error.code);};

    // Put video listeners into place
    if (navigator.getUserMedia) { // Standard
        navigator.getUserMedia(videoObj, function (stream) {
            video.src = stream;
        }, errBack);
    } else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
        navigator.webkitGetUserMedia(videoObj, function (stream) {
            video.src = window.webkitURL.createObjectURL(stream);
        }, errBack);

    // Trigger photo take
    document.getElementById(“snap”).addEventListener(“click”, function () {
        context.drawImage(video, 0, 0, 200, 100);



This thing works like a charm in mobile and dextop chrome browsers (note that in mobile chrome it worked only on hosting and not thru the local copy of html)…but sadly, the sencha app isn’t able to work  with this….

Trying this Sencha thing out :

: function(image) {
: 75,
: 200,
: 200,
: 'data'
Well i now saw that in both the cases above the browser is actually searching for camera on the dextop chrome it works 🙂

But on device, as apk is opening some other webview ..its not showing up…

NOW The only option is PHONEGAP………..

Sencha Touch Quirks and Lessons

I know that there are many posts by me on this thing :

Here’s what i learnt today :

1. Do not give up on the sencha architect as it is a great tool, if for nothing else, to tell you the available configurations of the UI controls

2. If you get any inconsistent behavior in the methods and handlers created using architect then plz delete and create again with another name

3. Never Duplicate controls as it creates a lot of mess specially in case there are handlers attached to stuff..Just don’t do it…simply create again as its not worth it at all.

4. Sencha HTML property of controls or even panels is not complete HTML but just specific elements like to add a canvas of button to a panel using HTML, in its HTML just  give <button..>

And then don’t forget to enable HTML styling

5. Another thing that i got stuck at was the HTML Styling of a sencha button and finally figured out that the right way is to add the HTML as text of the button (not as HTML of Button) and then to enable HTML styling

6. Always aim at writing minimum code and reusing controls even if it means more effort

Accessing Native Camera in Sencha App using Phonegap

Lessons : 

1. Native Camera API provides many features that Phonegap doesn’t expose yet (in 2.7), so native code has to be written for it.

2. In fact, WebRTC provide more photo editing features (using the HTML Canvas) than Phonegap does today.

>> The code is simple but i spent a day trying to make it as a rule always look for the eg. in the official source instead of banging your head with hit and try…

I used the example from the Phonegap Site as it is (tested) :

Basically this goes in the handler of the button tap of the button on the panel and then on success it shows the captured image on the current panel, so in the panel add HTML :

 xtype: ‘panel’,
                hidden: true,
                html: ‘<br/><img style=”display:none;margin-left: 20px;width:340px;height:185px;” id=”FrontImage” src=”” /><br/>’,
Code for handler :

var pictureSource;   // picture source
var destinationType; // sets the format of returned value;;, onFail, { quality: 20, allowEdit: true, 
destinationType: destinationType.DATA_URL });

function onFail(message) {
    alert(‘Failed because: ‘ + message);

function onPhotoDataSuccess(imageData) {

    Ext.getCmp(‘frontCheckBtnD’).setText(‘Image Captured’);

    if((Ext.getCmp(‘frontCheckBtnD’).getText()===’Image Captured’)&&(Ext.getCmp(‘toFieldLabelDnew’).getValue()!== ”)&&(Ext.getCmp(‘transferAmount’).getValue()!== ”))

    // Uncomment to view the base64 encoded image data
    // console.log(imageData);

    // Get image handle
    var FrontImage = document.getElementById(‘FrontImage’);

    // Unhide image elements
    // = ‘block’;

    // Show the captured photo
    // The inline CSS rules are used to resize the image
    FrontImage.src = “data:image/jpeg;base64,” + imageData;