目录

WUP-MY-LABEL-PRINTER 旻佑热敏打印机标签打印uniapp插件使用说明

插件地址:WUP-MY-LABEL-PRINTER 旻佑热敏打印机标签打印安卓库

简介

  1. 本插件主要用于旻佑热敏打印机打印标签,不支持票据打印。
  2. 适用于旻佑的各型支持标签打印的热敏打印机。
  3. 本插件开发时使用的打印机型号为MY-805嵌入式面板打印机,其他型号请先试用测试。
  4. 使用本插件需要在Android 5.0以上设备使用。
  5. 插件支持uniapp的vue2 和 vue3 版本;uniapp x没做支持,可自行测试。
  6. 初始化流程:获取设备-打开设备-设置多字节模式及编码。
  7. 打印流程:标签页开始-绘制内容-打印,打印每一页标签都是这个流程。

方法

方法引入

js 复制代码
import { sayHi, 需引入的其他方法 } from "@/uni_modules/WUP-MY-LABEL-PRINTER";

插件测试方法

js 复制代码
sayHi("wup-my-label-printer", function (res) {
	console.log("sayHi", res)
})

枚举本地USB打印设备

js 复制代码
enumUsb((res) => {
	console.log("enumUsb", res)
})

打开USB设备

  • usbDeviceStr:枚举的USB列表的中USB设备名
  • autoReplyMode:自动回传模式 0,不开启;1,开启
js 复制代码
openUsb(usbDeviceStr, autoReplyMode, (res) => {
	console.log("openUsb", res)
})

启用标签模式

js 复制代码
labelEnableLabelMode((res) => {
	console.log("labelEnableLabelMode", res)
})

关闭标签模式

js 复制代码
labelDisableLabelMode((res) => {
	console.log("labelEnableLabelMode", res)
})

全切纸

打印时全切纸推荐使用labelPagePrintAndFullCutPaper,不推荐打印后再单独调用,可能出现切纸位置不准确问题。

js 复制代码
fullCutPaper((res) => {
	console.log("fullCutPaper", res)
})

半切纸

打印时半切纸推荐使用labelPagePrintAndHalfCutPaper,不推荐打印后再单独调用,可能出现切纸位置不准确问题。

js 复制代码
halfCutPaper((res) => {
	console.log("halfCutPaper", res)
})

设置打印机为多字节模式及编码

  • encoding:编码 0,GBK;1,UTF8;3,BIG5;4,ShiftJIS;5,EUCKR
js 复制代码
setMultiByteEncoding(encoding, (res) => {
	that.msg = JSON.stringify(res)
	console.log("setMultiByteEncoding", res)
})

校准标签纸(更换不同规格标签纸,需要校准)

js 复制代码
labelCalibrateLabel((res) => {
	console.log("labelCalibrateLabel", res)
})

走纸到标签缝隙处

js 复制代码
labelFeedLabel((res) => {
	console.log("labelFeedLabel", res)
})

打印机退纸到打印位置(适用于标签打印开头定位)

js 复制代码
labelBackPaperToPrintPosition((res) => {
	console.log("labelBackPaperToPrintPosition", res)
})

打印机进纸到撕纸位置(适用于标签打印结束定位)

js 复制代码
labelFeedPaperToTearPosition((res) => {
	console.log("labelFeedPaperToTearPosition", res)
})

指示一个标签页面的开始,并设置标签页的大小,参考点坐标和页面旋转角度

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • width:页面宽度
  • height:页面高度
  • rotation:页面旋转 0,页面不旋转;1,页面旋转90° 标签打印不支持旋转属性,默认0就好。
js 复制代码
labelPageBegin(x, y, width, height, rotation, (res) => {
	console.log("labelPageBegin", res)
})

将标签页上的内容打印到标签纸上

  • copies:打印分数[1 - 255]
js 复制代码
labelPagePrint(copies, (res) => {
	console.log("labelPagePrint", res)
})

将标签页上的内容打印到标签纸上,并半切纸

  • copies:打印分数[1 - 255]
js 复制代码
labelPagePrintAndHalfCutPaper(copies, (res) => {
	console.log("labelPagePrintAndHalfCutPaper", res)
})

将标签页上的内容打印到标签纸上,并全切纸

  • copies:打印分数[1 - 255]
js 复制代码
labelPagePrintAndFullCutPaper(copies, (res) => {
	console.log("labelPagePrintAndFullCutPaper", res)
})

