如何读取javascript中的前4个字节,将其转换为整数并删除其余部分? [英] How do I read out the first 4 bytes in javascript, turn it into an integer and remove the rest?

查看:120
本文介绍了如何读取javascript中的前4个字节,将其转换为整数并删除其余部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Internet将网络摄像头数据从一个浏览器传输到另一个浏览器.

I need to transfer webcam data over the internet from one browser to another.

网络摄像头显示在HTML5 canvas中.然后,获取其dataUrl,并将其转换为blob.然后,将这个Blob发送到我的服务器.

The webcam is displayed in a HTML5 canvas. Then I get its dataUrl, and turn it into a blob. Then I send this blob to my server.

据我了解,blob本质上是一个byte array.我已经将其转换为服务器端的字节数组,它的长度与浏览器中的blob.size相同,所以看起来还不错.我需要在其中添加一个发件人ID,因此我将一个整数转换为4 bytes的数组并将其添加到字节数组的前面.

From what I understand, the blob is essentially a byte array. I've converted it to a byte array on the server-side, it has the same length as blob.size in the browser, so that seems fine. I need to add a sender id to it, so I convert an integer to an array of 4 bytes and add it to the front of the byte array.

现在,我需要将此修改后的Blob发送到其他浏览器.这就是我感到困惑的地方.

Now I need to send this modified blob to the other browser. And this is where I'm confused.

  1. 如何读取javascript中的前4个字节并将其再次转换为整数?
  2. 我如何切断其余的斑点?

推荐答案

您可以使用Blob.slice()方法获取字节.
请参阅文档: https://developer.mozilla.org/zh-美国/docs/Web/API/Blob.slice

You can use Blob.slice() method to get bytes.
See docs: https://developer.mozilla.org/en-US/docs/Web/API/Blob.slice

1)

var bytes = blob.slice(0,4);

2)

var arrayOfBytes = [];
for(var i=0;i<blob.size/4;i++){
    arrayOfBytes[i] = blob.slice(i*4,4+(i*4));
}

注意:我没有测试!

这篇关于如何读取javascript中的前4个字节,将其转换为整数并删除其余部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