Canvas drawImage无法在Cordova中绘制,安全问题

Canvas drawImage does not draw in Cordova, security issue?

本文关键字:绘制 安全 问题 Cordova drawImage Canvas      更新时间:2023-09-26

我想在Cordova应用程序中绘制一幅图像。

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

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

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

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

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