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 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!