node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org »

memobird

memobird

咕咕机 Node.js SDK

安装

npm install memobird

使用

初始化

const Memobird = require('Memobird');
 
const memobird = new Memobird({
  ak: 'xxxxxxxxxxxxxxxx',
  memobirdID: 'xxxxxxxx',
  useridentifying: 'xxx',
});

打印

打印文字

printText(text) 方法可以打印文字。

memobird.init()
  .then(() => memobird.printText('你好咕咕机'))

打印图片

由于咕咕机要求接收的图片必须是单色点位图base64编码值,printImage使用了graphicsmagick来实现这一图片格式转换的功能,所以使用printImage方法必须先安装graphicsmagick。

printImage(image, width) 方法可以打印任何格式的图片。参数中的image可以是图片的本地路径图片的网络地址图片的base64编码;参数中的width指打印宽度,默认为384是咕咕机打印区总宽度。

memobird.init()
  .then(() => memobird.printImage('http://7xrs2s.com1.z0.glb.clouddn.com/5388545BF2D3F99643AFE22BE8C87B8A.jpg'))
  .then(() => memobird.printImage('http://7xrs2s.com1.z0.glb.clouddn.com/5388545BF2D3F99643AFE22BE8C87B8A.jpg', 100))
  .then(() => memobird.printImage('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAYKADAAQAAAABAAAAGAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAGABgAwERAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk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db8VfGTRtFu/g58T9VkUOli3hjwhpS+KvhPp6TDy7HU7HWvidcWFmWi1DSfEN2yX0QB/Xx+xD4J+K/wy/Y4/Za+GPx1ttOtvjL8MvgD8J/hv8Tjo+rw69pF9428A+CNF8I+INZ0rV4VjW+03XtQ0efW7GWSGC5W1v4o7y2trtJoEAP/Z', 96))

打印Canvas

很多场景下,我们需要打印更丰富的内容,本模块可以直接打印H5的canvas。如果你感兴趣,canvas转单色点位图的功能是通过我编写的模块binary-bmp实现的。

encodeCanvas(canvas)静态方法可以将canvas转为单色点位图base64编码值。

浏览器中:

const Memobird = require('memobird');
const canvas = document.getElementById('canvas-id'); // 获取canvas对象 
const bmp = Memobird.encodeCanvas(canvas); // 将canvas转为单色点位图base64编码值 
// ... 
// 省略代码:将bmp的值传给服务器 
// ... 

服务器中:

// ... 
// 省略代码:接收客户端发来的bmp的值 
// ... 
memobird.init()
  .then(() => memobird.print(bmp));

一次打印多个

咕咕机每次打印一份内容都会留出上下间距,如果你不想看到这些上下间距可以选择一次性打印多个内容。

print(item1, item2, ...)方法可以一次性打印多个内容。
encodeText(text)静态方法可以将文本转为print方法可以处理的内容。
encodeImage(image, width)静态方法可以将图片转为print方法可以处理的内容。
encodeCanvas(canvas)静态方法可以将canvas对象转为print方法可以处理的内容。

memobird.init()
  .then(() => memobird.print(
    Memobird.encodeText('你好咕咕机,能不能一次性打印所有东西?'),
    Memobird.encodeImage('http://7xrs2s.com1.z0.glb.clouddn.com/5388545BF2D3F99643AFE22BE8C87B8A.jpg', 100)
  ))

获取打印状态

glance(printcontentid, delay)方法可以延时一定时间后获取咕咕机打印状态。printcontentid参数指打印内容的唯一ID,由所有名字以print开头的方法返回;delay参数指延时多少毫秒后才获取打印状态,默认1000。

memobird.init()
  .then(() => memobird.printText('你好咕咕机'))
  .then(printcontentid => memobird.glance(printcontentid, 1000))
  .then(printflag => console.log('打印状态:', printflag));

watch(printcontentid, interval, max) 方法可以监听打印状态。当打印时间难以预测时,watch方法可以隔一段时间获取一次打印状态,只有当打印状态为已完成或监听时间超时时才终止。printcontentid参数指打印内容的唯一ID;interval参数指监听频率,即多少毫秒获取一次打印状态,默认3000;max参数指监听超时,即超出多长时间立即终止监听,默认15000。

memobird.init()
  .then(() => memobird.printImage('https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png'))
  .then(printcontentid => memobird.watch(printcontentid, 3000, 15000))
  .then(printflag => console.log('打印状态:', printflag));

示例

参考 examples/index.js

const Memobird = require('../');
 
const memobird = new Memobird({
  ak: 'xxxxxxxxxxxxxxxx',
  memobirdID: 'xxxxxxxx',
  useridentifying: 'xxx',
});
 
memobird.init()
  // 打印文本 
  .then(() => memobird.printText('你好咕咕机!'))
  // 3000毫秒后获取打印状态 
  .then(printcontentid => memobird.glance(printcontentid, 3000))
  .then(printflag => console.log('打印状态:', printflag === 1 ? '已打印' : '未打印'))
 
  // 打印图片 
  .then(() => memobird.printImage('https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png'), 100)
  // 每3000毫秒获取一次打印状态,如果显示未打印则继续获取,当获取到结果为已打印的状态或总用时超出15000毫秒,则终止并返回结果 
  .then(printcontentid => memobird.watch(printcontentid, 3000, 15000))
  .then(printflag => console.log('打印状态:', printflag === 1 ? '已打印' : '未打印'))
 
  // 一次性打印多个 
  .then(() => memobird.print(
    memobird.encodeText('你好咕咕机,能不能一次性打印所有东西?'),
    memobird.encodeText('第一张图片:'),
    memobird.encodeImage('https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png', 50),
    memobird.encodeText('第二张图片:'),
    memobird.encodeImage('https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png', 100),
    memobird.encodeText('第三张图片:'),
    memobird.encodeImage('https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png')
  ))
  .catch(error => console.log('打印出错了:', error));