使用蓝牙热敏打印机与Firemonkey Android打印位图



我有这个代码是成功的使用蓝牙热敏打印机打印文本使用安卓使用火猴德尔菲, 我的朋友修改了打印位图,但任何错误访问与过程位图冲突。

procedure TBluetoothPrinter.Send(Data: TArray<Byte>);
begin
if Data = nil then
Exit; // nothing to write
Check(OutputStream <> nil, 'Cannot retrieve output stream');
OutputStream.write(ToJavaByteArray(Data));
end;
procedure Printing(sText: string);
begin
with TBluetoothPrinter.Create do
begin
Send(TEncoding.ANSI.GetBytes(sText + CRLF));
end;
end;
function BitmapToStr(BMP: TBitmap; EscapeStr:String; SliceEscapeStr:String; BitsSlice: Byte = 8):String;
var
BMPData: TBitmapData;
AColor: TAlphaColor;
nCol, nRow, nIndex: integer;
nOffset, nBytePos, nBitPos: integer;
nSliceIndex, nLum: integer;
nSlice, nBit, nTmpBit, BytesSlice: byte;
ADots: Array of boolean;
sSlice: String;
begin
try
SetLength(ADots, (BMP.Height * BMP.Width));
nIndex := 0;
for nRow := 0 to BMP.Height-1 do
begin
for nCol := 0 to BMP.Width-1 do
begin
AColor := BMPData.GetPixel(nCol, nRow);
nLum := Trunc((TAlphaColorRec(AColor).R * 0.3)  + (TAlphaColorRec(AColor).G * 0.59) + (TAlphaColorRec(AColor).B * 0.11));
ADots[nIndex] := (nLum < 127);
inc(nIndex);
end;
end;
BytesSlice := (BitsSlice div 8);
if BitsSlice mod 8 > 0 then
inc(BytesSlice);
Result := EscapeStr;
nOffset := 0;
while (nOffset < BMP.Height) do
begin
Result := Result + SliceEscapeStr;
for nCol := 0 to BMP.Width-1 do
begin
for nSliceIndex := 0 to BytesSlice - 1 do
begin
nSlice := 0;
for nBit := 0 to 7 do
begin
nBytePos := (((nOffset div 8) + nSliceIndex) * 8) + nBit;
nBitPos := (nBytePos * BMP.Width) + nCol;
nTmpBit := 0;
if (nBitPos < Length(ADots)) then
begin
if ADots[nBitPos] then
nTmpBit := 1
else
nTmpBit := 0;
end;
nSlice := nSlice or (nTmpBit shl (7 - nBit));
end;
Result := Result + Chr(nSlice);
end;
end;
inc(nOffset, BitsSlice);
Result := Result + CRLF;
end;
finally
ADots := nil;
end;
end;

有人有一些解决方案或样品参考吗?

BitmapToStr()中的错误是,您永远不会使用BMP位图MapBMPData

将此添加到函数的开头:

bmp.Map(TMapAccess.Read, BMPData);
try
...

最后:

finally
bmp.Unmap(BMPData);
end;

另一方面,您不需要try..finally..end块来确保正确清理ADots阵列。

最新更新