如何在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);
        });
    }); 

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

使用函数

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

并在"allow edit"函数中使用

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

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

最新更新