【发布时间】:2019-12-31 11:01:13
【问题描述】:
我有这个代码是成功使用蓝牙热敏打印机使用 android 使用 firemonkey delphi 打印文本, 我的朋友修改为打印位图,但过程 bitmaptostr 出现任何错误访问冲突。
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 才能在打印机上打印位图。我还没有看到任何打印机能够从一堆文本中打印位图。通常它们需要二进制共振峰的位图数据,当然还有一个特定的命令来告诉打印机下一个数据块代表图像而不仅仅是文本。
-
也许你有推荐代码。
-
恐怕不,因为它也可能取决于打印机到打印机
标签: android delphi bluetooth firemonkey thermal-printer