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 - 在开发环境中全页加载时加快 Rails 加载 Backbone Javascript Assets 的方法?

sockets - chrome.sockets API tcp 错误代码?

javascript - 如何将JS函数结果转换为HTML表单?

javascript - 如何在 Chrome 打包应用程序的 list 中使用 "system_indicator"?

javascript - 如何在 Chrome OS 或 Chrome 扩展程序的打包应用程序中启用摄像头和麦克风?

javascript - 更改 AudioContext (getUserMedia) 的采样率

javascript - 没有d3.js的矩形数组

javascript - 带有 Angular Web应用程序的Fabric Crashlytics

javascript - 将值的引用存储在数组中,然后重新分配原始变量(JavaScript)

c - 出现在 C 数组末尾的随机字符