正确使用Jquery移动和Phonegap设备Apis的方法

时间:2012-07-28 16:39:41

标签: jquery-mobile cordova

我已经在这里阅读了一些类似的帖子但没有成功。我想做的很简单。我有一个非常小的jquery移动应用程序,包裹在phonegap中。在某个页面上,我希望能够访问设备摄像头(比如在iPhone上)。我正在使用来自phonegap相机的代码docs

如果我不使用jquery mobile,代码本身可以正常工作,但是我想使用JQM来设置应用程序的样式。

我知道JQM使用ajax来调用页面,并且有自己的pageinit()和pageshow()函数。我怎样才能让phonegap和JQM更好地发挥作用,因为两者都需要激活它们自己的功能(deviceReady()和JQM是非常重要的页面)。

如果有帮助,这是我的相机页面代码:

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name = "format-detection" content = "telephone=no"/>
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.1.1.css" />
        <title>Take Picture</title>

    </head>
    <body>
        <div id="takePicturePage" data-role="page" data-add-back-btn="true">

            <div data-role="header" data-position="fixed" id="hrdHome" data-theme="b">
                <h1>Take a picture</h1>
            </div><!-- /header -->
            <div data-role="content" id="content">

                <button onclick="capturePhoto();">Capture Photo</button>
                <button onclick="capturePhotoEdit();">Capture Editable Photo</button>
                <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button>
                <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button>
                <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
                <img style="display:none;" id="largeImage" src="" />

            </div><!-- /content -->
         </div>

        <script type="text/javascript" src="cordova-2.0.0.js"></script>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.1.1.js"></script>
        <script type="text/javascript" src="js/takePicture.js"></script>
    </body>
</html>

这是我的takePicture.js:

    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) {
  // Uncomment to view the base64 encoded image data
  // console.log(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 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 capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
}

// A button will call this function
//
function capturePhotoEdit() {
  // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true }); 
}

// 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);
}

请问有人可以提供帮助,因为我无法看到这两个人如何继续工作?

非常感谢提前

3 个答案:

答案 0 :(得分:2)

只是对任何人都有任何用处,我最终使用这个cordova-jquerymobile-boilerplate来处理并解决了我的所有问题。

答案 1 :(得分:1)

答案 2 :(得分:1)

你的js对我来说非常合适。我正在使用JQM 1.2.0,cordova 2.0。 它看起来不像你正在调用你的设备,尝试使用body onload =“onload”方法,看看是否有帮助吗?

function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}