【问题标题】:Blackberry webworks 10 using image from picture taken\uploadedBlackberry webworks 10 使用图片中的图片拍摄\上传
【发布时间】:2013-11-27 13:18:55
【问题描述】:

我遇到了一个问题,不知道下一步该尝试什么。

在我的应用程序中,我为我的用户提供了一个选择,即拍摄照片或从图库中选择照片。这部分工作正常,问题出现在照片的保存\阅读上。让我们从相机调用的角度来看。

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();
}

所以应用程序运行并拍摄照片,一切看起来都很好,但上传到本地存储的数据只是一个空白屏幕。它在黑莓 10 模拟器中 100% 工作,但在我的设备上却不行。在设备上它保存一个空字符串。

编辑

好的。所以我将此添加到我的函数中以进行测试,但我仍然卡住了,我不知道该怎么办......

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);
    }
}

【问题讨论】:

    标签: blackberry-10 blackberry-webworks


    【解决方案1】:

    我相信当你使用 split 方法时它会返回一个数组,所以你可以这样访问它:

    var resized = resizeMe(image);
    var imagedata = resized.split("base64,");
        imagedata = imagedata[1]; // this gives you everything after the 'base64,' string
    

    但是,我看到的主要问题是您正在拆分图像数据字符串,这会从数据中删除整个“这是一个图像”前缀。

    将imagedata显示为图片时,需要data:image/jpeg;base64, 前缀也是。

    也就是说,你的图片来源是

    data:image/jpeg;base64,<rest of base64 string here>
    

    【讨论】:

    • split 确实返回了一个数组。然而,这不是我遇到问题的原因。我使用 base64,l 作为拆分,以便我可以轻松检索 base64 字符串。您可以在下面查看我的回答,了解导致我的问题的原因。感谢您的回复。
    【解决方案2】:

    我需要在我的应用程序的 config.xml 中包含一个额外的元素。

    <access subdomains="true" uri="file://accounts/"></access>
    <access subdomains="true" uri="file://accounts/100/shared/camera/"></access>
    

    这使您的应用程序可以访问这些文件夹及其包含的文件。模拟器不需要此权限。

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多