0

I am using TDelphiZXingQRCode to generate QRCode in FMX. I have a problem when saving an image to BITMAP: image dimensions are always 29x29 I don't understand how to make a picture 100x100. I would be grateful if you help me. enter image description here

The problem is that QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);

QRCode.Rows and QRCode.Columns is 29

How to resize and trace the value Columns and Rows = 100?

I think, it's around here somewhere (in TDelphiZXingQRCode ) FElements := GenerateQRCode(FData, Ord(FEncoding)); FRows := Length(FElements) + FQuietZone * 2;

W300I
  • 11
  • 2
  • I think the easiest way is to use TCanvas.DrawBitmap to create the resized version before saving it. Create a new bitmap object and set the size to 100x100 using SetSize. Lets call it Bitmap100. Then do. Bitmap100.Canvas.BeginScene; Bitmap100.Canvas.DrawBitmap(QRCodeBitmap, QRCodeBitmap.Bounds, Bitmap100.Bounds, 1); Bitmp100.Canvas.EndScene; Bitmap100.SaveToFile('QR100.png'); . Note that 100 isn't an exact multiple of 29, so the result will have aliasing effects. – XylemFlow Feb 15 '22 at 14:13
  • Alternatively, and probably better, you should draw the pixels from the QR code data as rectangles using TBitmap.ClearRect. You will have to work out the position, width and height of each square pixel by using the ratio of 100/29. I would first clear the bitmap to white with TBitmap.Clear and then draw the black blocks only. – XylemFlow Feb 15 '22 at 14:30

1 Answers1

0

I would draw your bitmap from the QR code like this. This assumes that your QR code is square.

procedure TForm1.Button1click(Sender: Tobject);
const
  bmpSize : Integer = 100;
var
  QRCode: TDelphiZXingORCode; 
  Row, Column: Integer;
  Scale : Single;
begin
  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := edtText.Text;
    QRCode.Encoding := TQRCodeEncoding(cmbEncoding.ItemIndex);
    QRCode.QuietZone := StrToIntDef(edtQuietZone.Text, 4);
    QRCodeBitmap.SetSize(bmpSize, bmpSize);
    QRCodeBitmap.Clear(TAlphaColors.White);
    Scale := bmpSize / QRCode.Rows;
    for Row := 0 to QRCode.Rows - 1 do 
    begin
      for Column := 0 to QRCode.Columns - 1 do 
      begin
        if QRCode.IsBlack[Row, Column] then 
          QRCodeBitmap.ClearRect(TRectF.Create(PointF(Column, Row)*Scale, Scale, Scale), TAlphaColors.Black);
      end; 
    end;
  finally
    QRCode.Free;
  end;
  PaintBox1.Repaint;
end;
XylemFlow
  • 963
  • 5
  • 12