JavaScript实现的JPEG流编码器和解码器:jpg-stream
jpg-stream是JavaScript实现的JPEG流编码器和解码器。它是libjpeg的一个JavaScript直接编译,使用 Emscripten。支持Node和浏览器。
Decoding
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. }));
Encoding
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'));
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!