if (Bitmap.Map(TMapAccess.Write, QrCodeBmpData)) then begin try 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 QrCodeBmpData.SetPixel(Column, Row, TAlphaColorRec.Black); end else begin QrCodeBmpData.SetPixel(Column, Row, TAlphaColorRec.White); end; end; end; finally Bitmap.Unmap(QrCodeBmpData); end; end;
finally QRCode.Free; end; end;
procedure TForm2.Button1Click(Sender: TObject); begin PaintQrCode(Image1.Bitmap,Memo1.Text); end;
而在VCL模式下,二维码就非常清楚,如下所示: procedure TForm2.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;
procedure TForm2.Button1Click(Sender: TObject); begin CreateQRCode(image1,Edit1.Text); end;
procedure TForm2.CreateQRCode(imgQRCode:TImage;QRText:string); const downsizeQuality: Integer = 2; // bigger value, better quality, slower rendering var QRCode: TDelphiZXingQRCode; Row, Column: Integer; pixelColor : TAlphaColor; vBitMapData : TBitmapData; pixelCount, y, x: Integer; columnPixel, rowPixel: Integer; function GetPixelCount(AWidth, AHeight: Single): Integer; begin if QRCode.Rows > 0 then Result := Trunc(Min(AWidth, AHeight)) div QRCode.Rows else Result := 0; end; begin QRCode := TDelphiZXingQRCode.Create; try QRCode.Data := QRText; QRCode.Encoding := TQRCodeEncoding.qrAuto; QRCode.QuietZone := StrToIntDef('4', 4); pixelCount := GetPixelCount(imgQRCode.Width, imgQRCode.Height); case imgQRCode.WrapMode of TImageWrapMode.Original,TImageWrapMode.Tile,TImageWrapMode.Center: begin if pixelCount > 0 then imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount, QRCode.Rows * pixelCount); end; TImageWrapMode.Fit: begin if pixelCount > 0 then begin imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount * downsizeQuality, QRCode.Rows * pixelCount * downsizeQuality); pixelCount := pixelCount * downsizeQuality; end; end; TImageWrapMode.Stretch: raise Exception.Create('Not a good idea to stretch the QR Code'); end;
try imgQRCode.Bitmap.Canvas.Clear(TAlphaColors.White); if pixelCount > 0 then begin if imgQRCode.Bitmap.Map(TMapAccess.Write, vBitMapData) then begin try for Row := 0 to QRCode.Rows - 1 do begin for Column := 0 to QRCode.Columns - 1 do begin if (QRCode.IsBlack[Row, Column]) then pixelColor := TAlphaColors.Black else pixelColor := TAlphaColors.White; columnPixel := Column * pixelCount; rowPixel := Row * pixelCount; for x := 0 to pixelCount - 1 do for y := 0 to pixelCount - 1 do vBitMapData.SetPixel(columnPixel + x, rowPixel + y, pixelColor); end; end; finally imgQRCode.Bitmap.Unmap(vBitMapData); end; end; end; finally
case imgQRCode.WrapMode of TImageWrapMode.Original,TImageWrapMode.Tile,TImageWrapMode.Center: begin if pixelCount > 0 then imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount, QRCode.Rows * pixelCount); end; TImageWrapMode.Fit: begin if pixelCount > 0 then begin imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount * downsizeQuality, QRCode.Rows * pixelCount * downsizeQuality); pixelCount := pixelCount * downsizeQuality; end; end; TImageWrapMode.Stretch: raise Exception.Create('Not a good idea to stretch the QR Code'); end;
if pixelCount > 0 then begin if imgQRCode.Bitmap.Map(TMapAccess.Write, vBitMapData) then begin try for Row := 0 to QRCode.Rows - 1 do begin for Column := 0 to QRCode.Columns - 1 do begin
for x := 0 to pixelCount - 1 do for y := 0 to pixelCount - 1 do vBitMapData.SetPixel(columnPixel + x,rowPixel + y, pixelColor); end; end; finally imgQRCode.Bitmap.Unmap(vBitMapData); end; end; end; finally