### Generating 1D Barcode with Bitgener (JavaScript) Source: https://github.com/nemesiaai/bitgener/blob/master/README.md Demonstrates how to use the bitgener library to generate a 1D barcode (Code 93) with various configuration options, including padding, bar dimensions, colors, and Human Readable Interpretation (HRI). The example shows how to output the result as a buffer asynchronously and log it to the console. ```javascript const bitgener = require('bitgener'); (async () => { try { const ret = await bitgener({ data: '012345', type: 'code93', output: 'buffer', encoding: 'utf8', crc: false, padding: 25, barWidth: 5, barHeight: 150, original1DSize: true, addQuietZone: true, color: '#FFFFFF', opacity: 1, bgColor: '#F7931A', bgOpacity: 0.1, hri: { show: true, fontFamily: 'Futura', fontSize: 25, marginTop: 9, }, }); console.log(ret); } catch (e) { console.error(e.toString()); } })(); ``` -------------------------------- ### Generating 2D Barcode with Bitgener (JavaScript) Source: https://github.com/nemesiaai/bitgener/blob/master/README.md Illustrates the usage of the bitgener library to create a 2D barcode (Data Matrix) with specific dimensions, colors, and HRI settings. This example demonstrates outputting the generated barcode directly to an SVG file asynchronously and logging the result to the console. ```javascript const bitgener = require('bitgener'); (async () => { try { const ret = await bitgener({ data: 'Bitgener', type: 'datamatrix', output: 'bitgener.svg', encoding: 'utf8', rectangular: true, padding: 0, width: 250, height: 250, original2DSize: false, color: '#FFFFFF', opacity: 1, bgColor: '#F7931A', bgOpacity: 1, hri: { show: true, fontFamily: 'Courier New', fontSize: 15, marginTop: 0, }, }); console.log(ret); } catch (e) { console.error(e.toString()); } })(); ``` -------------------------------- ### Convert Bitgener SVG to PNG using Sharp (JavaScript) Source: https://github.com/nemesiaai/bitgener/blob/master/README.md This snippet demonstrates generating an SVG buffer using the `bitgener` library and then converting it to a PNG file using the `sharp` library. It includes a generic `convert` function for handling different output formats and methods (`toFile`, `toBuffer`, stream) and shows how to use `stream.pipeline` for safe stream handling. ```javascript const stream = require('stream'); const { createWriteStream } = require('fs'); const path = require('path'); const { promisify } = require('util'); const sharp = require('sharp'); const bitgener = require('bitgener'); const pipeline = promisify(stream.pipeline); /** * Generic function to convert the SVG generated from Bitgener * into the specified format and get the specified output. * * Please note that no type/value checks are made in this function. * * @param {Buffer} buffer The buffer generated by Bitgener. * @param {Number} density The density needed to resize the image with no * image quality loss. * @param {String} format Format could be one of png, jpeg, * webp, tiff, raw supported by Sharp. * @param {String} method Method could be one of toFile, toBuffer, * or a Readable Stream returned by default by Sharp. * @param {String} filePath Path to write the image data to. If set, method is not required. * @return {Promise} A Readable Stream by default, a Buffer, * a Sharp info object depending on the method. */ const convert = async function convert({ buffer, density, format, method, filePath, } = {}) { // sharp it! const sharped = sharp(buffer, { density }); let ret; if (method === 'toFile' || filePath !== undefined) { ret = await sharped.toFile(filePath); // object returned by sharp: https://sharp.pixelplumbing.com/en/stable/api-output/#tofile } else if (method === 'toBuffer') { ret = sharped.toFormat(format).toBuffer(); } else { // return a sharp/streamable object ret = sharped[format](); } return ret; }; // then use it in an async function (async () => { try { const wstream = createWriteStream(path.join(__dirname, 'sharped.png')); const { svg: buffer, density, } = await bitgener({ data: 'Bitgener', type: 'datamatrix', output: 'buffer', encoding: 'utf8', rectangular: true, padding: 0, width: 250, height: 250, original2DSize: false, color: '#FFFFFF', opacity: 1, bgColor: '#F7931A', bgOpacity: 1, hri: { show: true, fontFamily: 'Courier New', fontSize: 15, marginTop: 0, }, }); const rstream = await convert({ buffer, density, format: 'png', }); // listen to rstream and wstream error events ;) // use pipeline to automatically clean up streams or you're exposing your code to memory leaks await pipeline(rstream, wstream); // ... } catch (e) { console.error(e.toString()); } })(); ``` -------------------------------- ### Importing Bitgener Module in Node.js Source: https://github.com/nemesiaai/bitgener/blob/master/README.md This snippet demonstrates how to import the `bitgener` library using the `require` function in a Node.js environment. The imported module provides an async function for generating barcodes. ```JavaScript const bitgener = require('bitgener'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.