处理 imageData 的 Web Worker 使用 Firefox,但不适用于 Chrome

发布于 2024-12-25 16:06:25 字数 1267 浏览 2 评论 0原文

当我运行处理将 imageData 传递给网络工作者然后返回的代码时,Firefox 工作得很好,但 Chrome 给出了“未捕获的错误:DATA_CLONE_ERR:DOM 异常 25”

搜索 google 表明旧版本的 Chrome 曾经可以工作?

我又检查了一些,似乎我需要在发送图像数据之前运行 JSON.stringify 和 JSON.parse,但随后它就停止工作了。在 FF 9 中运行的代码是:

image.js:

var myImageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
var worker = new Worker("http://direct.link/helpers/worker.js");
worker.postMessage(myImageData);  
worker.onmessage = function(event) {
  var value = event.data;
  switch (value.cmd){
    case 'last':
      //doing stuff 
      break;
  default:
      //doing stuff
    });
}

worker.js:

addEventListener('message', function(event) {
  var myImageData = event.data;
  // doing stuff.
  sendItBack(colors);
});
};

    function sendItBack(colors){
    each(colors, function(index, value){
      self.postMessage(value);
    }); 
    self.postMessage({'cmd': 'last'});
    }

我应该使用什么方法来将此图像数据来回发送到应用程序和 Web Worker?

谢谢!

编辑:

如果我复制到常规数组,则 Chrome 开始工作...

var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];

因此 chrome 无法将 CanvasPixelArray 传递给工作人员,但它可以传递常规数组。但火狐可以。

When I run code that deals with imageData being passed to a web worker and then back, then Firefox works great but Chrome gives "Uncaught Error: DATA_CLONE_ERR: DOM Exception 25"

Searching google suggests that older versions of Chrome used to work?

I checked some more and it seemed as if I needed to run JSON.stringify and JSON.parse on the imagedata before sending it but then it stops working everywhere. The code that works in FF 9 is:

image.js:

var myImageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
var worker = new Worker("http://direct.link/helpers/worker.js");
worker.postMessage(myImageData);  
worker.onmessage = function(event) {
  var value = event.data;
  switch (value.cmd){
    case 'last':
      //doing stuff 
      break;
  default:
      //doing stuff
    });
}

worker.js:

addEventListener('message', function(event) {
  var myImageData = event.data;
  // doing stuff.
  sendItBack(colors);
});
};

    function sendItBack(colors){
    each(colors, function(index, value){
      self.postMessage(value);
    }); 
    self.postMessage({'cmd': 'last'});
    }

What method should I use in order to send this imagedata back and forth the app and the web worker?

Thanks!

EDIT:

If I copy to a regular array then Chrome starts working...

var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];

So chrome can't pass a CanvasPixelArray to a worker but it can pass a regular Array. But firefox can.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2025-01-01 16:06:25

我所做的是将整个 imagedata 对象从上下文传递给工作人员,而不仅仅是数据:

var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);

// This works beautifully
worker.postMessage({imgData:imgData});

// But this throws the exception
worker.postMessage({imgData:imgData.data});

使用这种方法,您将一些额外的属性传递给工作人员(即数组的宽度和高度),但我认为这位额外数据比复制整个数据数组相关的开销要好。

What I do is pass the entire imagedata object from the context to the worker, and not just the data:

var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);

// This works beautifully
worker.postMessage({imgData:imgData});

// But this throws the exception
worker.postMessage({imgData:imgData.data});

With this approach you're passing a few extra attributes to the worker (namely, the array's width and height), but I think this bit of extra data is better than the overhead associated with copying the entire data array.

看春风乍起 2025-01-01 16:06:25

将此作为后续发布。我可以重现您的错误,以防其他人可以回答您的问题(不幸的是我不能)。我搜索了 Chromium 问题,看看这是否是一个突出的错误,但没有发现任何东西。鉴于图像处理是 WebWorkers 更流行的用途之一,我希望有人能够快速回答您。

http://jsfiddle.net/gGFSJ/9/

来自 Chrome(星号是我添加的):

window.URL does not exist
window.WebKitURL does not exist
using window.webkitURL for URL
window.BlobBuilder does not exist
using window.WebKitBlobBuilder for BlobBuilder
***Uncaught Error: DATA_CLONE_ERR: DOM Exception 25***
data=send back.
data=to worker.
data=send back.
data=0.

来自火狐浏览器:

using window.URL for URL
window.BlobBuilder does not exist
window.WebKitBlobBuilder does not exist
window.webkitBlobBuilder does not exist
using window.MozBlobBuilder for BlobBuilder
data=send back.
data=to worker.
data=send back.
data=0.
data=send back.
data=[object Uint8ClampedArray].

Posting this as follow up. I can reproduce your error in case anyone else can answer your question (I can't unfortunately). I've searched the Chromium issues to see if it's an outstanding bug, but not found anything. Given image processing is one of the more popular uses of WebWorkers I would hope someone can answer you quickly.

http://jsfiddle.net/gGFSJ/9/

From Chrome (asterisks added by me):

window.URL does not exist
window.WebKitURL does not exist
using window.webkitURL for URL
window.BlobBuilder does not exist
using window.WebKitBlobBuilder for BlobBuilder
***Uncaught Error: DATA_CLONE_ERR: DOM Exception 25***
data=send back.
data=to worker.
data=send back.
data=0.

From Firefox:

using window.URL for URL
window.BlobBuilder does not exist
window.WebKitBlobBuilder does not exist
window.webkitBlobBuilder does not exist
using window.MozBlobBuilder for BlobBuilder
data=send back.
data=to worker.
data=send back.
data=0.
data=send back.
data=[object Uint8ClampedArray].

如果我将 getimagedata.data 数组复制到常规数组,然后将该数组传递给网络工作者,那么 Chrome 就会开始工作。

var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];

因此,chrome 无法将 CanvasPixelArray 传递给工作人员,但它可以传递常规数组。但 Firefox 可以直接传递imagedata.data

If I copy the getimagedata.data array to a regular array and then pass the array to a webworker then Chrome starts working.

var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];

So chrome can't pass a CanvasPixelArray to a worker but it can pass a regular Array. But Firefox can pass an imagedata.data directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文