Phonegap + JQM - Camera API,无法查看拍摄的照片

时间:2012-03-06 14:44:22

标签: javascript jquery-mobile cordova

我正在尝试Camera API for Phonegap,但我遇到了一个问题。使用官方文档中的代码:

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

    </script>

我的按钮:

<button onclick="capturePhoto();">Capture Photo</button>

和img标签:

<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />

相机打开正常,拍照没问题,但是它没有出现在页面上。

我有更多的代码可以让您从相册中选择一个图像,这样可以完美地工作,并将其显示在不同的图像标记中。

我认为问题在于它无法找到imageData。

拍摄的照片会保存到手机中,并且可以使用其他按钮显示,但我想让它在拍照后直接显示。

我正在使用JQM btw并使用Phonegap:Build web编译器编译我的APK。

3 个答案:

答案 0 :(得分:5)

关于Phonegap的文档是错误的。要获得base64编码的图片,您需要调用

 navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality:50, destinationType:Camera.DestinationType.DATA_URL });

答案 1 :(得分:0)

使用文件URI更容易,更清晰(但如果您希望图像保持不变,请在iOS上将图像保存到临时文件夹 - http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html#Camera)。您可以按如下方式立即显示图像:

navigator.camera.getPicture(displayPicture, function(err){}, {quality : 40, 
        destinationType : Camera.DestinationType.FILE_URI, 
        sourceType : Camera.PictureSourceType.CAMERA});

function displayPicture(file_uri){
 var img_tag = '<img style="width:60px;height:60px;" id="smallImage" src="'+file_uri+'" />';
//Attach the img tag where ever you want it ... $(<some parent tag>).append(img_tag) etc.

}

答案 2 :(得分:0)

在CapturePhoto()函数中,添加此选项;

destinationType : Camera.DestinationType.FILE_URI,

正如Phonegap所说,使用FILE_URI是使用新一代手机拍照的最佳做法。

要显示图像,请在getPhotoDataSuccess中使用此方法;

$('#smallImage').attr("src",imageURI);