.NET MAUI Android 对接商米移动手持终端打印JAVA SDK
一、前言(整个流程除了调整打印模板位子,操作下来还是挺快的 几分钟就搞定,文章图片比较多大家可能感觉操作比较麻烦,其实挺简单的)
因甲方需求,需要对现项目进行升级,需要增加移动手持终端进行巡检,巡检时还需要对商品进行抽检并打印热敏不干胶标签进行贴到抽样样品上。在这样的背景下便在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自动生成的代码没有删除,也再文件中。以下是完整代码,应为是技术性测试所以代码写的不是很规范,打印标签的位子也是写的很随意,正式放到项目中将优化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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(); } } } } |
五、完成对接
通过以上操作便可以打印出标签了,以下是测试效果。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人