如何在WinJS中编码ToSase64String一个Windows.Storage.Streams.IBuffer(Video)

时间:2014-02-27 01:56:26

标签: video encoding winjs

我有一个应用程序,用网络摄像头拍照,然后获取字节,然后我使用一种方法在base64上转换它,一切正常

然后我想添加视频并对其执行相同操作(获取字节并将其转换为base 64)但它不起作用,这是代码:

        var f = Windows.Storage.StorageFile.getFileFromPathAsync(capturedItem.path);

        return f.then(function (file) { return file.openAsync(Windows.Storage.FileAccessMode.read); }).then
            (function (stream) {
                var inputStream = stream.getInputStreamAt(0);
                var size        = stream.size;
                var reader      = new Windows.Storage.Streams.DataReader(inputStream);
                return reader.loadAsync(size).then(function () {
                    var b = reader.readBuffer(size);

                    //Creates the array, datalist and the namespace for making this data public                    
                    ImageData.dataArray[ImageData.dataArray.length] = { title: capturedItem.name, id: "img" + ImageData.dataArray.length, picture: photoBlobUrl };
                    ImageData.arrayCaptures.push(capturedItem); //Stores the data into array (full captured object)
                    ImageData.arrayTextFieldValues.push(Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(b)); //Stores captured converted into bytes

                    updateCaptureList(ImageData.dataArray); //updates the grid with all the screenshots
                });
            });

然后我想打印一个用于观察字节的日志:

console.log("variable: " + b.toString() + "lenght: " + b.length);

两种情况(图片和视频)的结果

variable: [object Windows.Storage.Streams.IBuffer]
lenght: 2087358

但是当我想看到Windows.Storage.Streams.IBuffer转换了我的console.log时这个

console.log("data: " + ImageData.arrayTextFieldValues[ImageData.arrayTextFieldValues.length - 1]);

就在图片上我得到了这样的结果:

data: /9j/4AAQSkZJRgABAQEAAAAAAAD/4QCCRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAABJADAAIAAAAUAAAAUJAEAAIAAAAUAAAAZJKRAAIAAAADODAAAJKSAAIAAAADODAAAAAAAAAyMDE0OjAyOjI2IDE5OjUyOjE5ADIwMTQ6MDI6MjYgMTk6NTI6MTkAAAD/4QGgaHR0cDovL25zLmFkb2JlLmNv.....

但是有了视频,我甚至无法打印日志,我认为失败的方法是:

Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String()

我想找一个将我的视频编码编码为base64的方法,任何帮助我都会感激

1 个答案:

答案 0 :(得分:0)

太晚了但是,我在你的片段中分享了我的注意事项:

您必须替换

console.log("variable: " + b.toString + "lenght: " + b.length);

console.log("variable: " + b.toString() + "lenght: " + b.length );

b.toString()是一种方法,之后你必须使用括号。因为call methodmethod,如果您通过function,那么整个variable就在您的b.toString中,因为您只需将其传递给variable: function toString() { [native code] } }。

这就是你的变量包含

的原因
{{1}}

它可以帮助你。