How can I get back an array of objects from Worker?

帅比萌擦擦* 提交于 2019-12-13 04:34:35

问题


I have a simple Pixel pseudoclass that has .H(), .S() and .L() methods. I use these in image analysis - like this watermark detection:

To get the objects out of ImageData, I just loop through the ImageData and create multi-dimensional array:

  //Get image data
  var id = ctx.getImageData(0, 0, canvas1.width, canvas1.height);
  //The Uint8 array
  var pixels = id.data;
  //Dimensions
  var length = pixels.length;
  var width = id.width;
  var height = id.height;
  //Create empty array for 2D data
  var pixels2D = [];
  //Loop vertically
  for (var y = 0; y < height; y++) {
    //Create 2nd level array
    pixels2D.push([]);            
    //Cache the vertical offset
    var ofy = y * width;
    //Loop horizontally
    for (var x = 0; x < width; x++) {
      //Calculate X offset
      var off = (ofy + x) * 4;
      //Create new object
      pixels2D[y].push(new Pixel([pixels[off], pixels[off + 1], pixels[off + 2], pixels[off + 3]]));
    }
  }

For large images, this takes a while. Recetly, I found out about Workers. It seems like the best way to perform asynchronous CPU-demanding tasks, so that my browser doesn't freeze.

It's possible to pass UInt8Array to worker - but how can I then retrieve the pseudo-Class instance array? Pseudo-Class instances in javascript can't be serialised...

来源:https://stackoverflow.com/questions/26508887/how-can-i-get-back-an-array-of-objects-from-worker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!