javascript - Phonegap(3.0.0) 相机第一次尝试不成功

标签 javascript android cordova camera

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

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

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

    <!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 ------------------

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

    (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 上试过它,它可以工作。所以肯定和插件有关系……

最佳答案

我不知道这是否是正确的解决方案,但它非常适合我。跟踪您的日志猫并找到确切的问题会更好。

尝试使用navigator.camera.PictureSourceType instated of pictureSource。所以它看起来像

<button onclick="getPhoto(navigator.camera.PictureSourceType.PHOTOLIBRARY);">From Photo Library</button><br>

在 Javascript 代码中也以同样的方式替换

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

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

更新: 尝试在本地保存你的 corodova.js 并从本地目录调用,所以你的目录应该像

assets/www/js/cordova.js

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

Working Code

希望这对你有帮助!!!

关于javascript - Phonegap(3.0.0) 相机第一次尝试不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650715/

相关文章:

Cordova 3.5插件权限错误(rm : could not remove file (code EACCES)

javascript - 在多个上下文之间传递数据

Android:如何创建 MotionEvent?

android - libgdx Viewports - 为什么当我使用 FitViewport 时触摸输入 x 和 y 不准确?

php - 如何处理使用不同方法登录的用户

javascript - 用于 PhoneGap 的 zepto.js 与 xuijs

javascript - MongoDB 对象未从数据库中删除

javascript - $scope.$watch 数组内的特定对象 - Angular JS Controller

javascript - jQuery - 表单验证