ArrayBuffer到Blob的转换 [英] ArrayBuffer to blob conversion
问题描述
我有一个项目,需要在浏览器中显示djvu模式.
I have a project where I need to display djvu schemas in browser.
我在Github上找到了这个旧的库,据我了解,该库可以转换djvu文件到bmp,然后将其放入canvas元素.
I found this old library on Github which, as far as I understood, converts djvu files to bmp and then puts them into canvas element.
正如我所说,库是旧的(最后一次提交是在5年前),因此我需要进行一些更正.主要问题是lib使用过时的BlobBuilder.
As I said, library is old(last commit was 5 years ago), so I need to make some corrections. Main problem is that lib uses obsolete BlobBuilder.
我为解决此问题而采取的步骤:
Steps I made to solve this problem:
- 通过Chrome DevTools解压缩该库
- 初始错误在第3774行
var c = "undefined" != typeof MozBlobBuilder ? MozBlobBuilder : "undefined" != typeof WebKitBlobBuilder ? WebKitBlobBuilder : console.log("warning: cannot build blobs")
- 我注释了这一行
- 接下来,我也注释了
c = new c;
行以及以下几行.
- Decompress this library via Chrome DevTools
- Initial error is at line 3774
var c = "undefined" != typeof MozBlobBuilder ? MozBlobBuilder : "undefined" != typeof WebKitBlobBuilder ? WebKitBlobBuilder : console.log("warning: cannot build blobs")
- I commented out this line
- Next, I commented out line
c = new c;
and some of following lines too.
所以,现在看起来是这样(变量I是数组缓冲区,而ololo1和ololo2是某种偏移量和限制)
So, now it looks this way(variable I is array buffer, and ololo1 and ololo2 are some kind of offset and limit)
var c = new Blob(new Uint8Array(new Uint8Array(I,ololo1,ololo2)))
, b = b.createObjectURL(c)
, c = document.getElementById(kb)
, f = c.getContext("2d")
, h = new Image
, g = a[Ea >> 2]
, i = a[Fa >> 2]
, j = c.width
, k = Math.round(i * j / g);
h.onload = function()
{
var a = g / j;
4 < a && (a = 4);
1 > a && (a = 1);
f.globalAlpha = 1;
for (N = 0; N < a; N++)
f.drawImage(h, N, N, g - a + N, i - a + N, 0, 0, j, k),
f.globalAlpha *= 1 - 1 / a;
R(h.complete, "Image /bmp.bmp could not be decoded")
}
;
h.onerror = function(errorMsg, url, lineNumber, column, errorObj) {
console.log(errorMsg, url, lineNumber, column, errorObj);
console.log("Image /bmp.bmp could not be decoded!")
}
;
现在我停留在错误"Image/bmp.bmp无法解码!"(扔到h.onerror处理程序中).
And now I stuck at error "Image /bmp.bmp could not be decoded!"(throwed in h.onerror handler).
所以,我的问题是:我做错了什么?
So, my question is: what I am doing wrong?
推荐答案
我不知道作者为什么将他的Uint8Array
包装在一个新的...中,请注意,我真的不知道这两个弃用的BlobBuilder API,但我在您的代码中看到的一个错字是您需要将TypedArray包装在普通的Array中:
I don't know why the author did wrap his Uint8Array
in an new one... note that I don't really know either the deprecated BlobBuilder API, but one typo I can see in your code is that you need to wrap your TypedArray in a normal Array:
new Blob([new Uint8Array(buffer, byteOffset, length)]);
Blob构造函数将 blobParts序列作为第一个参数,然后搜索此序列中的 BufferSource , USVStrings 和 Blob 元素.因此,当您传递 TypedArray 时,它将实际上遍历此 TypedArray 的所有条目,并将其视为 USVString (从而将其数值转换为数字).值( Blob 中的UTF-8字符串).那很少是您想要的,所以最好总是在此构造函数中传递一个 Array .
The Blob constructor takes a blobParts sequence as first parameter, and then searches for BufferSource, USVStrings and Blob elements in this sequence. So when you pass a TypedArray, it will actually iterate over all the entries of this TypedArray and treat these as USVString (and thus convert their numerical value to UTF-8 strings in the Blob). That's rarely what you want, so better always pass an Array in this constructor.
这篇关于ArrayBuffer到Blob的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!