jopen 7年前

jpg-stream是JavaScript实现的JPEG流编码器和解码器。它是libjpeg的一个JavaScript直接编译,使用 Emscripten。支持Node和浏览器。


This example uses the concat-frames module to collect the output of the JPEG decoder into a single buffer.

var JPEGDecoder = require('jpg-stream/decoder');  var concat = require('concat-frames');    // decode a JPEG file to RGB pixels  fs.createReadStream('in.jpg')    .pipe(new JPEGDecoder)    .pipe(concat(function(frames) {      // frames is an array of frame objects (one for JPEGs)      // each element has a `pixels` property containing      // the raw RGB pixel data for that frame, as      // well as the width, height, etc.    }));


You can encode a JPEG by writing or piping pixel data to a JPEGEncoder stream. You can set the quality option to a number between 1 and 100 to control the size vs quality tradeoff made by the encoder.

The JPEG encoder supports writing data in the RGB, grayscale, or CMYK color spaces. If you need to convert from another unsupported color space, first pipe your data through the color-transform module.

var JPEGEncoder = require('jpg-stream/encoder');  var ColorTransform = require('color-transform');    // convert a PNG to a JPEG  fs.createReadStream('in.png')    .pipe(new PNGDecoder)    .pipe(new JPEGEncoder({ quality: 80 }))    .pipe(fs.createWriteStream('out.jpg'));    // colorspace conversion to convert from RGBA to RGB  fs.createReadStream('rgba.png')    .pipe(new PNGDecoder)    .pipe(new ColorTransform('rgb'))    .pipe(new JPEGEncoder)    .pipe(fs.createWriteStream('rgb.jpg'));