缩小相机插件的图库照片

时间:2014-12-14 15:07:17

标签: cordova windows-phone-8 camera windows-phone-8.1 image-gallery

我正在使用Cordova 2.6相机插件,从设备库中获取图像,我使用base64编码获取它们,我试图使用"质量"将它们压缩到更低的质量。选项。

我注意到压缩不起作用,当我在https://github.com/apache/cordova-plugin-camera/blob/master/doc/index.md阅读Cordova文档时,我可以读到:

  

注意:较新设备上的照片分辨率非常好。 已选择照片   甚至,来自设备的画廊也没有缩减到较低的质量   如果指定了质量参数。为避免常见的内存问题,   将Camera.destinationType设置为FILE_URI而不是DATA_URL。

在我的情况下,我只能使用DATA_URL,因为我使用base64加密来与第三方webservice上传图像。我也只使用设备库中的图像(不是来自相机本身)。 我遇到了一些性能问题,主要是在Windows Phone上。由于图像大小,我的应用程序需要花费太多时间来处理base64数据(我正在测试设备库中保存的大图像,但使用5 MP或8 MP手机的相机拍摄)。

我的问题是有一个解决方案来获得"质量"适用于Cordova插件中的图库照片的选项?

我们可以压缩设备库照片吗?

你建议还有其他选择吗? (例如,使用FILE_URI并返回压缩图像base64加密的自定义本机插件)

2 个答案:

答案 0 :(得分:0)

我们在调用.getPicture时使用以下选项:

quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
encodingType: Camera.EncodingType.JPEG,
sourceType: Camera.PictureSourceType.CAMERA,
targetWidth: 800,
correctOrientation: true

但质量参数似乎对文件大小没有太大影响。 targetWidth和,由于一些奇怪的原因,correctOrientation做。这些设置的结果图片大小约为24kB,具体取决于设备的相机分辨率。

答案 1 :(得分:0)

我在从相机或图库中拾取任何照片后正在使用下一个功能。我正在使用插件cordova-plugin-simple-image-resizer

  function resizeImageIfNeeded (imageUri, callback) {
    // if image larger than this, resize
    var MAX_IMG_FILE_SIZE = 307200 // 300kb

    app.functions.getFileSize(imageUri, function (fileSize, err) {
      if (!err && fileSize && fileSize < MAX_IMG_FILE_SIZE) {
        // no need to resize image, return image unchanged
        console.log('Image Not resized (file already small): ' + imageUri)
        callback(imageUri)
      } else {
        // resize image (try even if file size is not obtained)
        resizeImage(imageUri, function (resizedImageUri, err) {
          if (err) {
            // could not resize image
            callback(imageUri, Error(err))
          }
          // return resized image
          callback(resizedImageUri)
        })
      }
    })
  }

  function resizeImage (imageUri, callback) {
    // get just fileName with suffix, ex.: "photo1_resized.jpg"
    var fileNameResized = addSuffixToFileName(
      getFilenameFromURL(imageUri)[1],
      '_resized'
    )

    var resizeOptions = {
      uri: imageUri,
      fileName: fileNameResized,
      quality: 90,
      width: 1200,
      height: 1200,
      base64: false
    }

    window.ImageResizer.resize(resizeOptions,
      function (resizedImageUri) {
        // success on resizing
        console.log('%c Image resized: ', 'color: green; font-weight:bold', resizedImageUri)
        callback(resizedImageUri)
      },
      // failed to resize
      function (err) {
        console.log('%c Failed to resize: ', 'color: red; font-weight:bold')
        callback(imageUri, Error(err))
      })
  }

  function getFileSize (fileUri, callback) {
    var fileSize = null
    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
      fileEntry.file(function (fileObj) {
        fileSize = fileObj.size
        callback(fileSize)
      },
      function (err) {
        console.error('fileEntry error:\n', JSON.stringify(err))
        callback(fileSize, Error(err))
      })
    },
    function (err) {
      console.error('resolveLocalFileSystemURL error:\n', JSON.stringify(err))
      callback(fileSize, Error(err))
    })
  }

  // ex: from "file:///storage/emulated/0/Android/data/com.form.parking.violation/cache/1525698243664.jpg"
  // output[0] == "file:///storage/emulated/0/Android/data/com.form.parking.violation/cache"
  // output[1] == "1525698243664.jpg"
  function getFilenameFromURL (url) {
    if (!url) {
      return false
    }
    var output = []
    output[1] = url.split('/').pop()
    output[0] = url.substring(0, url.length - output[1].length - 1)
    return output
  }