From eb19265495c72435ec90ce0fc87e49cfaf72d432 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 28 Aug 2022 08:47:22 -0600 Subject: [PATCH] add various print size scripts --- .gitignore | 3 ++- blog.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ blog2.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ med_print.js | 54 +++++++++++++++++++++++++++++++++++++++++++++ print.js | 2 +- print_dhl_bottom.js | 54 +++++++++++++++++++++++++++++++++++++++++++++ print_dhl_top.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 blog.js create mode 100644 blog2.js create mode 100644 med_print.js create mode 100644 print_dhl_bottom.js create mode 100644 print_dhl_top.js diff --git a/.gitignore b/.gitignore index e900cf6..23da717 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -out.tspl \ No newline at end of file +out.tspl +*.png diff --git a/blog.js b/blog.js new file mode 100644 index 0000000..0699914 --- /dev/null +++ b/blog.js @@ -0,0 +1,53 @@ +// simple code for +// MIT license + +const jimp = require("jimp"); +const fs = require("fs"); +const path = require("path"); + +const THRESHOLD = 2500000000; // RGBA value in int form + +// 255 min; (r0 g0 b0 a255) +// 4294967295; max (r255 g255 b255 a255) + +const COLS = 16; +const ROWS = 16; +// const COLS = 16; +// const ROWS = 2; + +const labelPath = path.resolve(process.cwd(), process.argv[2]); + +jimp.read(labelPath, async (err, image) => { + //image.resize(COLS, ROWS).grayscale(); + const img = new Buffer.alloc((COLS / 8) * ROWS); + for (let row = 0; row < ROWS; row++) { + for (let byte = 0; byte < COLS / 8; byte++) { + let byteData = 0; + for (let bit = 0; bit < 8; bit++) { + const pixelX = byte * 8 + bit; + const pixelY = row; + const val = image.getPixelColor(pixelX, pixelY); + if (val > THRESHOLD) { + byteData = byteData | (1 << (7 - bit)); + } + } + img[row * (COLS / 8) + byte] = byteData; + // console.log({ byteData, img, row, byte }); + } + } + + const label = Buffer.concat([ + //Buffer.alloc(512), // 512 NUL bytes to start a new label + Buffer.from("\r\nSIZE 99.8 mm, 149.9 mm"), + Buffer.from("\r\nSET TEAR ON"), + Buffer.from("\r\nSET CUTTER OFF"), + Buffer.from("\r\nSET PEEL OFF"), + Buffer.from("\r\nCLS"), + Buffer.from("\r\nBITMAP 0,0,2,16,1,"), + img, + Buffer.from("\r\nPRINT 1,1"), + Buffer.from("\r\n"), + ]); + fs.writeFileSync("out.tspl", label); //for debugging + //fs.writeFileSync("/dev/usb/lp0", label); +}); diff --git a/blog2.js b/blog2.js new file mode 100644 index 0000000..29cbf66 --- /dev/null +++ b/blog2.js @@ -0,0 +1,53 @@ +// simple code for +// MIT license + +const jimp = require("jimp"); +const fs = require("fs"); +const path = require("path"); + +const THRESHOLD = 2500000000; // RGBA value in int form + +// 255 min; (r0 g0 b0 a255) +// 4294967295; max (r255 g255 b255 a255) + +const COLS = 800; +const ROWS = 1198; +// const COLS = 16; +// const ROWS = 2; + +const labelPath = path.resolve(process.cwd(), process.argv[2]); + +jimp.read(labelPath, async (err, image) => { + //image.resize(COLS, ROWS).grayscale(); + const img = new Buffer.alloc((COLS / 8) * ROWS); + for (let row = 0; row < ROWS; row++) { + for (let byte = 0; byte < COLS / 8; byte++) { + let byteData = 0; + for (let bit = 0; bit < 8; bit++) { + const pixelX = byte * 8 + bit; + const pixelY = row; + const val = image.getPixelColor(pixelX, pixelY); + if (val > THRESHOLD) { + byteData = byteData | (1 << (7 - bit)); + } + } + img[row * (COLS / 8) + byte] = byteData; + // console.log({ byteData, img, row, byte }); + } + } + + const label = Buffer.concat([ + Buffer.alloc(512), // 512 NUL bytes to start a new label + Buffer.from("\r\nSIZE 99.8 mm, 149.9 mm"), + Buffer.from("\r\nSET TEAR ON"), + Buffer.from("\r\nSET CUTTER OFF"), + Buffer.from("\r\nSET PEEL OFF"), + Buffer.from("\r\nCLS"), + Buffer.from("\r\nBITMAP 0,0,100,1198,1,"), + img, + Buffer.from("\r\nPRINT 1,1"), + Buffer.from("\r\n"), + ]); + fs.writeFileSync("out.tspl", label); //for debugging + //fs.writeFileSync("/dev/usb/lp0", label); +}); diff --git a/med_print.js b/med_print.js new file mode 100644 index 0000000..abf325e --- /dev/null +++ b/med_print.js @@ -0,0 +1,54 @@ +// simple code for +// MIT license + +const jimp = require("jimp"); +const fs = require("fs"); +const path = require("path"); + +const THRESHOLD = 2500000000; // RGBA value in int form + +// 255 min; (r0 g0 b0 a255) +// 4294967295; max (r255 g255 b255 a255) + +const COLS = 456; +const ROWS = 254; +// const COLS = 16; +// const ROWS = 2; + +const labelPath = path.resolve(process.cwd(), process.argv[2]); + +jimp.read(labelPath, async (err, image) => { + //image.resize(COLS, ROWS).grayscale(); + const img = new Buffer.alloc((COLS / 8) * ROWS); + for (let row = 0; row < ROWS; row++) { + for (let byte = 0; byte < COLS / 8; byte++) { + let byteData = 0; + for (let bit = 0; bit < 8; bit++) { + const pixelX = byte * 8 + bit; + const pixelY = row; + const val = image.getPixelColor(pixelX, pixelY); + if (val > THRESHOLD) { + byteData = byteData | (1 << (7 - bit)); + } + } + img[row * (COLS / 8) + byte] = byteData; + // console.log({ byteData, img, row, byte }); + } + } + + const label = Buffer.concat([ + Buffer.alloc(512), // 512 NUL bytes to start a new label + Buffer.from("\r\nSIZE 57.2 mm, 31.8 mm"), + Buffer.from("\r\nSPEED 1.5"), + Buffer.from("\r\nSET TEAR ON"), + Buffer.from("\r\nSET CUTTER OFF"), + Buffer.from("\r\nSET PEEL OFF"), + Buffer.from("\r\nCLS"), + Buffer.from("\r\nBITMAP 0,0,57,254,1,"), + img, + Buffer.from("\r\nPRINT 1,1"), + Buffer.from("\r\n"), + ]); + // fs.writeFileSync("out.tspl", label); //for debugging + fs.writeFileSync("/dev/usb/lp0", label); +}); diff --git a/print.js b/print.js index c4564a3..69f9cfb 100644 --- a/print.js +++ b/print.js @@ -48,6 +48,6 @@ jimp.read(labelPath, async (err, image) => { Buffer.from("\r\nPRINT 1,1"), Buffer.from("\r\n"), ]); - // fs.writeFileSync("out.tspl", label); //for debugging + //fs.writeFileSync("out.tspl", label); //for debugging fs.writeFileSync("/dev/usb/lp0", label); }); diff --git a/print_dhl_bottom.js b/print_dhl_bottom.js new file mode 100644 index 0000000..db47fed --- /dev/null +++ b/print_dhl_bottom.js @@ -0,0 +1,54 @@ +// simple code for +// MIT license + +const jimp = require("jimp"); +const fs = require("fs"); +const path = require("path"); + +const THRESHOLD = 2500000000; // RGBA value in int form + +// 255 min; (r0 g0 b0 a255) +// 4294967295; max (r255 g255 b255 a255) + +const COLS = 800; +const ROWS = 1198; +// const COLS = 16; +// const ROWS = 2; + +const labelPath = path.resolve(process.cwd(), process.argv[2]); + +jimp.read(labelPath, async (err, image) => { + //image.resize(COLS, ROWS).grayscale(); + await image.crop(0,1198,800,1198); + const img = new Buffer.alloc((COLS / 8) * ROWS); + for (let row = 0; row < ROWS; row++) { + for (let byte = 0; byte < COLS / 8; byte++) { + let byteData = 0; + for (let bit = 0; bit < 8; bit++) { + const pixelX = byte * 8 + bit; + const pixelY = row; + const val = image.getPixelColor(pixelX, pixelY); + if (val > THRESHOLD) { + byteData = byteData | (1 << (7 - bit)); + } + } + img[row * (COLS / 8) + byte] = byteData; + // console.log({ byteData, img, row, byte }); + } + } + + const label = Buffer.concat([ + Buffer.alloc(512), // 512 NUL bytes to start a new label + Buffer.from("\r\nSIZE 99.8 mm, 149.9 mm"), + Buffer.from("\r\nSET TEAR ON"), + Buffer.from("\r\nSET CUTTER OFF"), + Buffer.from("\r\nSET PEEL OFF"), + Buffer.from("\r\nCLS"), + Buffer.from("\r\nBITMAP 0,0,100,1198,1,"), + img, + Buffer.from("\r\nPRINT 1,1"), + Buffer.from("\r\n"), + ]); + //fs.writeFileSync("out.tspl", label); //for debugging + fs.writeFileSync("/dev/usb/lp0", label); +}); diff --git a/print_dhl_top.js b/print_dhl_top.js new file mode 100644 index 0000000..9c4a0f9 --- /dev/null +++ b/print_dhl_top.js @@ -0,0 +1,53 @@ +// simple code for +// MIT license + +const jimp = require("jimp"); +const fs = require("fs"); +const path = require("path"); + +const THRESHOLD = 2500000000; // RGBA value in int form + +// 255 min; (r0 g0 b0 a255) +// 4294967295; max (r255 g255 b255 a255) + +const COLS = 800; +const ROWS = 1198; +// const COLS = 16; +// const ROWS = 2; + +const labelPath = path.resolve(process.cwd(), process.argv[2]); + +jimp.read(labelPath, async (err, image) => { + //image.resize(COLS, ROWS).grayscale(); + const img = new Buffer.alloc((COLS / 8) * ROWS); + for (let row = 0; row < ROWS; row++) { + for (let byte = 0; byte < COLS / 8; byte++) { + let byteData = 0; + for (let bit = 0; bit < 8; bit++) { + const pixelX = byte * 8 + bit; + const pixelY = row; + const val = image.getPixelColor(pixelX, pixelY); + if (val > THRESHOLD) { + byteData = byteData | (1 << (7 - bit)); + } + } + img[row * (COLS / 8) + byte] = byteData; + // console.log({ byteData, img, row, byte }); + } + } + + const label = Buffer.concat([ + Buffer.alloc(512), // 512 NUL bytes to start a new label + Buffer.from("\r\nSIZE 99.8 mm, 149.9 mm"), + Buffer.from("\r\nSET TEAR ON"), + Buffer.from("\r\nSET CUTTER OFF"), + Buffer.from("\r\nSET PEEL OFF"), + Buffer.from("\r\nCLS"), + Buffer.from("\r\nBITMAP 0,0,100,1198,1,"), + img, + Buffer.from("\r\nPRINT 1,1"), + Buffer.from("\r\n"), + ]); + //fs.writeFileSync("out.tspl", label); //for debugging + fs.writeFileSync("/dev/usb/lp0", label); +});