获取文本字符风格值(异步)

  • bold:是否加粗 false,否;true,是
  • underline:是否下划线 false,否;true,是
  • highlight:是否反白(黑底白字) false,否;true,是
  • strikethrough:是否删除线 false,否;true,是(MY805测试无效)
  • rotation:旋转 0,不旋转;1,90°;2,180°;3,270°
  • widthscale:字体宽度放大倍数
  • heightscale:字体高度放大倍数
js 复制代码
getLabelTextStyle(bold, underline, highlight, strikethrough, rotation, widthscale, heightscale, (res) => {
	console.log("getLabelTextStyle", res)
})

获取文本字符风格值(同步)

  • bold:是否加粗 false,否;true,是
  • underline:是否下划线 false,否;true,是
  • highlight:是否反白(黑底白字) false,否;true,是
  • strikethrough:是否删除线 false,否;true,是(MY805测试无效)
  • rotation:旋转 0,不旋转;1,90°;2,180°;3,270°
  • widthscale:字体宽度放大倍数
  • heightscale:字体高度放大倍数
js 复制代码
const res = getLabelTextStyleSync(bold, underline, highlight, strikethrough, rotation, widthscale, heightscale)
console.log("getLabelTextStyleSync", res)

在标签页面上指定位置绘制文本(只能单行打印)。

js 复制代码
labelDrawText(x, y, font, style, str, (res) => {
	that.msg = JSON.stringify(res)
	console.log("labelDrawText", res)
})

在标签页指定位置绘制一维条码

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • nBarcodeType:标识条码类型 0,UPCA;1,UPCE;2,EAN13;3,EAN8;4,CODE39;5,ITF;6,CODEBAR;7,CODE93;8,CODE128;9,CODE11;10,MSI;11,128M;12,EAN128;13,25C;14,39C;15,39;16,EAN13PLUS2;17,EAN13PLUS5;18,EAN8+2;19,EAN8PLUS5;20,POST;21,UPCAPLUS2;22,UPCAPLUS5;23,UPCEPLUS2;24,UPCEPLUS5;25,CPOST;26,MSIC;27,PLESSEY;28,ITF14;29,EAN14;
  • nBarcodeTextPrintPosition:条码可读字符位置 0,不显示可读字符;1,下方显示;2,上方显示;3,上方和下方显示
  • height:条码高度
  • unitwidth:码块单元宽度
  • rotation:旋转 0,不旋转;1,90°;2,180°;3,270°
  • str:要打印的条码
js 复制代码
labelDrawBarcode(x, y, nBarcodeType, nBarcodeTextPrintPosition, height, unitwidth,  rotation, str, (res) => {
	console.log("labelDrawBarcode", res)
})

在标签页指定位置绘制二维码

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • nVersion:字符版本(不知道用处,默认0就好)
  • nECCLevel:ECC纠错等级 1,L:7%,低纠错,数据多;2,M:15%,中纠错;3,Q:优化纠错;4,H:30%,最高纠错,数据少
  • unitwidth:码块单元宽度(MY-805实测最大可为8,超过8会出现打印无反应,可以根据实际调整)
  • rotation:旋转 0,不旋转;1,90°;2,180°;3,270°
  • str:要打印的二维码
js 复制代码
labelDrawQRCode(x, y, nVersion, nECCLevel, unitwidth,  rotation, str, (res) => {
	console.log("labelDrawQRCode", res)
})

在标签页指定位置绘制 PDF417 条码

  • x:页面起始点X轴坐标

  • y:页面起始点Y轴坐标

  • column:列数,表述每行容纳多少码字。一个码字为 17*UnitWidth 个点。行数由打印机自动产生,行数范围限定为 3~90。ColNum 的取值范围:[1,30]。

  • nAspectRatio:条码模块的高宽比

  • nECCLevel:ECC纠错等级,取值范围[0, 8]

    纠错等级取值 纠错码数 可存资料量(字节)
    0 2 1108
    1 4 1106
    2 8 1101
    3 16 1092
    4 32 1072
    5 64 1024
    6 128 957
    7 256 804
    8 512 496
  • unitwidth:码块单元宽度,取值范围[1, 3]

  • rotation:旋转 0,不旋转;1,90°;2,180°;3,270°

  • str:要打印的PDF417条码

js 复制代码
labelDrawPDF417Code(x, y, column, nAspectRatio, nECCLevel, unitwidth,  rotation, str, (res) => {
	console.log("labelDrawPDF417Code", res)
})

