将原始图像转换为缓冲区
Convert raw image to buffer
在 Raspberry Pi 中显然没有简单的流式传输图像的方法。虽然有许多 hacks 可用,但在我的 Raspberry Pi 零中它在保持合适的帧率方面遇到了一些麻烦。
我怀疑主要问题之一是每个图像的 1st Google solution 和大多数 writes/reads 到 SD。到目前为止,我已经在不接触 SD 的情况下从终端读取图像:
const out = await exec(`fswebcam -r 640x480 -`);
const img = out[0];
console.log(img);
这在终端上给了我这个:
����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality
��
$.' ",#(7),01444'9=82<.342��C
还有更多。以前我对缓冲区做了类似的事情:
const file = fs.readFileSync(temp);
console.log(file.toString('base64'));
ctx.socket.emit('frame', { image: true, buffer: file.toString('base64') });
其中 file
是缓冲区,file.toString('base64')
是以下形式的字符串:
/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv ...
这成功了(但通过 SD 卡)。所以我的问题是,终端中第一个输出的格式是什么?以及如何将其转换为 Buffer 或类似于后者的 String。
我最后只是通过管道使用终端将其转换为 base64:
fswebcam -r 640x480 - | base64
所以现在我的整个片段是:
// Take a picture in an async way and return it as a base64 encoded string
// Props: https://scottlinux.com/2012/09/01/encode-or-decode-base64-from-the-command-line/
module.exports = async ({ resolution = '640x480', rotate = 0 } = {}) => {
const query = `fswebcam -r ${resolution} --rotate ${rotate} - | base64`;
const out = await exec(query, { maxBuffer: 1024 * 1024 });
return out[0];
};
在 Raspberry Pi 中显然没有简单的流式传输图像的方法。虽然有许多 hacks 可用,但在我的 Raspberry Pi 零中它在保持合适的帧率方面遇到了一些麻烦。
我怀疑主要问题之一是每个图像的 1st Google solution 和大多数 writes/reads 到 SD。到目前为止,我已经在不接触 SD 的情况下从终端读取图像:
const out = await exec(`fswebcam -r 640x480 -`);
const img = out[0];
console.log(img);
这在终端上给了我这个:
����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality
��
$.' ",#(7),01444'9=82<.342��C
还有更多。以前我对缓冲区做了类似的事情:
const file = fs.readFileSync(temp);
console.log(file.toString('base64'));
ctx.socket.emit('frame', { image: true, buffer: file.toString('base64') });
其中 file
是缓冲区,file.toString('base64')
是以下形式的字符串:
/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv ...
这成功了(但通过 SD 卡)。所以我的问题是,终端中第一个输出的格式是什么?以及如何将其转换为 Buffer 或类似于后者的 String。
我最后只是通过管道使用终端将其转换为 base64:
fswebcam -r 640x480 - | base64
所以现在我的整个片段是:
// Take a picture in an async way and return it as a base64 encoded string
// Props: https://scottlinux.com/2012/09/01/encode-or-decode-base64-from-the-command-line/
module.exports = async ({ resolution = '640x480', rotate = 0 } = {}) => {
const query = `fswebcam -r ${resolution} --rotate ${rotate} - | base64`;
const out = await exec(query, { maxBuffer: 1024 * 1024 });
return out[0];
};