【问题标题】:Piping stdout from one command into stdin for another, and then to a writable stream将标准输出从一个命令传输到另一个命令的标准输入,然后传输到可写流
【发布时间】:2015-06-01 01:21:33
【问题描述】:

使用app.js 中的代码,我可以将camerautil.js 中camera() 中生成的raspistill 命令的输出通过管道传输到可写流。但不是可写流,我想将输出通过管道传输到在resize() 中生成的 convert 命令,然后从 that 管道输出标准输出> 命令到可写流。

我尝试过的:

var streamOut = fs.createWriteStream('./image.jpg');
camerautil.camera().pipe(camerautil.resize(streamOut, 1, 1));

它会抛出以下错误:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: Cannot pipe. Not readable.
    at WriteStream.Writable.pipe (_stream_writable.js:162:22)
    at Object.exports.resize (/home/pi/dev/app/camerautil.js:27:14)
    at Object.<anonymous> (/home/pi/dev/app/index3.js:5:37)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

这是我的(工作)代码:

app.js

var camerautil = require('./camerautil'),
    fs = require('fs');

var streamOut = fs.createWriteStream('./image.jpg');
camerautil.camera().pipe(streamOut);

camerautil.js

var spawn = require('child_process').spawn,
    Stream = require('stream');

exports.camera = function() {
    var child = spawn('raspistill', ['-w', 320, '-h', 240, '-n', '-t', 1, '-o', '-']);

    var stream = new Stream();

    child.stderr.on('data', stream.emit.bind(stream, 'error'));
    child.stdout.on('data', stream.emit.bind(stream, 'data'));
    child.stdout.on('end', stream.emit.bind(stream, 'end'));
    child.on('error', stream.emit.bind(stream, 'error'));

    return stream;
}

exports.resize = function(streamIn, width, height) {
    var child = spawn('convert', ['-', '-resize', width + 'x' + height, '-']);

    var stream = new Stream();

    child.stderr.on('data', stream.emit.bind(stream, 'error'));
    child.stdout.on('data', stream.emit.bind(stream, 'data'));
    child.stdout.on('end', stream.emit.bind(stream, 'end'));
    child.on('error', stream.emit.bind(stream, 'error'));

    streamIn.pipe(child.stdin);

    return stream;
}

我应该提到我在 Raspberry Pi 上运行 node v0.12.0。 raspistill 命令用于使用相机模块拍照。 convert 命令是 ImageMagick 的一部分。

【问题讨论】:

    标签: node.js stream imagemagick raspberry-pi child-process


    【解决方案1】:

    经过一番折腾,我找到了解决办法。

    var camerautil = require('./camerautil'),
        fs = require('fs');
    
    var streamOut = fs.createWriteStream('./image.jpg');
    camerautil.resize(camerautil.camera(), 1, 1).pipe(streamOut);
    

    【讨论】:

      【解决方案2】:

      流入gm 模块(Imagemagick 的包装器)可能更简单。如果那将安装在 Raspberry PI 上。

      var gm = require('gm')
      gm(camerautil.camera())
        .resize('100', '100')
        .stream('jpg')
        .pipe(streamOut)
      

      https://github.com/aheckmann/gm#streams

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-02
        • 2020-11-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多