delphi安卓下热敏小票打印 源码
时间: 2023-07-05 15:19:58 浏览: 108
以下是Delphi安卓下热敏小票打印的一个示例源码,可以适用于一些常见型号的热敏打印机。需要注意的是,具体的打印指令格式和打印机型号有关,需要根据自己的打印机手册进行调整。
```delphi
unit uPrintUtil;
interface
uses
System.SysUtils, System.Classes, System.Types, System.IniFiles, FMX.Printer, FMX.Printer.Android,
FMX.Types, FMX.Graphics, Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.Os, Androidapi.JNI.Provider, Androidapi.Helpers, Androidapi.JNI.Print,
Androidapi.JNIBridge, FMX.Platform.Android, FMX.Forms, FMX.Dialogs;
type
TPrintUtil = class(TObject)
private
class var FPrintManager: JPrintManager;
class var FPrintAttributes: JPrintAttributes;
class var FPrintDocumentAdapter: JPrintDocumentAdapter;
class var FPrintJob: JPrintJob;
class var FPrintContent: TStringList;
class var FPrintIndex: Integer;
class procedure DrawPage(canvas: JCanvas; pageWidth, pageHeight, pageIndex: Integer);
class procedure StartPrintJob(printerName: string);
class procedure EndPrintJob;
public
class procedure Print(printerName: string; content: TStringList);
end;
implementation
{ TPrintUtil }
class procedure TPrintUtil.Print(printerName: string; content: TStringList);
begin
FPrintContent := content;
FPrintIndex := 0;
StartPrintJob(printerName);
end;
class procedure TPrintUtil.DrawPage(canvas: JCanvas; pageWidth, pageHeight, pageIndex: Integer);
var
paint: JPaint;
rect: JRectF;
text: string;
lineHeight, x, y, i, j: Integer;
begin
paint := TJPaint.Create;
rect := TJRectF.Create(0, 0, pageWidth, pageHeight);
lineHeight := Round(paint.getTextSize);
x := 0;
y := lineHeight;
i := FPrintIndex;
while (i < FPrintContent.Count) and (y + lineHeight < pageHeight) do
begin
text := FPrintContent[i];
if Length(text) > 0 then
begin
if text[1] = #$1B then
begin // 打印机控制命令
if (Length(text) > 1) and (text[2] = #$21) then
begin
if (Length(text) > 3) and (text[3] = #1) then
paint.setFakeBoldText(True)
else
paint.setFakeBoldText(False);
end;
if (Length(text) > 1) and (text[2] = #$21) then
begin
if (Length(text) > 3) and (text[3] = #8) then
paint.setTextSize(lineHeight * 2)
else
paint.setTextSize(lineHeight);
end;
end
else
canvas.drawText(StringToJString(text), x, y, paint);
end;
Inc(y, lineHeight);
Inc(i);
end;
FPrintIndex := i;
if FPrintIndex < FPrintContent.Count then
canvas.drawPage(rect);
end;
class procedure TPrintUtil.EndPrintJob;
begin
FPrintAttributes := nil;
FPrintDocumentAdapter := nil;
FPrintJob := nil;
FPrintManager := nil;
FPrintContent := nil;
end;
class procedure TPrintUtil.StartPrintJob(printerName: string);
var
printService: JPrintService;
begin
FPrintManager := TJPrintManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.PRINT_SERVICE));
if not Assigned(FPrintManager) then
begin
ShowMessage('打印服务不可用!');
Exit;
end;
if not FPrintManager.isPrintServiceEnabled then
begin
ShowMessage('请先开启打印服务!');
Exit;
end;
printService := FPrintManager.getPrintService;
if not Assigned(printService) then
begin
ShowMessage('打印服务不可用!');
Exit;
end;
FPrintAttributes := TJPrintAttributes.Wrap(TJPrintAttributes.JavaClass.builder.setMediaSize(TJPrintAttributes.JavaClass.MediaSize.ISO_A4)
.setResolution(TJPrintAttributes.JavaClass.Resolution("300", "300", TJPrintAttributes.JavaClass.ResolutionDpi.DPI_INCH))
.setColorMode(TJPrintAttributes.JavaClass.COLOR_MODE_COLOR)
.setMinMargins(TJPrintAttributes.JavaClass.Margins.NO_MARGINS).build);
FPrintDocumentAdapter := TJPrintDocumentAdapter.JavaClass.init(
procedure (canvas: JCanvas; page: JInteger)
begin
DrawPage(canvas, FPrintAttributes.getMediaSize.getWidthMils, FPrintAttributes.getMediaSize.getHeightMils, page);
end,
procedure
begin
EndPrintJob;
end);
FPrintJob := printService.createPrintJob;
if not Assigned(FPrintJob) then
begin
ShowMessage('打印失败!');
EndPrintJob;
Exit;
end;
FPrintJob.setPrintAttributes(FPrintAttributes);
FPrintJob.setPrintJobName(StringToJString('打印任务'));
if FPrintJob.isCancelled then
begin
EndPrintJob;
Exit;
end;
FPrintJob.start;
while not FPrintJob.isCompleted do
begin
if FPrintIndex < FPrintContent.Count then
FPrintJob.print(FPrintDocumentAdapter, FPrintAttributes);
end;
EndPrintJob;
end;
end.
```
以上代码中,主要用到了Android打印机相关的API,将打印内容通过PrintDocumentAdapter逐页绘制到画布上,然后传递给PrintJob进行打印。在绘制的过程中,需要解析打印内容中的控制命令(例如设置字体大小、加粗等),并进行相应的处理。
在打印之前,需要先开启设备的打印服务,并确保打印机已连接到设备。具体使用方法可以参考Delphi官方文档和Android官方文档。
阅读全文