在标签页指定位置绘制位图

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • dstw:要打印的宽度
  • dsth:要打印的高度
  • pszFile:图片路径,插件assets目录下路径,不需含assets
  • binaryzation_method:图片二值化算法 0,表示抖动算法;1,表示阀值算法;2,表示误差扩散法。具体效果请测试查看。
  • compression_method:最终打印数据的压缩方式 0,不压缩;1,一级压缩;2,二级压缩
js 复制代码
labelDrawImageFromFile(x, y, dstw, dsth, pszFile, binaryzation_method, compression_method, (res) => {
	console.log("labelDrawImageFromFile", res)
})

在标签页指定位置绘制线段

  • startx:直线段起始点 x 坐标值,取值范围:[0, Page_Width-1]。
  • starty:直线段起始点 y 坐标值,取值范围:[0,Page_Height-1]。
  • endx:直线段终止点 x 坐标值,取值范围:[0, Page_Width-1]。
  • endy:直线段终止点 y 坐标值,取值范围:[0,Page_Height-1]。
  • linewidth:直线段线宽,取值范围:[1,Page_Height-1]。
  • linecolor:直线段颜色线条颜色,0,白色;1,是黑色
js 复制代码
labelDrawLine(startx, starty, endx, endy, linewidth, linecolor, (res) => {
	console.log("labelDrawLine", res)
})

在标签页指定位置绘制矩形

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • width:矩形宽度
  • height:矩形高度
  • color:矩形颜色 0,白色;1,是黑色
js 复制代码
labelDrawRect(x, y, width, height, color, (res) => {
	console.log("labelDrawRect", res)
})

在标签页指定位置绘制矩形框

  • x:页面起始点X轴坐标
  • y:页面起始点Y轴坐标
  • width:矩形宽度
  • height:矩形高度
  • borderwidth:矩形框边框宽度
  • bordercolor:矩形框边框颜色 0,白色;1,是黑色
js 复制代码
labelDrawBox(x, y, width, height, borderwidth, bordercolor, (res) => {
	console.log("labelDrawBox", res)
})

获取开发包版本字符串

js 复制代码
getLibraryVersion((res) => {
	console.log("getLibraryVersion", res)
})

示例代码

js 复制代码
<template>
	<view>
		<view style="margin-top: 50px;padding: 0 15px;">
			<button @click="enumUsb" type="primary" style="margin-bottom: 15px;">usb设备列表</button>
			<button @click="openUsb" type="primary" style="margin-bottom: 15px;">打开usb设备</button>
			<button @click="halfCutPaper" type="primary" style="margin-bottom: 15px;">半切纸</button>
			<button @click="fullCutPaper" type="primary" style="margin-bottom: 15px;">全切纸</button>
			<button @click="getLibraryVersion" type="primary" style="margin-bottom: 15px;">获取开发包版本字符串</button>
			<button @click="labelEnableLabelMode" type="primary" style="margin-bottom: 15px;">开启标签打印</button>
			<button @click="labelDisableLabelMode" type="primary" style="margin-bottom: 15px;">关闭标签打印</button>
			<button @click="labelCalibrateLabel" type="primary" style="margin-bottom: 15px;">校准标签纸</button>
			<button @click="labelFeedLabel" type="primary" style="margin-bottom: 15px;">走纸到标签缝隙处</button>
			<button @click="labelBackPaperToPrintPosition" type="primary" style="margin-bottom: 15px;">退纸到打印位置</button>
			<button @click="labelFeedPaperToTearPosition" type="primary" style="margin-bottom: 15px;">进纸到撕纸位置</button>
			<view style="margin: 50px 0 15px 0;font-size: 2rem;font-weight: bold;">打印</view>
			<button @click="labelPageBegin" type="primary" style="margin-bottom: 15px;">标签页开始</button>
			<!-- <button @click="getLabelTextStyle" type="primary" style="margin-bottom: 15px;">获取文本样式</button> -->
			<button @click="labelDrawText" type="primary" style="margin-bottom: 15px;">绘制文本</button>
			<button @click="labelDrawBarcode" type="primary" style="margin-bottom: 15px;">绘制条形码</button>
			<button @click="labelDrawQRCode" type="primary" style="margin-bottom: 15px;">绘制二维码</button>
			<button @click="labelDrawPDF417Code" type="primary" style="margin-bottom: 15px;">绘制PDF417码</button>
			<button @click="labelDrawImageFromFile" type="primary" style="margin-bottom: 15px;">绘制本地图片</button>
			<button @click="labelDrawLine" type="primary" style="margin-bottom: 15px;">绘制线段</button>
			<button @click="labelDrawRect" type="primary" style="margin-bottom: 15px;">绘制矩形</button>
			<button @click="labelDrawBox" type="primary" style="margin-bottom: 15px;">绘制矩形框</button>
			<button @click="labelPagePrint" type="primary" style="margin-bottom: 15px;">打印</button>
			<button @click="labelPagePrintAndHalfCutPaper" type="primary" style="margin-bottom: 15px;">半切打印</button>
			<button @click="labelPagePrintAndFullCutPaper" type="primary" style="margin-bottom: 15px;">全切打印</button>
			<button @click="print" type="primary" style="margin-bottom: 15px;">完整打印示例</button>
		</view>
		<view style="padding: 15px 30px;">
			{{msg}}
		</view>
	</view>
