Convert ArrayBuffer to blob

I have a project where I need to display djvu schemas in a browser.

I found this old library on Github , which, as I understand it, converts djvu files to bmp and then puts them in a canvas element.

As I said, the library is out of date (the last commit was 5 years ago), so I need to make some corrections. The main problem is that lib is using outdated BlobBuilder.

The steps I took to solve this problem:

  • Decompress this library through Chrome DevTools
  • The original error is on line 3774 var c = "undefined" != typeof MozBlobBuilder ? MozBlobBuilder : "undefined" != typeof WebKitBlobBuilder ? WebKitBlobBuilder : console.log("warning: cannot build blobs") var c = "undefined" != typeof MozBlobBuilder ? MozBlobBuilder : "undefined" != typeof WebKitBlobBuilder ? WebKitBlobBuilder : console.log("warning: cannot build blobs")
  • I commented on this line
  • Then I commented out the line c = new c; and some of the following lines.

So now it looks like this (the variable i is a buffer of arrays, and ololo1 and ololo2 are a kind of offset and limitation)

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

And now I'm stuck in the error "Image / bmp.bmp cannot be decrypted!" (selected in the h.onerror handler).

So my question is: what am I doing wrong?

+17
source share
2 answers

I don’t know why the author of Uint8Array his Uint8Array in a new one ... note that I don’t know any obsolete BlobBuilder API, but one typo that I see in your code is that you need to wrap your TypedArray in normal array:

 new Blob([new Uint8Array(buffer, byteOffset, length)]); 

The Blob constructor takes the blobParts sequence as the first parameter, and then searches for BufferSource, USVStrings, and Blob elements in that sequence. Therefore, when you pass a TypedArray, it actually iterates over all the entries of that TypedArray and treats them as a USVString (and thus converts their numeric value to UTF-8 strings in Blob). This is rarely what you want, so it's best to always pass an array in this constructor.

+54
source
 var buffer = new ArrayBuffer(32); new Blob([buffer]); 

so the Uint8Array must be

 new Blob([new Uint8Array([1, 2, 3, 4]).buffer]); 
0
source

Source: https://habr.com/ru/post/1268149/


All Articles