【问题标题】:Canvas drawImage does not draw in Cordova, security issue?Canvas drawImage 无法在 Cordova 中绘制,安全问题?
【发布时间】:2014-09-11 20:29:02
【问题描述】:

我想在 Cordova 应用程序中将图像绘制到画布上。

当图像路径在我的应用程序的www 目录中时,绘图按预期工作。 但是,如果图像是由 Cordova 相机制作的,因此存储在与www 目录相关的../../tmp 中,则drawImage(...) 会生成黑色图片。

这可能是一个安全问题,因为应用程序的源代码位于 www 目录中,但图像不是。另一方面,<img> 标签可以毫无问题地显示这些图像。

问题真的是安全问题吗?我能做些什么来解决它,即不产生黑色图片?

【问题讨论】:

    标签: javascript cordova canvas drawimage code-access-security


    【解决方案1】:

    尝试了无数次之后: 问题只是我想与drawImage() 一起使用的图像分辨率太高。降低分辨率使问题消失:画布不再是黑色的......(所以不是安全问题)

    【讨论】:

      【解决方案2】:

      听起来您正在尝试直接通过文件系统访问图像。您将需要使用 Cordova API 来检索图像

      http://cordova.apache.org/docs/en/2.5.0/cordova_camera_camera.md.html#camera.getPicture

      查看完整的检索示例

      var pictureSource;   // picture source
      var destinationType; // sets the format of returned value 
      
      // Wait for Cordova to connect with the device
      //
      document.addEventListener("deviceready",onDeviceReady,false);
      
      // Cordova is ready to be used!
      //
      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 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);
      }
      

      HTML

      <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="" />
      

      【讨论】:

        猜你喜欢
        • 2014-07-24
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-23
        相关资源
        最近更新 更多