一、前言(整个流程除了调整打印模板位子,操作下来还是挺快的 几分钟就搞定,文章图片比较多大家可能感觉操作比较麻烦,其实挺简单的)
因甲方需求,需要对现项目进行升级,需要增加移动手持终端进行巡检,巡检时还需要对商品进行抽检并打印热敏不干胶标签进行贴到抽样样品上。在这样的背景下便在JD进行了采样,进行技术性研究。本文章将一步一步教大家如何使用.NET MAUI 来集成java printerx 来实现使用MAUI 在移动终端上进行标签打印。 下方是采购的移动终端设备。

二、下载AAR 开发SDK
通过官方文档我们得知 设备的打印SDK包是 printerx,开发包是JAVA开发的,这种情况如何使用MAUI进行对接呢?这难不倒我们,我们今天就用.NET MAUI 来进行集成 printerx

1、打开https://mvnrepository.com 网站,搜索 printerx
https://mvnrepository.com/search?q=printerx

2、点击 printerx 进去后,我们选择最新版 “1.0.14”

3、然后点击 View All ,将看到如下界面。

4、第一个 printerx-1.0.14.aar 是我们需要下载的文件,点击将来文件下载到本地桌面。

三、在MAUI中集成AAR SDK
1、是用VS2022 新建MAUI项目(.NET 7),然后添加新项目“Android 绑定库(Xmarin)”,没有“Android 绑定库(Xmarin)”的在installer中选择安装 Xmarin。


2、打开“Android 绑定库(Xmarin)”项目,打开 Jars文件夹,将之前下载的 printerx-1.0.14.aar 拷贝进文件夹内。

3、右击 printerx-1.0.14.aar 选择属性,将生成操作选择 “LibraryProjectZip”。

4、查看项目属性,和我的保持一致。

5、将MAUI项目中 安卓端引用 “Android 绑定库(Xmarin)”项目。

6、应为前几次测试的时候,WIndows、IOS端有报错提示不兼容发生,我将其他端都删掉了,只保留了安卓端。
四、调试移动终端
1、为了方便测试 我把代码写到了"MainPage.xaml.cs",MAUI自动生成的代码没有删除,也再文件中。以下是完整代码,应为是技术性测试所以代码写的不是很规范,打印标签的位子也是写的很随意,正式放到项目中将优化。- using Android.Icu.Text;
- using Com.Sunmi.Printerx;
- using Java.Interop;
- using Java.Util;
- namespace MauiApp1
- {
- public partial class MainPage : ContentPage
- {
- int count = 0;
- public MainPage()
- {
- InitializeComponent();
- }
- public Com.Sunmi.Printerx.PrinterSdk.Printer Printer;
- private void OnCounterClicked(object sender, EventArgs e)
- {
- var listen = new PrinterListen();
- Com.Sunmi.Printerx.PrinterSdk.Instance.GetPrinter(
- Android.App.Application.Context, listen
- );
-
- count++;
- if (count == 1)
- CounterBtn.Text = $"Clicked {count} time";
- else
- CounterBtn.Text = $"Clicked {count} times";
- SemanticScreenReader.Announce(CounterBtn.Text);
- }
- public class PrintResult2 : Com.Sunmi.Printerx.Api.PrintResult
- {
-
- public override void OnResult(int p0, string p1)
- {
-
- }
- }
- //获取打印机监
- public class PrinterListen : Java.Lang.Object, Com.Sunmi.Printerx.PrinterSdk.IPrinterListen
- {
- public Com.Sunmi.Printerx.PrinterSdk.Printer Printer;
-
- public void OnDefPrinter(PrinterSdk.Printer p0)
- {
- this.Printer = p0;//获取到打印机
-
- var api = Printer.CanvasApi();
-
- api.InitCanvas(Com.Sunmi.Printerx.Style.BaseStyle.Style.SetWidth(52*8).SetHeight(35*8));//初始化
- api.RenderArea(Com.Sunmi.Printerx.Style.AreaStyle.Style.SetWidth(49*8).SetHeight(33*8)//设置打印区域
- .SetStyle(Com.Sunmi.Printerx.Enums.Shape.RectWhite)
- .SetPosX(0).SetPosY(0));
- api.RenderText("标题", //打印字符串
- Com.Sunmi.Printerx.Style.TextStyle.Style
- .SetAlign(Com.Sunmi.Printerx.Enums.Align.Center)
- .SetTextSize(32)
- .SetPosX(208).SetPosY(16)
- );
- api.RenderText("商品名称(车牌号)",
- Com.Sunmi.Printerx.Style.TextStyle.Style
- .SetAlign(Com.Sunmi.Printerx.Enums.Align.Center)
- .SetTextSize(30)
- .SetPosX(208).SetPosY(40 + 10)
- .EnableBold(true)
- );
- api.RenderBarCode("2311202003400013",//打印条形码
- Com.Sunmi.Printerx.Style.BarcodeStyle.Style
- .SetAlign(Com.Sunmi.Printerx.Enums.Align.Center)
- .SetPosX(208).SetPosY(38+12+30)
- .SetBarHeight(80).SetDotWidth(3)
- .SetWidth(350)
- );
- api.RenderText("2311202003400013",
- Com.Sunmi.Printerx.Style.TextStyle.Style
- .SetAlign(Com.Sunmi.Printerx.Enums.Align.Center)
- // .SetTextSize(24)
- .SetPosX(208).SetPosY(38+32+95)
- );
- api.RenderText("商户姓名:姓名007",
- Com.Sunmi.Printerx.Style.TextStyle.Style
- // .SetAlign(Com.Sunmi.Printerx.Enums.Align.Center)
- // .SetTextSize(20)
- .EnableBold(true)
- .SetPosX(30).SetPosY(38 + 32 + 100 +23)
- );
- api.RenderText("抽样时间:2023-11-02 13:00",
- Com.Sunmi.Printerx.Style.TextStyle.Style
- .EnableBold(true)
- .SetPosX(30).SetPosY(38 + 32 + 100 +25+25)
- );
- var printReuslt = new PrintResult2();
- api.PrintCanvas(1, printReuslt);
- }
- public void OnPrinters(IList<PrinterSdk.Printer> p0)
- {
- this.Printer.Dispose();
- }
- }
- }
- }
复制代码 五、完成对接
通过以上操作便可以打印出标签了,以下是测试效果。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |