1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const usb = require('usb'); const Jimp = require('jimp');
function getImageData(path, cb) { Jimp.read(path, (err, img) => { const widthInBytes = Math.ceil(img.getWidth() / 8); const data = new Array(img.getHeight()); for (let y = 0; y < img.getHeight(); y++) { const row = new Array(widthInBytes); for (let b = 0; b < widthInBytes; b++) { let byte = 0; let mask = 128; for (let x = b*8; x < (b+1)*8; x++) { const color = Jimp.intToRGBA(img.getPixelColor(x, y)); if (color.a < 65) byte = byte ^ mask; mask = mask >> 1; } row[b] = byte; } data[y] = row; } cb(data); }); }
function print(buffer) { let device = usb.findByIds(8137, 8214); device.open(); device.interfaces[0].claim(); const outEndpoint = device.interfaces[0].endpoints.find(e => e.direction === 'out'); outEndpoint.transferType = 2; outEndpoint.transfer(buffer, (err) => { device.close(); }); }
getImageData('hn-logo.png', (data) => { const widthInBytes = data[0].length; const heightInDots = data.length;
const buffer = Buffer.concat([ Buffer.from('SIZE 48 mm,25 mm\r\n'), Buffer.from('CLS\r\n'), Buffer.from(`BITMAP 10,20,${widthInBytes},${heightInDots},0,`), Buffer.from(data.flat()), Buffer.from('BARCODE 10,100,"128",50,1,0,2,2,"altospos.com"\r\n'), Buffer.from('PRINT 1\r\n'), Buffer.from('END\r\n'), ]); print(buffer); });
|