UniAPP Android 蓝牙 ESCPOS打印机 打印图片和二维码

移动端打印的实现方式参考

上一篇:DCloud UniAPP Android 蓝牙连接ESCPOS打印机

本片来实现 上一篇中 打印图片中 的 img (图片点阵灰度数据)  

var img=XXXXX //图片点阵灰度数据
printer.printImg(img);

对应的实现方式使用C#实现,draw2PxPoint方法将指定图片转成ESCPOS打印机可以识别的图片打印指令

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace CommonPrint.ImageUtil
{
    public class ImageHelper
    {
        public static Image UrlToImage(string url)
        {
            WebClient mywebclient = new WebClient();
            byte[] Bytes = mywebclient.DownloadData(url);
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                Image outputImg = Image.FromStream(ms);
                return outputImg;
            }
        }

        public static int[] draw2PxPoint(Bitmap bmp)
        {
            //第一步调整图片高宽 到目标大小 商米设备最大支持124~125个点位的图片打印
            int newWidth = 120;
            int newHeight = 120;
            bmp = ZoomImage(bmp, newWidth, newHeight);

            List<int> data = new List<int>();
            //设置行距为0的指令
            data.Add(0x1B);
            data.Add(0x33);
            data.Add(0x00);

            int temp = 0;
            int turn = 0;
            // 逐行打印
            for (int j = 0; j < bmp.Height / 12f; j++)
            {
                //打印图片的指令
                data.Add(0x1B);
                data.Add(0x2A);
                data.Add(32);//单精度
                data.Add((byte)(bmp.Width % 256)); //nL  因为一个Byte最大255 表示大于255的数据需要用2位来处理( nL+nH×256)
                data.Add((byte)(bmp.Width / 256)); //nH
                                                   //对于每一行,逐列打印
                for (int i = 0; i < bmp.Width; i++)
                {
                    每一列24个像素点,分为3个字节存储
                    for (int m = 0; m < 3; m++)
                    {
                        StringBuilder sb = new StringBuilder();
                        //一次性读4个点 单精度 需要放大Y轴方向的数据 所以取一个点占2位
                        for (int n = 0; n < 4; n++)
                        {
                            var p = px2Byte(i, j * 12 + m * 4 + n, bmp);
                            //每个点Y轴方向重复一次 占2位
                            sb.Append(p);
                            sb.Append(p);
                        }
                        //Java上 byte 是-128 ~ 127  255=-1
                        int res = Convert.ToInt32(sb.ToString().Substring(1, 7), 2);
                        if (res > 127) {
                            res = res - 256;
                        }
                        data.Add((byte)res);
                    }
                }
                data.Add(10);//换行
            }
            bmp.Dispose();
            return data.ToArray();
        }

        public static byte px2Byte(int x, int y, Bitmap bmp)
        {
            if (x < bmp.Width && y < bmp.Height)
            {
                byte b;
                Color color = bmp.GetPixel(x, y);
                int gray = (int)(0.29900 * color.R + 0.58700 * color.G + 0.11400 * color.B);
                if (gray > 128)
                {
                    b = 0;
                }
                else
                {
                    b = 1;
                }
                return b;
            }
            return 0;
        }

        /// <summary>
        /// 等比例缩放图片
        /// </summary>
        /// <param name="bitmap">图片</param>
        /// <param name="destHeight">高度</param>
        /// <param name="destWidth">宽度</param>
        /// <returns></returns>
        private static Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if ((sourWidth * destHeight) > (sourHeight * destWidth))
                    {
                        width = destWidth;
                        height = (destWidth * sourHeight) / sourWidth;
                    }
                    else
                    {
                        height = destHeight;
                        width = (sourWidth * destHeight) / sourHeight;
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.Transparent);
                //设置画布的描绘质量
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在传统的PC应用中,通过直接调用打印机驱动程序的方式可以轻松地实现对蓝牙打印机的调用。但是,在Web应用和移动应用中,这种方式并不适合,所以我们需要寻找一种新的解决方法。 其中一种解决方案是使用JavaScript库或框架,比如原生JavaScript、jQuery和React等。这些工具可以为我们提供跨平台或跨浏览器的API,使得我们可以屏蔽底层的硬件驱动细节,从而更容易地实现对蓝牙打印机的调用。 实现蓝牙打印机的调用需要遵循ESC / POS打印机语言规范。ESC / POS是一种通用的打印机语言,被各种打印机采用,包括热敏和针式打印机。这种语言通过控制位、字符和命令来描述打印机的行为,每个命令都会发送给打印机的控制寄存器。 要实现对蓝牙打印机的调用,首先需要链接蓝牙打印机,这可以通过调用浏览器的Web Bluetooth API来完成。一旦与打印机建立连接,我们就可以通过发送ESC / POS命令来控制打印机,从而实现小票图片打印。 对于小票打印,我们需要设计好小票模板并将其转换为ESC / POS命令。具体来说,需要先设置打印机的一些参数,比如字符大小和行距,然后将文本和表格等元素添加到模板中,最后将整个模板转换为ESC / POS命令并发送给打印机即可。 对于图片打印,我们需要将图片转换为位图,并将其转换为ESC / POS命令。具体操作可以使用像CW浏览器的Canvas API在浏览器中渲染位图文件,然后将渲染后的位图文件转换为ESC / POS命令并发送给打印机即可。 总之,实现对蓝牙打印机的调用需要理解ESC / POS语言规范,并使用Web Bluetooth API和Canvas API等便利的工具来实现。虽然这种方法需要花费一些精力来学习和开发,但它可以轻松地在Web应用和移动应用中实现对蓝牙打印机的调用,具有很好的可移植性和开发效率。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值