</template>

<script>
	import { fullCutPaper, halfCutPaper, openUsb, sayHi, setMultiByteEncoding, enumUsb, getLibraryVersion, labelEnableLabelMode, labelDisableLabelMode, labelCalibrateLabel, labelFeedLabel, labelBackPaperToPrintPosition, labelFeedPaperToTearPosition, labelPageBegin, labelDrawText, labelPagePrint, getLabelTextStyle, getLabelTextStyleSync, labelDrawBarcode, labelDrawQRCode, labelDrawPDF417Code, labelDrawImageFromFile, labelDrawLine, labelDrawRect, labelDrawBox, labelPagePrintAndFullCutPaper, labelPagePrintAndHalfCutPaper } from "@/uni_modules/WUP-MY-LABEL-PRINTER";
	export default {
		data() {
			return {
				msg: "",
				deviceInx: 0,
			}
		},
		onLoad() {
			let that = this
			sayHi("wup-my-label-printer", function (res) {
				console.log("sayHi", res)
			})
		},
		methods: {
			print () {
				let that = this
				labelPageBegin(0, 0, 530, 390, 0, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPageBegin", res)
				})
				labelDrawText(10, 10, 24, getLabelTextStyleSync(true, false, false, true, 0, 1, 1), "Hello World!", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawText", res)
				})
				labelDrawQRCode(10, 40, 0, 1, 4, 0, "https://blog.csdn.net/Douz_lungfish", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawQRCode", res)
				})
				labelDrawImageFromFile(200, 30, 125, 125, "logo.png", 0, 0, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawImageFromFile", res)
				})
				labelDrawLine(10, 180, 300, 180, 5, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawLine", res)
				})
				labelDrawBox(0, 10, 530, 380, 2, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawBox", res)
				})
				labelPagePrintAndFullCutPaper(1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPagePrintAndFullCutPaper", res)
				})
			},
			getLabelTextStyle () {
				let that = this
				// bold, underline, highlight, strikethrough, rotation, widthscale, heightscale
				getLabelTextStyle(true, true, false, false, 1, 1, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("getLabelTextStyle", res)
				})
				const res1 = getLabelTextStyleSync(true, true, false, false, 1, 1, 1)
				console.log("getLabelTextStyleSync", res1)
			},
			labelPagePrint () {
				let that = this
				labelPagePrint(1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPagePrint", res)
				})
			},
			labelPagePrintAndHalfCutPaper () {
				let that = this
				labelPagePrintAndHalfCutPaper(1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPagePrintAndHalfCutPaper", res)
				})
			},
			labelPagePrintAndFullCutPaper () {
				let that = this
				labelPagePrintAndFullCutPaper(1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPagePrintAndFullCutPaper", res)
				})
			},
			labelDrawText () {
				let that = this
				labelDrawText(10, 10, 24, getLabelTextStyleSync(true, false, false, true, 0, 1, 1), "Hello World!", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawText", res)
				})
			},
			labelDrawBarcode () {
				let that = this
				labelDrawBarcode(10, 70, 0, 1, 60, 2, 0, "02031344565", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawBarcode", res)
				})
			},
			labelDrawQRCode () {
				let that = this
				// x, y, nVersion, nECCLevel, unitwidth,  rotation, str
				labelDrawQRCode(10, 10, 0, 1, 8, 0, "https://blog.csdn.net/Douz_lungfish", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawQRCode", res)
				})
			},
			labelDrawPDF417Code () {
				let that = this
				// x, y, column, nAspectRatio, nECCLevel, unitwidth,  rotation, str
				labelDrawPDF417Code(10, 10, 5, 5, 0, 3, 0, "Hello World!", (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawPDF417Code", res)
				})
			},
			labelDrawImageFromFile () {
				let that = this
				// x, y, dstw, dsth, pszFile,  binaryzation_method, compression_method
				labelDrawImageFromFile(10, 30, 200, 200, "la/logo.png", 0, 0, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawImageFromFile", res)
				})
			},
			labelDrawLine () {
				let that = this
				// startx, starty, endx, endy, linewidth, linecolor
				labelDrawLine(10, 10, 100, 20, 30, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawLine", res)
				})
			},
			labelDrawRect () {
				let that = this
				// x, y, width, height, color
				labelDrawRect(10, 10, 100, 80, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawRect", res)
				})
			},
			labelDrawBox () {
				let that = this
				// x, y, width, height, borderwidth, bordercolor
				labelDrawBox(10, 10, 100, 80, 2, 1, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelDrawBox", res)
				})
			},
			labelPageBegin () {
				let that = this
				labelPageBegin(0, 0, 576, 240, 0, (res) => {
					that.msg = JSON.stringify(res)
					console.log("labelPageBegin", res)
				})
			},
			labelFeedPaperToTearPosition () {
				let that = this
				labelFeedPaperToTearPosition((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelFeedPaperToTearPosition", res)
				})
			},
			labelBackPaperToPrintPosition () {
				let that = this
				labelBackPaperToPrintPosition((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelBackPaperToPrintPosition", res)
				})
			},
			labelFeedLabel () {
				let that = this
				labelFeedLabel((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelFeedLabel", res)
				})
			},
			labelCalibrateLabel () {
				let that = this
				labelCalibrateLabel((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelCalibrateLabel", res)
				})
			},
			labelDisableLabelMode () {
				let that = this
				labelDisableLabelMode((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelEnableLabelMode", res)
				})
			},
			labelEnableLabelMode () {
				let that = this
				labelEnableLabelMode((res) => {
					that.msg = JSON.stringify(res)
					console.log("labelEnableLabelMode", res)
				})
			},
			getLibraryVersion () {
				let that = this
				getLibraryVersion((res) => {
					that.msg = JSON.stringify(res)
					console.log("getLibraryVersion", res)
				})
			},
			enumUsb () {
				let that = this
				enumUsb((res) => {
					that.msg = JSON.stringify(res)
					console.log("enumUsb", res)
				})
			},
			openUsb () {
				let that = this
				openUsb('KCEC_USB in FS Mode/0E08F5C2050601006415000055080172', 1, (res) => {
					that.msg = JSON.stringify(res)
					if (res.code === 0) {
						setMultiByteEncoding(1, (res) => {
							that.msg = JSON.stringify(res)
							console.log("setMultiByteEncoding", res)
						})
					}
					console.log("openUsb", res)
				})
			},
			halfCutPaper () {
				let that = this
				halfCutPaper((res) => {
					that.msg = JSON.stringify(res)
					console.log("halfCutPaper", res)
				})
			},
			fullCutPaper () {
				let that = this
				fullCutPaper((res) => {
					that.msg = JSON.stringify(res)
					console.log("fullCutPaper", res)
				})
			},
		}
	}
</script>
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
每次的天空11 分钟前
Android学习总结之算法篇五(字符串)
android·学习·算法
Gracker1 小时前
Android Weekly #202513
android
张拭心3 小时前
工作九年程序员的三月小结
android·前端
每次的天空3 小时前
Flutter学习总结之Android渲染对比
android·学习·flutter
鸿蒙布道师5 小时前
鸿蒙NEXT开发土司工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
智想天开6 小时前
11.使用依赖注入容器实现松耦合
android
yunteng5217 小时前
音视频(四)android编译
android·ffmpeg·音视频·x264·x265
tangweiguo030519877 小时前
(kotlin) Android 13 高版本 图片选择、显示与裁剪功能实现
android·开发语言·kotlin
匹马夕阳7 小时前
(一)前端程序员转安卓开发分析和规划建议
android·前端
Kika写代码7 小时前
【Android】UI开发:XML布局与Jetpack Compose的全面对比指南
android·xml·ui