Recent

Author Topic: LazbarCode save as image  (Read 5152 times)

makgab

  • Jr. Member
  • **
  • Posts: 55
LazbarCode save as image
« on: March 17, 2016, 10:28:45 am »
Hi!
How can I save the generated QR code as image?
For example as PNG format.
Generating is ok:
Quote
procedure TForm1.Button1Click(Sender: TObject);
begin
  BarcodeQR1.Text:=Edit1.Text;
  // BarcodeQR1.Generate;
end;

wp

  • Hero Member
  • *****
  • Posts: 12579
Re: LazbarCode save as image
« Reply #1 on: March 17, 2016, 10:56:05 am »
The Barcode component has a PaintOnCanvas method which you can use to paint on a bitmap canvas:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4.   R: TRect;
  5. begin
  6.   bmp := TBitmap.Create;
  7.   try
  8.     R := Rect(0, 0, BarcodeQR1.Width, BarcodeQR1.Height);
  9.     bmp.SetSize(BarcodeQR1.Width, BarcodeQR1.Height);
  10.     bmp.Canvas.Brush.Color := clWhite;
  11.     bmp.Canvas.FillRect(R);
  12.     BarcodeQR1.PaintOnCanvas(bmp.Canvas, R);
  13.     bmp.SaveToFile('d:\barcode.bmp');
  14.   finally
  15.     bmp.Free;
  16.   end;
  17. end;

makgab

  • Jr. Member
  • **
  • Posts: 55
Re: LazbarCode save as image
« Reply #2 on: March 17, 2016, 11:03:33 am »
thanks! It works.
I modified it with format png:
Quote
procedure TForm1.Button2Click(Sender: TObject);
var
  bmp: TBitmap;
  R: TRect;
  png : TPortableNetworkGraphic;
begin
  // bmp, png
  bmp := TBitmap.Create;
  png := TPortableNetworkGraphic.Create;

  try
    // bmp
    R := Rect(0, 0, BarcodeQR1.Width, BarcodeQR1.Height);
    bmp.SetSize(BarcodeQR1.Width, BarcodeQR1.Height);
    bmp.Canvas.Brush.Color := clWhite;
    bmp.Canvas.FillRect(R);
    BarcodeQR1.PaintOnCanvas(bmp.Canvas, R);
    bmp.SaveToFile('barcode.bmp');
    // png
    png.Assign(bmp);
    png.SaveToFile('barcode.png');

  finally
    bmp.Free;
    png.Free;
  end;
end;

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: LazbarCode save as image
« Reply #3 on: March 22, 2024, 03:36:39 am »
The Barcode component has a PaintOnCanvas method which you can use to paint on a bitmap canvas:

I know it's old topic and it's about QR code. I wanted to save PDF417 to bitmap and this method doesn't work (because width is always 225 and height is always 110).

I solved it like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4.   msBitmap: TMemoryStream;
  5. begin
  6.   bmp := TBitmap.Create;
  7.   msBitmap := TMemoryStream.Create;
  8.   try
  9.     BarcodeQR1.SaveToStream(msBitmap, TBitmap);
  10.     msBitmap.Seek(0, soBeginning);
  11.     bmp.LoadFromStream(msBitmap, msBitmap.Size);
  12.  
  13.     bmp.SaveToFile('d:\barcode.bmp');
  14.   finally
  15.     msBitmap.Free;
  16.     bmp.Free;
  17.   end;
  18. end;

lainz

  • Hero Member
  • *****
  • Posts: 4685
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: LazbarCode save as image
« Reply #4 on: March 22, 2024, 03:43:12 am »
Maybe that's the bitmap resolution despite how is displayed on canvas.

You must scale it yourself....

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: LazbarCode save as image
« Reply #5 on: March 22, 2024, 11:02:16 am »
Maybe that's the bitmap resolution despite how is displayed on canvas.

You must scale it yourself....

I thought Width and Height property of barcode object set when barcode is generated.
In SaveToStream method width and height are set from Exact_Width and Exact_Height from rendered symbol.
I found this comment in zint unit:
Code: Pascal  [Select][+][-]
  1.     // added by wp
  2.     exact_width: single;              // exact width of symbol after rendering. symbol^.width may be wrong...
  3.     exact_height: single;             // height of symbol after rendering.

If I call GetPreferredSize method, I seem to get correct width and height.

I tested like this:
Code: Pascal  [Select][+][-]
  1. procedure CreatePDF417(AText: String);
  2. var barcode: TBarcodePDF417;
  3.     bitmap: TBitmap;
  4.     msBitmap: TMemoryStream;
  5.     w, h: Integer;
  6.     r: TRect;
  7. begin
  8.   barcode := TBarcodePDF417.Create(nil);
  9.   try
  10.     barcode.RecommendedSymbolSize := True;
  11.     barcode.Text := AText;
  12.     WriteLn('Generated barcode width: ', barcode.Width, ', height: ', barcode.Height);
  13.  
  14.     bitmap := TBitmap.Create;
  15.     msBitmap := TMemoryStream.Create;
  16.     try
  17.       barcode.SaveToStream(msBitmap, TBitmap);
  18.       msBitmap.Seek(0, soBeginning);
  19.       bitmap.LoadFromStream(msBitmap, msBitmap.Size);
  20.       WriteLn('BMP from stream width: ', bitmap.Width, ', height: ', bitmap.Height);
  21.     finally
  22.       msBitmap.Free;
  23.       bitmap.Free;
  24.     end;
  25.  
  26.     barcode.GetPreferredSize(w, h);
  27.     WriteLn('Preffered barcode size width: ', w, ', height: ', h);
  28.     barcode.Width := w;
  29.     barcode.Height := h;
  30.  
  31.     bitmap := TBitmap.Create;
  32.     try
  33.       r := Rect(0, 0, barcode.Width, barcode.Height);
  34.       bitmap.SetSize(barcode.Width, barcode.Height);
  35.       bitmap.Canvas.Brush.Color := clWhite;
  36.       bitmap.Canvas.FillRect(r);
  37.       barcode.PaintOnCanvas(bitmap.Canvas, r);
  38.       WriteLn('BMP from canvas width: ', bitmap.Width, ', height: ', bitmap.Height);
  39.     finally
  40.       bitmap.Free;
  41.     end;
  42.   finally
  43.     barcode.Free;
  44.   end;
  45. end;

 

TinyPortal © 2005-2018