如何在cordova相机中获取相机图像的大小

时间:2015-08-12 09:54:30

标签: javascript cordova camera

所以,我正在使用这个cordova相机插件使用相机拍照。由于我的应用程序有将近10个div会激活相机,我想要配置服务器以获取我发送的二进制字符串的大小。我尝试了以下代码,它打印了二进制字符串而不是文件大小

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

      function onSuccess(imageData) {
          //var finalimage = "data:image/jpeg;base64," + imageData;
          window.resolveLocalFileSystemURI(imageData, function(fileEntry) {
        fileEntry.file(function(fileObj) {
            console.log("Size = " + fileObj.size);
        });
    }); 

这段代码给了我很棒的二进制字符串作为输出而不是文件大小。现在我也有另一个问题,在上面的评论中我将图像转换为base 64,那么会有change in the file size between final image and imageData吗?

1 个答案:

答案 0 :(得分:-1)

使用功能



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;


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




并在功能中使用"允许编辑"

您将获得完整的编辑和裁剪选项。 !!



  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
  //
  var x = 0;

  function onPhotoDataSuccess(imageURI) {
    x++;
    // Uncomment to view the base64-encoded image data
    console.log(imageURI);
    alert(imageURI);
    // Get image handle
    //
    var y = 'smallImage' + x;
    var smallImage = document.getElementById(y);

    // Unhide image elements
    //
    smallImage.style.display = 'block';


    // Show the captured photo
    // The in-line CSS rules are used to resize the image
    //
    smallImage.src = "data:image/jpeg;base64," + imageURI;
  }

  // Called when a photo is successfully retrieved
  //
  var z = 0;

  function onPhotoURISuccess(imageURI) {
    z++;
    // Uncomment to view the image file URI
    console.log(imageURI);
    alert(imageURI);


    // Get image handle
    //
    var w = 'largeImage' + z;
    var largeImage = document.getElementById(w);

    // Unhide image elements
    //
    largeImage.style.display = 'block';

    // Show the captured photo
    // The in-line 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,
      allowEdit: true,
      destinationType: destinationType.FILE_URI,
      sourceType: source
    });
  }

  // Called if something bad happens.
  //
  function onFail(message) {
    alert('Failed because: ' + message);
  }