简体   繁体   English

在javascript和Html5中从Array创建图像

[英]Creating image from Array in javascript and Html5

Here is my code. 这是我的代码。 I created imageData 2D array in javascript. 我在javascript中创建了imageData 2D数组。 After I put all pixels into the this array, I want to create image and put these values into the image. 将所有像素放入此数组后,我想创建图像并将这些值放入图像中。

    var imageData = new Array(width);

    var image = new Image;

for (var i = 0; i < width; i++) {
    imageData[i] = new Array(height);
}

    image.src = imageData; //Here is the problem. I want to do that. 

To create an image from array you can do this: 要从数组创建图像,您可以执行以下操作:

var width = 400,
    height = 400,
    buffer = new Uint8ClampedArray(width * height * 4); // have enough bytes

The * 4 at the end represent RGBA which we need to be compatible with canvas. 最后的* 4表示我们需要与画布兼容的RGBA。

Fill the buffer with some data, for example: 使用一些数据填充缓冲区,例如:

for(var y = 0; y < height; y++) {
    for(var x = 0; x < width; x++) {
        var pos = (y * width + x) * 4; // position in buffer based on x and y
        buffer[pos  ] = ...;           // some R value [0, 255]
        buffer[pos+1] = ...;           // some G value
        buffer[pos+2] = ...;           // some B value
        buffer[pos+3] = 255;           // set alpha channel
    }
}

When filled use the buffer as source for canvas: 填充时使用缓冲区作为画布的来源:

// create off-screen canvas element
var canvas = document.createElement('canvas'),
    ctx = canvas.getContext('2d');

canvas.width = width;
canvas.height = height;

// create imageData object
var idata = ctx.createImageData(width, height);

// set our buffer as source
idata.data.set(buffer);

// update canvas with new data
ctx.putImageData(idata, 0, 0);

Note that you could use the imageData buffer (here: idata.data) instead of creating your own. 请注意,您可以使用imageData缓冲区(此处为:idata.data)而不是创建自己的缓冲区。 Creating your own is really only useful if you use for example floating point values or get the buffer from some other source - setting the buffer as above will take care of clipping and rounding the values for you though. 如果你使用例如浮点值或从其他来源获取缓冲区,那么创建自己的实际上非常有用 - 设置缓冲区如上所述将为你剪切和舍入值。

Now the data in your custom array is copied to the canvas buffer. 现在,自定义数组中的数据将复制到画布缓冲区。 Next step is to create an image file: 下一步是创建一个图像文件:

var dataUri = canvas.toDataURL(); // produces a PNG file

Now you can use the data-uri as source for an image: 现在您可以使用data-uri作为图像的源:

image.onload = imageLoaded;       // optional callback function
image.src = dataUri

You can't make an image.src like that. 你不能像这样制作一个image.src。

A valid dataURL is a base64 encoded string with a type prefix--not an array. 有效的dataURL是base64编码的字符串,带有类型前缀 - 而不是数组。

The image data array associated with a canvas is an Uint8ClampedArray--not a normal javascript array. 与画布关联的图像数据数组是Uint8ClampedArray - 不是普通的javascript数组。

Here's one way to create a pixel array that you can manipulate: 这是创建可以操作的像素阵列的一种方法:

A Demo: http://jsfiddle.net/m1erickson/956kC/ 演示: http//jsfiddle.net/m1erickson/956kC/

// create an offscreen canvas
var canvas=document.createElement("canvas");
var ctx=canvas.getContext("2d");

// size the canvas to your desired image
canvas.width=40;
canvas.height=30;

// get the imageData and pixel array from the canvas
var imgData=ctx.getImageData(0,0,40,30);
var data=imgData.data;

// manipulate some pixel elements
for(var i=0;i<data.length;i+=4){
    data[i]=255;   // set every red pixel element to 255
    data[i+3]=255; // make this pixel opaque
}

// put the modified pixels back on the canvas
ctx.putImageData(imgData,0,0);

// create a new img object
var image=new Image();

// set the img.src to the canvas data url
image.src=canvas.toDataURL();

// append the new img object to the page
document.body.appendChild(image);

Create a canvas element of the right size and get its 2D rendering context. 创建大小合适的canvas元素并获取其2D渲染上下文。 You don't have to add this canvas to the document. 您不必将此画布添加到文档中。 Use the context to create an ImageData object. 使用上下文创建ImageData对象。 Copy the values from your array into the ImageData object. 将数组中的值复制到ImageData对象中。 In your case, it might be more efficient to populate the ImageData object in the first place, instead of a separate array. 在您的情况下,首先填充ImageData对象可能更有效,而不是单独的数组。 Use the context's putImageData to draw the pixel data. 使用上下文的putImageData绘制像素数据。 Then, depending on the specific requirements of "Creating image," you might need to serialize the canvas into a data uri, so that you can fill in an img element's src . 然后,根据“创建图像”的具体要求,您可能需要将画布序列化为数据uri,以便您可以填写img元素的src

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

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