Delphi genera un código QR [PaintBox], lo guarda como un archivo local y lo agrega al control [Imagen]

Prueba RAD Studio 10.3

Controles necesarios: scEdit, scGPButton, Image, PaintBox
Inserte la descripción de la imagen aquí
necesita declarar : DelphiZXIngQRCode
dirección de descarga: https://pan.baidu.com/s/1dN_7WGAlOIGoSdJJviQoEQ
código de extracción: 8m9c
[Al mismo tiempo, recuerde agregar la ruta para buscarlo, no importa qué De esa manera]


  private
    QRCodeBitmap: TBitmap;
  public
    procedure Update;

Evento OnCreate y evento OnDestroy del formulario :

procedure TForm1.FormCreate(Sender: TObject);                                 // 窗体创建
begin
  QRCodeBitmap := TBitmap.Create;                                             // 代码图位
  Update;
end;

procedure TForm1.FormDestroy(Sender: TObject);                                // 窗体销毁
begin
  QRCodeBitmap.Free;
end;

Evento OnPaint de PaintBox :

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  Scale: Double;
begin
  PaintBox1.Canvas.Brush.Color := clRed;                                      // 背景颜色
  PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Height, PaintBox1.Width));   // 填充矩形
  if ((PaintBox1.Width > 0) and (PaintBox1.Height > 0)) then
  begin
    if PaintBox1.Width < PaintBox1.Height then
    begin
      Scale := PaintBox1.Width / QRCodeBitmap.Width;
    end
    else
    begin
      Scale := PaintBox1.Height / QRCodeBitmap.Height;
    end;
    { StretchDraw(Rect, Jpg); }
    { Rect:是你要画的区域(你要放大就设置的大一些,缩小就设置的小一些) }
    { Jpg:是要画的东西(我这里只是用Jpg文件做例子) }
    PaintBox1.Canvas.StretchDraw(Rect(0, 0, Trunc(Scale * QRCodeBitmap.Width), Trunc(Scale * QRCodeBitmap.Height)), QRCodeBitmap);
  end;
end;

El método Updata declarado por sí mismo :

procedure TForm1.Update;
var
  QRCode: TDelphiZXingQRCode;
  Column, Row: Integer;
begin
  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := scEdit1.Text;                                              // 数据
    QRCode.Encoding := TQRCodeEncoding(5);                                    // 编码方式 【qrUTF8BOM】
    QRCode.QuietZone := StrToIntDef(scEdit2.Text, 1);                         // 边框区域
    QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);                        // 设定大小
    {【Row:行  ;  Column:列】}
    for Row := 0 to QRCode.Rows - 1 do                                        // 循环行
    begin
      for Column := 0 to QRCode.Columns - 1 do                                // 循环列
      begin
        if (QRCode.IsBlack[Row, Column]) then                                 // 判断二维码次坐标是否是黑色的
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clGreen;                 // 是的就给画布画上绿色
        end
        else
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clWhite;                 // 否则就画白色
        end;
      end;
    end;
  finally
    QRCode.Free;
  end;
  PaintBox1.Repaint;                                                          // 重涂
end;

En lo que se refiere al código de seguridad, el código QR puede ser generado, y la actualización de método puede ser añadido a la OnChange caso de scEdit

procedure TForm1.scEdit1Change(Sender: TObject);
begin
  Update;
end;

Lo siguiente es guardar el contenido de PaintBox en el local y agregarlo al control de imagen

procedure SaveBmpFromPaintBox(pbox: TPaintBox; fn: string);
var
  bmp: TBitmap;
  r: TRect;
begin
  bmp := TBitmap.Create;                                                      // 创建图位
  bmp.Width := pbox.Width;                                                    // 图位宽 = 盒子宽度
  bmp.Height := pbox.Height;                                                  // 图位高 = 盒子高度
  try
    r := Rect(0, 0, pbox.Height, pbox.Width);                                 // 矩形图的 左上右下 四边
    { 如果图象一【r1】的选取区域小于图象二【r2】中的选取区域,那么图一【r1】选取区域中的图象,
    拉伸填充到图象二【r2】中的选取区域。(图象区域相同的复制不会造成图象失真,如果变大或者变小,就容易造成失真) }
    bmp.Canvas.CopyRect(r, pbox.Canvas, r); { 两个 r 在上面分别用【r1、r2】表示(从左至右),中间参数为拷贝内容 }
    bmp.SaveToFile(fn);                                                       // 文件保存地址
  finally
    FreeAndNil(bmp);
  end;
end;

procedure TForm1.scGPButton2Click(Sender: TObject);
begin
  SaveBmpFromPaintBox(paintbox1, 'd:\test.bmp');                              // 保存到本地 【需要改成jpg或png的直接改文件的后缀即可】
  Image1.Picture.LoadFromFile('d:\test.bmp');                                 // 本地图片展示到 Image 上
end;

Tengo una pregunta aquí. Quiero preguntarles por
qué aparece la siguiente advertencia. ¿Hay alguna forma de solucionar esta advertencia?

Inserte la descripción de la imagen aquí


Tenga en cuenta el código fuente


Un poco de notas grabadas durante el estudio, para que puedas leerlas más tarde.

Supongo que te gusta

Origin blog.csdn.net/qq_44111597/article/details/108359774
Recomendado
Clasificación