uni-app meter to achieve business printing hardware

This document uses the device models: Suppliers meters (sunmi) V2,2G + 16G.

Suppliers need to reference print plug-meters (APP each project should take 1 yuan to buy the plug-in), plug-in address: ext.dcloud.net.cn/plugin?id=8... .

 

 

The basic reference document print instructions on the case, for printing a picture of the document does not provide a direct method to use, to be performed by the ESC print instruction content, reference method sunmi_print.printRawData (data). Wherein, data is converted into image ESC instruction, here to convert the server (client conversion efficiency is too low).

ESCPOS instruction document official document reference providers meters: docs.sunmi.com/general-fun...

 

 

 

/*
* @param $file_path 本地图片路径,图片进行大小整合thumb(400,210)长为400宽为210以符合打印要求
* @return $data 打印插件直接使用的数据
*/
public function imageFileToEsc($file_path) { $image = Image::open($file_path); $image->thumb(400, 210)->save($file_path); //规范化图片 $i = imagecreatefrompng($file_path); $width = ceil(imagesx($i) / 8) * 8 - 8; $height = imagesy($i); $data = []; $data[0] = 0x1D; $data[1] = 0x76; $data[2] = 0x30; $data[3] = 0; $data[4] = $width / 8 % 256; $data[5] = (int)($width / 8 / 256); $data[6] = $height % 256; $data[7] = (int)($height / 256); $index = 8; for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x += 8) { $part = []; for ($j = 0; $j < 8; $j++) { $read_width = $x + $j >= $width ? $width : $x + $j; $rgb = imagecolorat($i, $read_width, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $gray = (int)($r * 0.3 + $g * 0.59 + $b * 0.11); if ($gray > 127) { $part[] = 0; } else { $part[] = 1; } } $temp = $part[0] * 128 + $part[1] * 64 + $part[2] * 32 + $part[3] * 16 + $part[4] * 8 + $part[5] * 4 + $part[6] * 2 + $part[7] * 1; $data[$index++] = $temp; } } return $data; }复制代码

 

 

 

 

//引用插件
const sunmi_print= uni.requireNativePlugin('Sunmi-Print'); 
//连接打印机
//首次启动app时调用此方法,使app可以连上打印机,从而可以操作打印
sunmi_print.init();
//打印图片
sunmi_print.printRawData(data);
//断开打印机
sunmi_print.destroy();

Guess you like

Origin www.cnblogs.com/jucheng/p/12342957.html
Recommended