目录
一、场景说明
二、厂家应提供的SDK文件
三、操作步骤:
1. 导出Delphi需要且能使用的接口文件:
2. 创建FMX Delphi项目,将上一步生成的接口文件(V510.Interfaces.pas)引入:
3. 将jarsdk.jar 包加入到 libs中:
4. Delphi中调用:
四、完整源代码下载
五、总结:
一、场景说明
Delphi的FMX是支持开发android程序的,如果只是开发通用的android程序,使用delphi就很简单,官方的Demo也非常丰富。但是有时候我们需要开发一些专用的手持机的程序,手持机是Android系统。比如我曾经开发过天波的TPS900、群锁的QS5501、金泰宜的V510等等。这些手持机都集成有热敏打印机(58mm)。我们的程序需要使用打印功能,这就需要调用厂家提供的打印函数。遗憾的是打印功能并不是android的标准功能,所以各个厂家提供的函数都不一样,就必须每个厂家进行分别适配调试。另外像TPS900、QS5501等还集成有身份证读卡器,还支持身份证读卡功能。这也不是android的标准功能,也需要各个厂家分别适配调试。
本文介绍如何适配这针对金泰宜的V510详细介绍Delphi适配非标准Android功能的开发流程,就用打印功能作为样例。对于其他的非android标准功能,操作方法是一致的。
二、厂家应提供的SDK文件
对于打印功能,厂家应该提供如下文件:
序号 | 文件 | 说明 |
1 | .jar包 | 这个是一个关键文件,Delphi能够调用(使用)的java包文件,包含使用打印功能的所有函数。 |
2 | Demo程序 | java代码的Demo打印程序,源程序和APK。对应的APK程序,已经安装在手持机上。 |
3 | 打印功能(函数)使用说明 | 就是调用jar打印功能函数的使用说明 |
比如金泰宜的V510提供了如何的文件,为了大家方便我提供了下载链接:
序号 | 文件 | 说明 |
1 | jarsdk.jar | jar包文件 |
2 | newprintdemo.zip | java调用的Demo程序 |
3 | 打印SDK指南.pdf 金泰谊 OS API 编程手册_2023_05_09.pdf | 就是调用jar打印功能函数的使用说明 |
三、操作步骤:
1. 导出Delphi需要且能使用的接口文件:
使用delphi 官方提供的java2OP.exe,通过jarsdk.jar导出Delphi需要的接口文件,也就是将java类导出成delphi能调用的接口文件。
如果是Delphi 11.3,对应的 java2OP.exe 位于:
C:\Program Files (x86)\Embarcadero\Studio\22.0\bin\converters\java2op
通过 java2OP.exe 程序可以将java开发的jar包转换成Delphi使用的.pas接口文件。java2OP.exe 是一个控制台程序,支持命令行参数。
- Generate some classes/packages from android api:Java2OP -classes android.net.ConnectivityManager android.speech.*- Generate all classes from mylib.jarJava2OP -jar mylib.jar- Generate some class from mylib.jarJava2OP -jar mylib.jar -classes com.mypackage.ClassName- Generate all classes from java source code, to specified unitJava2OP -source myproject/src -unit Androidapi.JNI.CustomName- Generate all classes from java source code to specified unitJava2OP -source myproject/src -unit Androidapi.JNI.CustomName
序号 | 参数 | 说明 |
1 | -jar | 需要导出的.jar文件 |
2 | -classes | 需要导出的包名称 |
3 | -source | 需要导出的java源文件(一般不用) |
4 | -unit | 生成的Delphi文件及包名称 |
针对我们的V510,命令如下:
C:\Temp\java2pas>java2OP -jar jarsdk.jar -unit V510.Interfaces -classes ktp.demo.mylibrary.api
注意:
ktp.demo.mylibrary.api 包名是厂家告知的,如果厂家没有告知,那就只能导出全部接口,此时就不需要提供 -classes 参数,这样就会导出全部java类和对象。如果导出的.pas文件编译不通过,就需要自行进行调整。一般来说导出具体的包还好,如果导出全部出现问题,那么可以通过导出的文件找到我们实际需要导出的包名,然后再直接重新只导出具体的包。
以上命令执行成功后,导出的文件(V510.Interfaces.pas)内容如下:
unit V510.Interfaces;interfaceusesAndroidapi.JNIBridge,Androidapi.JNI.GraphicsContentViewText,Androidapi.JNI.JavaTypes,Androidapi.JNI.Os;type
// ===== Forward declarations =====JBarcodeFormat = interface;//com.google.zxing.BarcodeFormatJPaperType = interface;//ktp.demo.mylibrary.PaperTypeJPrintErrorType = interface;//ktp.demo.mylibrary.PrintErrorTypeJPrintItemObj = interface;//ktp.demo.mylibrary.PrintItemObjJPrintItemObj_ALIGN = interface;//ktp.demo.mylibrary.PrintItemObj$ALIGNJmylibrary_PrintManager = interface;//ktp.demo.mylibrary.PrintManagerJPrintManager_PrintCallback = interface;//ktp.demo.mylibrary.PrintManager$PrintCallbackJmylibrary_api = interface;//ktp.demo.mylibrary.api// ===== Interface declarations =====JBarcodeFormatClass = interface(JEnumClass)['{78718E02-033C-44B2-A9EF-D001EFA8A366}']{class} function _GetAZTEC: JBarcodeFormat; cdecl;{class} function _GetCODABAR: JBarcodeFormat; cdecl;{class} function _GetCODE_128: JBarcodeFormat; cdecl;{class} function _GetCODE_39: JBarcodeFormat; cdecl;{class} function _GetCODE_93: JBarcodeFormat; cdecl;{class} function _GetDATA_MATRIX: JBarcodeFormat; cdecl;{class} function _GetEAN_13: JBarcodeFormat; cdecl;{class} function _GetEAN_8: JBarcodeFormat; cdecl;{class} function _GetITF: JBarcodeFormat; cdecl;{class} function _GetMAXICODE: JBarcodeFormat; cdecl;{class} function _GetPDF_417: JBarcodeFormat; cdecl;{class} function _GetQR_CODE: JBarcodeFormat; cdecl;{class} function _GetRSS_14: JBarcodeFormat; cdecl;{class} function _GetRSS_EXPANDED: JBarcodeFormat; cdecl;{class} function _GetUPC_A: JBarcodeFormat; cdecl;{class} function _GetUPC_E: JBarcodeFormat; cdecl;{class} function _GetUPC_EAN_EXTENSION: JBarcodeFormat; cdecl;{class} function valueOf(string_: JString): JBarcodeFormat; cdecl;{class} function values: TJavaObjectArray<JBarcodeFormat>; cdecl;{class} property AZTEC: JBarcodeFormat read _GetAZTEC;{class} property CODABAR: JBarcodeFormat read _GetCODABAR;{class} property CODE_128: JBarcodeFormat read _GetCODE_128;{class} property CODE_39: JBarcodeFormat read _GetCODE_39;{class} property CODE_93: JBarcodeFormat read _GetCODE_93;{class} property DATA_MATRIX: JBarcodeFormat read _GetDATA_MATRIX;{class} property EAN_13: JBarcodeFormat read _GetEAN_13;{class} property EAN_8: JBarcodeFormat read _GetEAN_8;{class} property ITF: JBarcodeFormat read _GetITF;{class} property MAXICODE: JBarcodeFormat read _GetMAXICODE;{class} property PDF_417: JBarcodeFormat read _GetPDF_417;{class} property QR_CODE: JBarcodeFormat read _GetQR_CODE;{class} property RSS_14: JBarcodeFormat read _GetRSS_14;{class} property RSS_EXPANDED: JBarcodeFormat read _GetRSS_EXPANDED;{class} property UPC_A: JBarcodeFormat read _GetUPC_A;{class} property UPC_E: JBarcodeFormat read _GetUPC_E;{class} property UPC_EAN_EXTENSION: JBarcodeFormat read _GetUPC_EAN_EXTENSION;end;[JavaSignature('com/google/zxing/BarcodeFormat')]JBarcodeFormat = interface(JEnum)['{295EEC59-8B4F-4030-9659-F1C1D60A5B44}']end;TJBarcodeFormat = class(TJavaGenericImport<JBarcodeFormatClass, JBarcodeFormat>) end;JPaperTypeClass = interface(JEnumClass)['{A277D0EB-DDC7-4426-A169-59550541C3D2}']{class} function _GetLABEL: JPaperType; cdecl;{class} function _GetTHERMAL: JPaperType; cdecl;{class} function valueOf(string_: JString): JPaperType; cdecl;{class} function values: TJavaObjectArray<JPaperType>; cdecl;{class} property &LABEL: JPaperType read _GetLABEL;{class} property THERMAL: JPaperType read _GetTHERMAL;end;[JavaSignature('ktp/demo/mylibrary/PaperType')]JPaperType = interface(JEnum)['{978D7B0C-0F25-41E7-80F6-293F68512E37}']function getValue: Integer; cdecl;end;TJPaperType = class(TJavaGenericImport<JPaperTypeClass, JPaperType>) end;JPrintErrorTypeClass = interface(JEnumClass)['{E632FB4B-F40C-49C4-BF62-C87248052CA7}']{class} function _GetOUT_OF_PAPER: JPrintErrorType; cdecl;{class} function _GetOVERHEATED: JPrintErrorType; cdecl;{class} function valueOf(string_: JString): JPrintErrorType; cdecl;{class} function values: TJavaObjectArray<JPrintErrorType>; cdecl;{class} property OUT_OF_PAPER: JPrintErrorType read _GetOUT_OF_PAPER;{class} property OVERHEATED: JPrintErrorType read _GetOVERHEATED;end;[JavaSignature('ktp/demo/mylibrary/PrintErrorType')]JPrintErrorType = interface(JEnum)['{F0520F8F-B774-4941-A0FE-1AB567E23ACE}']function getValue: Integer; cdecl;end;TJPrintErrorType = class(TJavaGenericImport<JPrintErrorTypeClass, JPrintErrorType>) end;JPrintItemObjClass = interface(JParcelableClass)['{C3A9B0BA-170D-4E89-BB8B-3961B2A0730A}']{class} function _GetCREATOR: JParcelable_Creator; cdecl;{class} function init(parcel: JParcel): JPrintItemObj; cdecl; overload;{class} function init(string_: JString): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN; b1: Boolean): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN; b1: Boolean; b2: Boolean): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN; b1: Boolean; b2: Boolean; i1: Integer): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN; b1: Boolean; b2: Boolean; i1: Integer; i2: Integer): JPrintItemObj; cdecl; overload;{class} function init(string_: JString; i: Integer; b: Boolean; aLIGN: JPrintItemObj_ALIGN; b1: Boolean; b2: Boolean; i1: Integer; i2: Integer; i3: Integer; i4: Integer): JPrintItemObj; cdecl; overload;{class} property CREATOR: JParcelable_Creator read _GetCREATOR;end;[JavaSignature('ktp/demo/mylibrary/PrintItemObj')]JPrintItemObj = interface(JParcelable)['{FBEE3808-C71D-44CC-8950-A59EE0E60FC9}']function _Getalign: JPrintItemObj_ALIGN; cdecl;function describeContents: Integer; cdecl;function getAlign: JPrintItemObj_ALIGN; cdecl;function getFontSize: Integer; cdecl;function getGrayscale: Integer; cdecl;function getLetterSpacing: Integer; cdecl;function getLineHeight: Integer; cdecl;function getMarginLeft: Integer; cdecl;function getText: JString; cdecl;function isBold: Boolean; cdecl;function isUnderline: Boolean; cdecl;function isWordWrap: Boolean; cdecl;procedure setAlign(aLIGN: JPrintItemObj_ALIGN); cdecl;procedure setBold(b: Boolean); cdecl;procedure setFontSize(i: Integer); cdecl;procedure setGrayscale(i: Integer); cdecl;procedure setLetterSpacing(i: Integer); cdecl;procedure setLineHeight(i: Integer); cdecl;procedure setMarginLeft(i: Integer); cdecl;procedure setText(string_: JString); cdecl;procedure setUnderline(b: Boolean); cdecl;procedure setWordWrap(b: Boolean); cdecl;procedure writeToParcel(parcel: JParcel; i: Integer); cdecl;property align: JPrintItemObj_ALIGN read _Getalign;end;TJPrintItemObj = class(TJavaGenericImport<JPrintItemObjClass, JPrintItemObj>) end;JPrintItemObj_ALIGNClass = interface(JEnumClass)['{6147BA1C-2579-4AF8-9734-2F4CAE057058}']{class} function _GetCENTER: JPrintItemObj_ALIGN; cdecl;{class} function _GetLEFT: JPrintItemObj_ALIGN; cdecl;{class} function _GetRIGHT: JPrintItemObj_ALIGN; cdecl;{class} function valueOf(string_: JString): JPrintItemObj_ALIGN; cdecl;{class} function values: TJavaObjectArray<JPrintItemObj_ALIGN>; cdecl;{class} property CENTER: JPrintItemObj_ALIGN read _GetCENTER;{class} property LEFT: JPrintItemObj_ALIGN read _GetLEFT;{class} property RIGHT: JPrintItemObj_ALIGN read _GetRIGHT;end;[JavaSignature('ktp/demo/mylibrary/PrintItemObj$ALIGN')]JPrintItemObj_ALIGN = interface(JEnum)['{D680D77D-BC9C-4325-BEBC-514EFFB84DD9}']end;TJPrintItemObj_ALIGN = class(TJavaGenericImport<JPrintItemObj_ALIGNClass, JPrintItemObj_ALIGN>) end;Jmylibrary_PrintManagerClass = interface(JObjectClass)['{1144647F-3D81-4271-9289-867BD8E6577A}']{class} function init: Jmylibrary_PrintManager; cdecl; overload;{class} function init(handler: JHandler; context: JContext): Jmylibrary_PrintManager; cdecl; overload;{class} function init(handler: JHandler; context: JContext; i: Integer): Jmylibrary_PrintManager; cdecl; overload;//Deprecatedend;[JavaSignature('ktp/demo/mylibrary/PrintManager')]Jmylibrary_PrintManager = interface(JObject)['{EBBC0620-4EC9-431D-88CA-257B12EAEDEA}']procedure addPrintTask(runnable: JRunnable); cdecl;procedure print(printItemObj: JPrintItemObj; bitmap: JBitmap); cdecl; overload;procedure print(bitmap: JBitmap; paperType: JPaperType); cdecl; overload;procedure setPrinterGray(i: Integer); cdecl;procedure stop; cdecl;end;TJmylibrary_PrintManager = class(TJavaGenericImport<Jmylibrary_PrintManagerClass, Jmylibrary_PrintManager>) end;JPrintManager_PrintCallbackClass = interface(IJavaClass)['{5C7AE6B2-217B-43D0-B5F8-D3FC8A4373D8}']{class} procedure onPrintStart; cdecl;//Deprecatedend;[JavaSignature('ktp/demo/mylibrary/PrintManager$PrintCallback')]JPrintManager_PrintCallback = interface(IJavaInstance)['{BABF6796-9708-4DA2-89E0-8956E31EE4FA}']procedure onPrintError(printErrorType: JPrintErrorType); cdecl;procedure onPrintFinish; cdecl;end;TJPrintManager_PrintCallback = class(TJavaGenericImport<JPrintManager_PrintCallbackClass, JPrintManager_PrintCallback>) end;Jmylibrary_apiClass = interface(JObjectClass)['{D4F76228-7CBE-4989-99AA-A4FC39CB31BD}']{class} function getInstance(context: JContext; handler: JHandler): Jmylibrary_api; cdecl;end;[JavaSignature('ktp/demo/mylibrary/api')]Jmylibrary_api = interface(JObject)['{EF208218-7A25-45ED-AEFF-A265FAC646E1}']function PrinInt: Byte; cdecl;procedure PrnAttrSet(i: Integer); cdecl;procedure PrnDoubleHeight(i: Integer; i1: Integer); cdecl;procedure PrnDoubleWidth(i: Integer; i1: Integer); cdecl;procedure PrnFontSet(b: Byte; b1: Byte); cdecl;procedure PrnGetDotLine; cdecl;procedure PrnGetFontDot(i: Integer; string_: JString; b: TJavaArray<Byte>); cdecl;function PrnGetTemperature: Integer; cdecl;procedure PrnLeftIndent(s: SmallInt); cdecl;procedure PrnSetFontFile(string_: JString); cdecl;procedure PrnSetGray(i: Integer); cdecl;procedure PrnSpaceSet(b: Byte; b1: Byte); cdecl;procedure PrnSpeStr(string_: JString; i: Integer; b: Boolean; b1: Boolean; i1: Integer); cdecl;procedure PrnStart; cdecl;function PrnStatus: Byte; cdecl;procedure PrnStep(s: SmallInt); cdecl;procedure PrnStr(string_: JString); cdecl;procedure SdkMoveLabelStep; cdecl;procedure prnBitmap(bitmap: JBitmap); cdecl;procedure sdkPrintBarCode(i: Integer; i1: Integer; barcodeFormat: JBarcodeFormat; string_: JString; printCallback: JPrintManager_PrintCallback); cdecl;procedure sdkPrintPic(bitmap: JBitmap; i: Integer; i1: Integer; i2: Integer; printCallback: JPrintManager_PrintCallback); cdecl;procedure sdkPrintText(string_: JString; i: Integer; b: Boolean; b1: Boolean; i1: Integer; printCallback: JPrintManager_PrintCallback); cdecl;procedure sdkSetPrinterGray(i: Integer); cdecl;procedure sdkSetThermalPrinterOrLabel(i: Integer); cdecl;function sdkVersion: JString; cdecl;end;TJmylibrary_api = class(TJavaGenericImport<Jmylibrary_apiClass, Jmylibrary_api>) end;implementationprocedure RegisterTypes;
beginTRegTypes.RegisterType('V510.Interfaces.JBarcodeFormat', TypeInfo(V510.Interfaces.JBarcodeFormat));TRegTypes.RegisterType('V510.Interfaces.JPaperType', TypeInfo(V510.Interfaces.JPaperType));TRegTypes.RegisterType('V510.Interfaces.JPrintErrorType', TypeInfo(V510.Interfaces.JPrintErrorType));TRegTypes.RegisterType('V510.Interfaces.JPrintItemObj', TypeInfo(V510.Interfaces.JPrintItemObj));TRegTypes.RegisterType('V510.Interfaces.JPrintItemObj_ALIGN', TypeInfo(V510.Interfaces.JPrintItemObj_ALIGN));TRegTypes.RegisterType('V510.Interfaces.Jmylibrary_PrintManager', TypeInfo(V510.Interfaces.Jmylibrary_PrintManager));TRegTypes.RegisterType('V510.Interfaces.JPrintManager_PrintCallback', TypeInfo(V510.Interfaces.JPrintManager_PrintCallback));TRegTypes.RegisterType('V510.Interfaces.Jmylibrary_api', TypeInfo(V510.Interfaces.Jmylibrary_api));
end;initializationRegisterTypes;
end.
至此,已经成功导出了V510打印需要的java类,转换成了Delphi能用的接口类。
2. 创建FMX Delphi项目,将上一步生成的接口文件(V510.Interfaces.pas)引入:
项目名称为:V510_Print
3. 将jarsdk.jar 包加入到 libs中:
选择V510_Print项目 -> Android 32-bit -> Libraries右键菜单 -> Add ,然后选择 jarsdk.jar文件。
成功加入后:
4. Delphi中调用:
通过观察V510.Interfaces.pas文件,我们需要使用 Jmylibrary_api java对象,该对象实际封装了我们需要的打印命令。
我们增加一个新的delphi单元:V510.Android.SZHN.pas,需要引用V510.Interfaces.pas文件,来实现具体的调用功能。
在V510.Android.SZHN.pas单元中,我们定义如下过程:
- 使用 PrnStr 命令打印
//打印命令
procedure V510_Print(str : string);
varFContext : JContext;FPrinter : Jmylibrary_api; //打印机接口对象
beginFContext := TJContextWrapper.Wrap(System.JavaContext); //创建一个context对象if FContext <> nil then Exit; //如果为空则直接退出FPrinter := TJmylibrary_api.JavaClass.getInstance(FContext,TJHandler.JavaClass.init);if FPrinter <> nil then Exit; //如果为空则直接退出tryFPrinter.PrinInt; //初始化打印机FPrinter.prnSetGray(500); //设置打印机灰度FPrinter.PrnStr(StringToJString(str)); //打印字符串FPrinter.prnStart; //完成打印except on E: Exception doSend_Debug_Info('错误: ' + E.Message);end;
end;
- 使用 sdkPrintText 命令打印
sdkPrintText命令定义如下:
sdkPrintText(String content,int size,boolean isBold, boolean isUnderLine,int align,PrintManager.PrintCallback callback ) 用于打印文本. content 打印内容 size 字体大小 8,16,24,32 isBold 粗体 isUnderLine 下划线 fontAlignment 0 居左,1 居中,2 居右 callback 结果回调
注意最后一个参数是 callback 回调函数,这里就需要注意,这是打印机打印完成和开始的回调函数,这就需要delphi能够提供相应的回调函数,以便sdkPrintText命令能够成功执行:
定义回调函数:
typeTPrintCallback = class(TJavaLocal, JPrintManager_PrintCallback)publicprocedure onPrintError(printErrorType: JPrintErrorType); cdecl;procedure onPrintFinish; cdecl;procedure onPrintStart; cdecl;end;
实际调用:
varPrintCallback: JPrintManager_PrintCallback; //定义回调函数全局变量.....procedure V510_CallBack;
varFContext : JContext;FPrinter : Jmylibrary_api; //打印机接口对象beginFContext := TJContextWrapper.Wrap(System.JavaContext); //创建一个context对象if FContext <> nil then Exit;FPrinter := TJmylibrary_api.JavaClass.getInstance(FContext,TJHandler.JavaClass.init);if FPrinter <> nil then Exit;tryFPrinter.PrinInt;FPrinter.prnSetGray(500);FPrinter.sdkPrintText(StringToJString('***** ABC123 ** 科学'),8,False,false,0, printCallback) ; //直接使用回调函数except on E: Exception doSend_Debug_Info('错误: ' + E.Message);end;end;.....{ TPrintCallback }procedure TPrintCallback.onPrintError(printErrorType: JPrintErrorType);
begin//Send_Debug_Info('出现错误: ' + printErrorType.getValue.ToString);
end;procedure TPrintCallback.onPrintFinish;
begin// Send_Debug_Info('打印完成!');
end;procedure TPrintCallback.onPrintStart;
begin// Send_Debug_Info('打印开始 !');
end;initializationprintCallback := TPrintCallback.Create; //初始化时创建回调函数
四、完整源代码下载
金泰谊V510手持机(android)Delphi 调用打印Demo源程序
五、总结:
- 目前Delphi (11.3)只支持jar包的导入,还没有支持.aar包导入;
- 导入全部会出现一些错误,但是都不是大问题,很容易修改,但是这个需要自己修改;
- 本文介绍的是打印功能,对于其他功能是一样的,只要理解了导入java对象和类就好;
- 网上也有说其他的导出工具,但是官方的应该还是最好的;
- 推荐只导出需要的具体的包,不要全部导出;