javascript - 如何在 native 客户端和 javascript 中来回发送数组缓冲区

标签 javascript arrays google-chrome-app google-nativeclient chrome-native-messaging

我想将数组缓冲区从 Javascript 发送到 Native Client 模块,然后我想将数组缓冲区转换为整数指针。我在nacl-sdk目录下看到了earth的例子。他们正在传递图像数据并像这样转换它:

    //javascript

    var imageData = context.getImageData(0, 0, img.width, img.height);
// Send NaCl module the raw image data obtained from canvas.
common.naclModule.postMessage({'message' : 'texture',
                               'name' : name,
                               'width' : img.width,
                               'height' : img.height,
                               'data' : imageData.data.buffer});

    //nativeclient
    std::string name = dictionary.Get("name").AsString();
    int width = dictionary.Get("width").AsInt();
    int height = dictionary.Get("height").AsInt();
    pp::VarArrayBuffer array_buffer(dictionary.Get("data"));
    if (!name.empty() && !array_buffer.is_null()) {
      if (width > 0 && height > 0) {
        uint32_t* pixels = static_cast<uint32_t*>(array_buffer.Map());
        SetTexture(name, width, height, pixels);
        array_buffer.Unmap();

我正在使用 eclipse 调试,我不知道如何检查数组缓冲区是否被正确接收以及我是否可以将像素作为参数传递给某个函数或者我必须使用 pixels = new uint32_t 创建它们[size] 在通过之前。 更重要的是,我需要知道如何将 uint32_t* 像素转换为 VarArrayBuffer 并使用字典将其发送到 Javascript 并发布消息以及如何在 Javascript 中接收并处理作为 ArrayBuffer 值的消息。

最佳答案

最简单的示例是 SDK 中的 ArrayBuffer 示例 (examples/api/var_array_buffer)。

ArrayBuffer 的内存由 pp::VarArrayBuffer 拥有,所以只要你有对它的引用(并且你没有调用 pp::VarArrayBuffer::Unmap )你就不必复制内存.

pp::Var 变量会自动进行引用计数,因此您无需显式调用 AddRef .

关于javascript - 如何在 native 客户端和 javascript 中来回发送数组缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163547/

相关文章:

javascript - Selenium:如何检查匿名函数的代码

c++ - 迭代并连接 char 数组

c++ - 将数组 int 值转换为 double 值

javascript - 使用 tab.url.indexOf() 进行特定 URL 匹配

google-chrome-app - Kiosk 模式下的 Google Chrome 应用程序 Chrome 管理许可证序列号

html - HTML5 拖放与 Chrome 应用程序(打包)兼容吗?

javascript - 书籍之间的边距不是 css 3d 书籍封面中的大小

javascript - 在 Canvas 上绘制多个矩形

javascript - 当文本焦点(键盘打开)时,固定标题隐藏(消失)

比较 strcpy() 的两个实现