Canvas drawImage无法在Cordova中绘制,安全问题
Canvas drawImage does not draw in Cordova, security issue?
我想在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="" />
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 有没有一个javascript图形绘制库可以进行气球树布局
- 使用javascript存储变量的最安全方式
- 使用相同的数据集绘制各种符号
- 使用onclick绘制SVG路径
- 在谷歌地图上绘制位置数据库
- 用chart.js绘制条形图
- “可绘制地图”设置地图选项“纬度-经度”
- 多维数据集网格未在指定的分区中绘制
- 是否存在React Native“;WEB代码安全防护”;
- 内容安全策略:页面's设置阻止加载资源
- 使用谷歌图表在x轴上绘制日期
- 使用fabric.js从矩形区域获取对象,并将该区域绘制到画布上
- 如何在React Native中绘制图形
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 画布中绘制的矩形区域的弹出工具提示
- 如何在OpenLayers中获取动态绘制的多边形的坐标
- Canvas drawImage无法在Cordova中绘制,安全问题
- 可以'由于安全策略的原因,不要使用Raphael JS在Chrome扩展弹出窗口中绘制路径