【问题标题】:Printing Bitmap Using Bluetooth Thermal Printer With Firemonkey Android使用带有 Firemonkey Android 的蓝牙热敏打印机打印位图
【发布时间】: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


【解决方案1】:

您在BitmapToStr() 中遇到的错误是您从不使用Map BMPDataBMP 位图。

将此添加到函数的开头:

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

最后:

finally
  bmp.Unmap(BMPData);
end;

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

【讨论】:

  • av 错误消失了,事实证明这个想法不能像@SilverWarrior 所说的用于打印
  • @mgk 首先,您需要了解 SO 的主要原则是“每个帖子一个问题”。不关注该原则的帖子通常因“过于宽泛”而被关闭。现在,您将您的问题描述为:..some error access violation with procedure bitmaptostr. 并问了一个问题:任何人都有一些解决方案..? 我回答了你的问题现在也确认是正确答案:av error has消失...。您的代码还存在其他问题,这并没有影响我回答您的问题这一事实。发布有关您的转移算法的新问题
猜你喜欢
  • 1970-01-01
  • 2018-04-28
  • 2019-08-27
  • 2013-06-03
  • 2013-11-11
  • 2019-11-23
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多