Javascript Canvas drawImage不在Cordova中绘制,安全问题?
我想在Cordova应用程序中绘制画布上的图像 当图像路径位于我的应用程序的Javascript Canvas drawImage不在Cordova中绘制,安全问题?,javascript,cordova,canvas,drawimage,code-access-security,Javascript,Cordova,Canvas,Drawimage,Code Access Security,我想在Cordova应用程序中绘制画布上的图像 当图像路径位于我的应用程序的www目录中时,图形将按预期工作。 但是,如果图像是由Cordova摄像机制作的,因此相对于www目录存储在./../tmp中,则drawImage(…)会生成黑色图片 这可能是一个安全问题,因为应用程序的源代码位于www目录中,但图像不在其中。另一方面,标记可以毫无问题地显示这些图像 这个问题真的是安全问题吗?我能做些什么来解决这个问题,即不生成黑色图片?听起来您正试图通过文件系统直接访问图像。您需要使用Cordova
www
目录中时,图形将按预期工作。
但是,如果图像是由Cordova摄像机制作的,因此相对于www
目录存储在./../tmp
中,则drawImage(…)
会生成黑色图片
这可能是一个安全问题,因为应用程序的源代码位于www
目录中,但图像不在其中。另一方面,
标记可以毫无问题地显示这些图像
这个问题真的是安全问题吗?我能做些什么来解决这个问题,即不生成黑色图片?听起来您正试图通过文件系统直接访问图像。您需要使用Cordova API来检索图像 请参阅完整的检索示例
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
来自照片库的
来自相册
尝试了无数次之后:
问题只是我想与drawImage()
一起使用的图像分辨率太高。降低分辨率使问题消失:画布不再是黑色的。。。(因此不是安全问题)
<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="" />