简体   繁体   English

如何从JPG的arrayBuffer表示创建canvas imageData数组

[英]How can I create a canvas imageData array from an arrayBuffer representation of a JPG

First of all I am aware there are standard methods of achieving this (readAsDataURL and drawImage), but unfortunately they are not usable for this specific use case. 首先,我知道有一些实现这一目标的标准方法(readAsDataURL和drawImage),但遗憾的是它们不能用于这个特定的用例。

I am reading an image using the filereader API as an arraybuffer like so: 我正在使用filereader API作为arraybuffer读取图像,如下所示:

    var reader = new fileReader();
    reader.onload = function(e){

          var byteArray = new Uint8ClampedArray(e.target.result);
          //do stuff to this array
    }
    reader.readAsArrayBuffer(file);

I am then creating a clampedarray with this returned data. 然后我用这个返回的数据创建一个clampedarray。

What I want to be able to do is now draw this pixel array directly to a canvas using putImageData 我现在想要做的是使用putImageData将这个像素数组直接绘制到画布putImageData

Currently I am doing the following: 目前我正在做以下事情:

var byteArray = new Uint8ClampedArray(e.target.result);
var imgdata = ctx.createImageData(img.width, img.height);
var canvdata = imgdata.data;

for (var i = 0; i < canvdata.length; i += 4) {
    canvdata[i] = byteArray[i];
    canvdata[i + 1] = byteArray[i + 1];
    canvdata[i + 2] = byteArray[i + 2];
    canvdata[i + 3] = byteArray[i + 3];
}
ctx.putImageData(imgdata, 0, 0);

Using this method, although the data gets drawn to the canvas, it appears like garbled snow or noise and is not a correct representation of the image. 使用此方法,虽然数据被绘制到画布,但它看起来像是雪花或噪音,并且不是图像的正确表示。

This leads me to believe that I am constructing the imagedata data incorrectly in my loop or perhaps my initial method of getting the pixel array is incorrect. 这让我相信我在循环中错误地构建了imagedata数据,或者我的初始获取像素数组的方法可能不正确。

To summarize, I would like to be able to take an arraybuffer (of a jpeg) retrieved via the html5 fileReader API and then create a canvas compatible imageData array, so that it can later be pushed into a canvas using putImageData 总而言之,我希望能够通过html5 fileReader API获取一个arraybuffer(jpeg),然后创建一个与canvas兼容的imageData数组,以便稍后可以使用putImageData将其推送到画布中

Thanks in advance. 提前致谢。

Edit 编辑

A JPEG file isn't a simple byte-array of colour data, and therefore you can't simply load it in like this. JPEG文件不是简单的颜色数据字节数组,因此您不能像这样简单地加载它。 If you want to get around this without importing directly into a canvas you'll have to use a JPEG decoding library, such as this project by notmasteryet which I found via a quick google search. 如果你想绕过它而不直接导入画布,你将不得不使用JPEG解码库,例如我通过快速谷歌搜索找到的notmasteryet项目

Original 原版的

unfortunately they are not usable for this specific use case 不幸的是,它们不能用于这个特定的用例

Why are they not usable? 为什么它们不可用?

// example array
u = new Uint8ClampedArray(4);
u[0] = 255, u[1] = 56, u[2] = 201, u[3] = 8; // [255, 56, 201, 8]
// to String
str = String.fromCharCode.apply(null, u); // "ÿ8É"
// to Base64
b64 = btoa(str); // "/zjJCA=="
// to DataURI
uri = 'data:image/jpeg;base64,' + b64; // "data:image/jpeg;base64,/zjJCA=="

And the reverse 反之亦然

// uri to Base64
b64 = uri.slice(uri.indexOf(',')+1);
// to String
str = atob(b64);
// to Array
arr = str.split('').map(function (e) {return e.charCodeAt(0);});
// to Uint8ClampedArray
u = new Uint8ClampedArray(arr); // [255, 56, 201, 8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM