首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Phonegap(3.0.0)摄像头第一次尝试不成功

Phonegap(3.0.0)摄像头第一次尝试不成功
EN

Stack Overflow用户
提问于 2013-09-06 13:44:54
回答 6查看 4.6K关注 0票数 8

出于测试目的,我复制了在phonegap camera API上找到的完整示例,并在onPhotoDataSuccess上放置了一个警报,以测试函数何时被触发。在拍摄第一张照片时,将不会显示警报。但是,在第一次尝试之后,将在保存照片后显示警报。

有什么建议吗?如果有什么不清楚的地方,我很乐意说得更具体一些。

我在我的Android Galaxy S3上测试了下面的代码

代码语言:javascript
复制
    <!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready",onDeviceReady,false);

    // device APIs are available
    //
    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,
        destinationType: destinationType.DATA_URL });
    }

    // 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,
        destinationType: destinationType.DATA_URL });
    }

    // 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>
  </head>
  <body>
    <button onclick="capturePhoto();">Capture Photo</button> <br>
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

-更新1

我已经在另一段代码上进行了测试:

代码语言:javascript
复制
    (function () {
        $scroller = $('.scroller'),

        // Take a picture using the camera or select one from the library
        takePicture = function (e) {
            var options = {
                quality: 45,
                targetWidth: 1000,
                targetHeight: 1000,
                destinationType: Camera.DestinationType.FILE_URI,
                encodingType: Camera.EncodingType.JPEG,
                sourceType: Camera.PictureSourceType.CAMERA
            };

            navigator.camera.getPicture(
                function (imageURI) {
                    console.log(imageURI);
                    alert('test');
                    $scroller.append('<img src="' + imageURI + '"/>');
                },
                function (message) {
                    // We typically get here because the use canceled the photo operation. Fail silently.
                }, options);

            return false;

        };

    $('.camera-btn').on('click', takePicture);

}());

这也有同样的效果。它在第一次捕捉期间不做任何事情,但在第二次捕捉之后显示图片。我还发现第二张照片后面的照片是我拍的第一张照片。getPicture中的第一个参数似乎不会在第一个快照上触发。这很令人沮丧,因为logcat并没有向我展示任何可以使用的东西。

-更新2

我刚刚在Phonegap Build上试过,它工作正常。所以这一定和插件有关...

EN

Stack Overflow用户

发布于 2013-10-25 00:36:39

从3.0.0更新到3.1.0后,我也遇到了同样的问题。相机延迟,没有地理位置等。

查看文件platforms\android\cordova\version是否声明了旧版本。然后你需要更新你的平台。这就是我所做的。

删除所有插件:删除平台:cordova plugin rm org.apache.cordova.camera

  • Remove

  • cordova platform remove android (将删除您对*.java文件所做的更改)

  • 添加平台:删除所有插件:cordova plugin add org.apache.cordova.camera

  • Check permissions

  • Build it*.java

这基本上就像创建一个新项目。

票数 1
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18650715

复制
相关文章

相似问题

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