我被一个问题困住了,我不知道下一步该做什么。
在我的应用程序中,我为我的用户提供了一个选择,要么拍摄一张照片,要么从画廊中选择一张照片。这部分工作正常,问题的产生与保存\阅读照片。让我们从摄像机调用的角度看这个问题。
function startCameraApp() {
PhotoTaken = false;
blackberry.event.addEventListener("onChildCardClosed", sharePhoto);
blackberry.invoke.invoke({
target: "sys.camera.card"
}, onInvokeSuccess, onInvokeError);
}在sharePhoto中我有以下代码..。
function sharePhoto(request) {
var image = new Image();
image.src = "file://" + request.data;
image.onload = function () {
// Now here I need to read the image file and convert it into base64.
var resized = resizeMe(image); // the resizeMe function is given below and it simply makes my image smaller
var imagedata = resized.split("base64,");
sessionStorage.setItem("MyNewPicture", imagedata);
}
}
function resizeMe(img) {
var canvas = document.createElement('canvas');
var max_width = 600;
var max_height = 600;
var width = img.width;
var height = img.height;
// calculate the width and height, constraining the proportions
if (width > height) {
if (width > max_width) {
height = Math.round(height * max_width / width);
width = max_width;
}
} else {
if (height > max_height) {
width = Math.round(width * max_height / height);
height = max_height;
}
}
//resize the canvas and draw the image data into it
img.width = width;
img.height = height;
canvas.width = width;
canvas.height = height;
canvas.classname += "ui-hidden";
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
return canvas.toDataURL();
}因此,这个应用程序运行,它拍摄了照片,一切看起来都很好,但上传到本地存储的数据只是一个空白屏幕。它100%的工作在黑莓10模拟器,但不是在我的设备上。在设备上,它保存了一个空字符串。
编辑
好的。所以我把这个添加到我的函数中,用于测试,我仍然被困住了,我不知道该怎么做……
function sharePhoto(request) {
var image = new Image();
image.src = "file://" + request.data;
image.onload = function () {
// Now here I need to read the image file and convert it into base64.
var resized = resizeMe(image); // the resizeMe function is given below and it simply makes my image smaller
var imagedata = resized.split("base64,");
alert(imagedata); // This returns a blank popup
sessionStorage.setItem("MyNewPicture", imagedata);
}
}发布于 2013-11-26 18:46:00
我相信当您使用split方法时,它会返回一个数组,所以您可以这样访问它:
var resized = resizeMe(image);
var imagedata = resized.split("base64,");
imagedata = imagedata[1]; // this gives you everything after the 'base64,' string但是,我看到的主要问题是,您正在拆分imagedata字符串,该字符串从数据中删除了整个“这是一个图像”前缀。
当您将imagedata显示为图像时,您需要有数据: image /jpeg;base64 64,前缀。
所以说,你的图像来源是
data:image/jpeg;base64,<rest of base64 string here>发布于 2013-11-27 05:18:55
我需要在我的应用程序的config.xml中包含一个额外的元素。
<access subdomains="true" uri="file://accounts/"></access>
<access subdomains="true" uri="file://accounts/100/shared/camera/"></access>这使您的应用程序可以访问这些文件夹及其包含的文件。模拟器的不需要这个权限。
https://stackoverflow.com/questions/20197794
复制相似问题