如何从Windows.Storage.StorageFile WinJS加载blob

时间:2013-12-12 22:55:26

标签: javascript image visual-studio-2012 blob winjs

我正在使用WinJS开发一个Metro应用程序,它打开我的相机,拍摄并创建一张图片,将结果存储在Windows.Storage.StorageFile对象( capturedItem )上我想从它们创建一个blob对象

这是我的方法:

 function imageCapture() {

        var captureUI = new _capture.CameraCaptureUI();

        captureUI.photoSettings.format = _capture.CameraCaptureUIPhotoFormat.jpeg;
        captureUI.captureFileAsync(_capture.CameraCaptureUIMode.photo)
            .then(function (capturedItem) {
                if (capturedItem) {

                    var photoBlobUrl = URL.createObjectURL(
                        capturedItem,
                        { oneTimeOnly: true });

                    imageElement = document.createElement("img");
                    imageElement.id = "img1";
                    imageElement.setAttribute("src", photoBlobUrl);

                    _divPicture.appendChild(imageElement);

                    // prints in textfield blob value
                    document.getElementById("field1").value = capturedItem.path;
                }
            }
        );
    }

1 个答案:

答案 0 :(得分:0)

你其实非常接近。您应该首先从capturedItem创建一个文件。像这样:

var file = MSApp.createFileFromStorageFile(capturedItem);
// than use the file variable in the createObjectURL function
var url = URL.createObjectURL(file, { oneTimeOnly: true });
var imageElement = document.createElement("img");
imageElement.id = "img1";
imageElement.setAttribute("src", url);
_divPicture.appendChild(imageElement);