Canvas drawImage不会在Cordova中绘制安全问题?

时间:2014-09-11 20:29:02

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

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

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

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

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

2 个答案:

答案 0 :(得分:4)

尝试了无穷无尽的事情之后: 问题很简单,我想用drawImage()的图像太高了。降低分辨率使问题消失:画布不再是黑色......(所以不是安全问题)

答案 1 :(得分:1)

听起来您正试图直接通过文件系统访问图像。您将需要使用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="" />