1

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

如果我不使用 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);
}

请有人帮忙,因为我看不到两者如何一起工作?

提前谢谢了

4

3 回答 3

2

万一它对那里的任何人有用,我最终使用了这个cordova-jquerymobile-boilerplate,它可以解决我所有的问题。

于 2012-11-19T11:51:43.643 回答
1

你可以在这里找到它http://jquerymobile.com/test/docs/pages/phonegap.html

于 2012-07-30T06:26:31.270 回答
1

你的 js 非常适合我。我正在使用 JQM 1.2.0、cordova 2.0。不过,看起来您并没有准备好调用您的设备,请尝试使用 body onload="onload" 方法,看看是否有帮助?

function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
于 2012-11-02T14:15:48.837 回答