首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Phonegap + JQM - Camera接口,无法查看采集到的照片

Phonegap + JQM - Camera接口,无法查看采集到的照片
EN

Stack Overflow用户
提问于 2012-03-06 22:44:22
回答 3查看 4K关注 0票数 3

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

代码语言:javascript
复制
<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>

和我的按钮:

代码语言:javascript
复制
<button onclick="capturePhoto();">Capture Photo</button>

和img标签:

代码语言:javascript
复制
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />

相机打开得很好,拍照没有问题,但它不显示在页面上。

我还有更多的代码可以让你从相册中选择一张图片,而且效果很好,将它显示在不同的图片标签中。

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

捕获的照片确实会保存到手机上,并可以使用其他按钮显示,但我希望它在拍摄照片后直接显示。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-15 19:45:11

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

代码语言:javascript
复制
 navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality:50, destinationType:Camera.DestinationType.DATA_URL });
票数 5
EN

Stack Overflow用户

发布于 2012-06-14 23:04:14

使用文件URI要容易得多,也更干净(但如果您希望图像持久存在,请小心,因为在iOS上,图像被保存到一个临时文件夹- http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html#Camera)。您可以立即显示图像,如下所示:

代码语言:javascript
复制
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.

}
票数 0
EN

Stack Overflow用户

发布于 2014-08-28 16:24:32

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

代码语言:javascript
复制
destinationType : Camera.DestinationType.FILE_URI,

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

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

代码语言:javascript
复制
$('#smallImage').attr("src",imageURI);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9585869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档