Inkjet
JPEG-image decoding, encoding & EXIF reading library for a browser and node.js
Install / Use
/learn @gchudnov/InkjetREADME

JPEG-image decoding, encoding & EXIF reading library for a browser and node.js

Installation
installing with npm:
npm install inkjet --save
In browser
To use inkjet in a browser, use inkjet.js or inkjet.min.js in /dist directory, or build it manually:
npm install
npm run browser
Usage
Decoding, encoding and EXIF extraction operations are offloaded to WebWorkers if the environment supports them.
Decode JPEG
const inkjet = require('inkjet');
const filepath = './images/js_logo-4-2-0.jpg';
const buf = fs.readFileSync(filepath);
inkjet.decode(buf, (err, decoded) => {
// decoded: { width: number, height: number, data: Uint8Array }
});
Encode JPEG
const inkjet = require('inkjet');
const width = 320;
const height = 180;
const frameData = new Buffer(width * height * 4);
const i = 0;
while (i < frameData.length) {
frameData[i++] = 0xFF; // R, red
frameData[i++] = 0x00; // G, green
frameData[i++] = 0x00; // B, blue
frameData[i++] = 0xFF; // A, alpha - ignored in JPEGs
}
const buf = frameData;
const options = {
width: width,
height: height,
quality: 80
};
inkjet.encode(buf, options, (err, encoded) => {
// encoded: { width: number, height: number, data: Uint8Array }
});
Read EXIF
const inkjet = require('inkjet');
const filepath = './images/js_logo-exif.jpg';
const buf = fs.readFileSync(filepath);
inkjet.exif(buf, (err, metadata) => {
// metadata -- an object that maps EXIF tags to string values
});
Deduce image type
const inkjet = require('inkjet');
const filepath = './images/js_logo-4-2-0.jpg';
const buf = fs.readFileSync(filepath);
inkjet.magic(buf, (err, data) => {
// data -- an object that contains mime-type and extension
});
Image information
const inkjet = require('inkjet');
const filepath = './images/js_logo-4-2-0.jpg';
const buf = fs.readFileSync(filepath);
inkjet.info(buf, (err, data) => {
// data -- an object that contains width, height, mime type and extension data
});
API
.decode(buf, [options], cb);
Decodes a JPEG image.
Arguments:
buf- source buffer, one of the following types:Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray[options]- an optional object with settings to decode an image. Supported options:width- override image widthheight- override image height
cb- a callback that gets 2 arguments:err- decodingErrordecoded- an object that describes the decoded image:{ width: number, height: number, data: Uint8Array }where data represents colors in RGBA format.
inkjet.decode(buf, (err, decoded) => {
// ...
});
.encode(buf, [options], cb);
Encodes the provided buffer to a JPEG format.
Arguments:
buf- source buffer, one of the following types:Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray[options]- an optional object with settings to encode an image. Supported options:width- width of the image inbufheight- height of the image inbufquality- a numberic value [0-100], describes quality of encoding. 0 - low quality, 100 - high quality.
cb- a callback that gets 2 arguments:err- encodingErrorencoded- an object that describes the encoded image:{ width: number, height: number, data: Uint8Array }
inkjet.encode(buf, (err, encoded) => {
// ...
});
.exif(buf, [options], cb);
Get EXIF metadata for the image. The metadata tags defined in the Exif standard cover date and time information, camera settings, descriptions, resolution and location information.
Arguments:
buf- source buffer, one of the following types:Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray[options]- an optional object with settings to encode an image. Supported options:hasMakerNote- exclude MakerNote tag from metadata. Default value:true, MakerNote tag is excluded.
cb- a callback that gets 2 arguments:err- exif extractionErrormetadata- metadata object, a set of tags and their values.
inkjet.exif(buf, (err, metadata) => {
// ...
});
.magic(buf, cb);
Deduce image type (mime type and extension) for the provided buffer.
Arguments:
buf- source buffer, one of the following types:Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArraycb- a callback that gets 2 arguments:err-Errorobjectdata- data object { "mimeType": string, "extension": string }
inkjet.magic(buf, (err, data) => {
// ...
});
.info(buf, cb);
Get image information without reading and decoding an image.
Arguments:
buf- source buffer, one of the following types:Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArraycb- a callback that gets 2 arguments:err-Errorobjectdata- data object { "type": string, "mimeType": string, "extension": string, "width": number, "height: number" }
inkjet.info(buf, (err, data) => {
// data: {
// type: "image"
// mimeType: ...
});
Tests
To run the tests for inkjet in Node.js:
npm test
To run tests in a browser:
npm run bundle:test
a bundle file inkjet-test-bundle.js with all tests will be generated in inkjet/test/browser directory.
Open inkjet/test/browser/index.html in the target browser. Tests will run automatically.
Contact
[Grigorii Chudnov] (mailto:g.chudnov@gmail.com)
License
Distributed under the The MIT License (MIT).
Related Skills
node-connect
349.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.7kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
