10 Replies Latest reply on Mar 28, 2014 4:39 AM by David Miller

    Access camera using Javascript and 4.0 API

    Mark Ross

      I would like to access the camera on an Android device (hopefully its the same for iPhones too).  I'm currently writing a pure javascript application (no Ruby) using RhoMobile's 4.0 API.  I've read in the docs that the Camera API has not yet been ported to 4.0, so I have to access via the 2.2 APIs.  This is where I start to get lost.  I believe I have all the necessary files included for the 2.2 API access via javascript:

       

      build.yml:

           extensions:

             - rhoconnect-client

             - rho-javascript

           capabilities:

             - camera

       

      index.html (main page):

      <!-- required for pre-4.0 JS API -->

      <script type="text/javascript" src="/public/jquery/jquery-1.6.4.min.js"></script>

      <script type="text/javascript" src="/public/jquery/jquery.json-2.4.min.js"></script>

      <!-- pre-4.0 JS API -->

      <script type="text/javascript" src="/public/js/rho_javascript_api.js"></script>

      <!-- compatibility layer -->

      <script type="text/javascript" src="/public/js/rho_common_api_noconflict.js"></script>

      <!-- new Common JS API -->

      <script type="text/javascript" charset="utf-8" src="/public/api/rhoapi-modules.js"></script>

      <script type="text/javascript" charset="utf-8" src="/public/jquery/jquery-1.9.1.min.js"></script>

      .....

      <div data-role="content">

        <form action="" method="post" id="findingForm">

        <button type="button" id="photoBtn">Add Photo</button>

        <button type="submit" id="findingBtn">Submit</button>

        </form>

      </div>




      application.js:


      $('#photoBtn').on('click', function(e){

        Rho.Camera.choose_picture(function(params) {

            console.log(params);

        });

      });

       

       

       

      When I click the button above, I receive the following errors in adb logcat:

       

      I/APP     (15917): I 02/11/2014 11:53:57:639 00003e42           HttpServer| Process URI: '/app/RhoJavascriptApi/command_handler'

      I/APP     (15917): E 02/11/2014 11:53:57:639 00003e42           HttpServer| The file /app/RhoJavascriptApi/command_handler was not found

      E/Web Console(15917): Uncaught SyntaxError: Unexpected token < at http://localhost:44326/public/index.html#findings:1

       

      It seems like the javascript code is looking for a Ruby controller or something along those lines?  Or I'm completely missing something.  Any help is greatly appreciated!

       

      Device: Getac Z710 (tablet)

      Android: 4.1.1

       

      -Mark

        • Re: Access camera using Javascript and 4.0 API
          Derek Warren

          Mark,

           

            Camera support with the new JS APIs is expected in the 4.1 release sometime by the end of Q1.

           

          In the meantime you might want to take a look at the RhoSpection application that uses the camera using a short ruby controller.

          rho-samples/Rhospection/Rhospection/app at master · rhomobile/rho-samples · GitHub

           

          Look in the Camera directory and Report/new.erb (Search for camera). In the Report/new.erb the ruby method for the camera is called directly.

            • Re: Access camera using Javascript and 4.0 API
              Mark Ross

              Derek,

               

              Last week RhoMobile held a chat on Twitter to discuss the 4.1 API release.  I sent this message and got the reply below:

               

              @rossmd: @RhoMobile will 4.1 support camera functionality using a pure javascript app (no Ruby code/controllers/etc)? #RhoChat - 05 Mar 14

               

              @adamblum: @rossmd @RhoMobile not yet. You could write your own JavaScript proxy call down to the Ruby. #rhochat

               

               

              So Adam says camera functionality will NOT be in 4.1.  Can you confirm if this is correct or not?  If not, do you have any ideas/suggestions on how to get camera access working with what he's suggesting "Javascript proxy down to the Ruby"?  It was my understanding that if I'm using javascript only (no /app folder at all), the compiler never uses Ruby (at least that Javascript has access to).  If I'm wrong about that, can you explain and/or give an example of how to access Ruby from Javascript using an app with "javascript_application: true" in build.yml?

               

              Thanks,

              Mark

            • Re: Access camera using Javascript and 4.0 API
              Jon Tara

              I'd be happy if I could get the camera API to work in 4.1 Ruby...

               

              I build an existing 3.5.1 app with 4.1. It uses the camera API.

               

              It opens the camera window, I can snap a picture, the camera UI closes, and, then... nothing. Something must have changed about the callback.

               

              Need to take a look at logs yet, add logging to insure I get the callback, etc.

               

              Any obvious known difference?