Browse Source

some refactorings.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
a1a6cf5caf

+ 9 - 5
QRCodeGenLib.Demo/FreePascal.Demo/QrCodeGeneratorDemo.lpi

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
 <CONFIG>
 <CONFIG>
   <ProjectOptions>
   <ProjectOptions>
-    <Version Value="11"/>
+    <Version Value="10"/>
     <PathDelim Value="\"/>
     <PathDelim Value="\"/>
     <General>
     <General>
       <Flags>
       <Flags>
@@ -22,13 +22,17 @@
       <Version Value="2"/>
       <Version Value="2"/>
     </PublishOptions>
     </PublishOptions>
     <RunParams>
     <RunParams>
-      <FormatVersion Value="2"/>
-      <Modes Count="0"/>
+      <local>
+        <FormatVersion Value="1"/>
+      </local>
     </RunParams>
     </RunParams>
-    <RequiredPackages Count="1">
+    <RequiredPackages Count="2">
       <Item1>
       <Item1>
-        <PackageName Value="QRCodeGenLib4PascalPackage"/>
+        <PackageName Value="LCL"/>
       </Item1>
       </Item1>
+      <Item2>
+        <PackageName Value="QRCodeGenLib4PascalPackage"/>
+      </Item2>
     </RequiredPackages>
     </RequiredPackages>
     <Units Count="2">
     <Units Count="2">
       <Unit0>
       <Unit0>

+ 11 - 4
QRCodeGenLib.Demo/src/uQrCodeGeneratorDemo.pas

@@ -2,6 +2,11 @@
 
 
 {$IFNDEF FPC}
 {$IFNDEF FPC}
 {$DEFINE DELPHI}
 {$DEFINE DELPHI}
+{$IF DECLARED(FireMonkeyVersion)}
+{$DEFINE FMX}
+{$ELSE}
+{$DEFINE VCL}
+{$ENDIF}
 {$ELSE}
 {$ELSE}
 {$MODE DELPHI}
 {$MODE DELPHI}
 {$ENDIF FPC}
 {$ENDIF FPC}
@@ -10,11 +15,13 @@ interface
 
 
 uses
 uses
   SysUtils,
   SysUtils,
+{$IF DEFINED(VCL)}
+  Vcl.Graphics,
+  Vcl.Imaging.jpeg, // for VCL JPEG Support
+  Vcl.Imaging.pngimage, // for VCL PNG Support
+{$ELSEIF DEFINED(LCL)}
   Graphics,
   Graphics,
-{$IFDEF DELPHI}
-  Imaging.jpeg, // for Delphi JPEG Support
-  Imaging.pngimage, // for Delphi PNG Support
-{$ENDIF DELPHI}
+{$IFEND}
   QlpIQrCode,
   QlpIQrCode,
   QlpQrCode,
   QlpQrCode,
   QlpIQrSegment,
   QlpIQrSegment,

+ 5 - 0
QRCodeGenLib/src/Include/QRCodeGenLib.inc

@@ -125,6 +125,11 @@
 {$WARN COMBINING_SIGNED_UNSIGNED64 OFF}
 {$WARN COMBINING_SIGNED_UNSIGNED64 OFF}
 {$ENDIF}
 {$ENDIF}
 
 
+{$IF DECLARED(FireMonkeyVersion)}
+{$DEFINE FMX}
+{$ELSE}
+{$DEFINE VCL}
+{$IFEND}
 
 
 {$ENDIF DELPHI}
 {$ENDIF DELPHI}
 
 

+ 6 - 4
QRCodeGenLib/src/Interfaces/QlpIQrCode.pas

@@ -5,11 +5,13 @@ unit QlpIQrCode;
 interface
 interface
 
 
 uses
 uses
+{$IF DEFINED(VCL)}
+  Vcl.Graphics,
+  Vcl.Imaging.jpeg, // for VCL JPEG Support
+  Vcl.Imaging.pngimage, // for VCL PNG Support
+{$ELSEIF DEFINED(LCL)}
   Graphics,
   Graphics,
-{$IFDEF DELPHI}
-  Imaging.jpeg, // for Delphi JPEG Support
-  Imaging.pngimage, // for Delphi PNG Support
-{$ENDIF DELPHI}
+{$IFEND}
   QlpQRCodeGenLibTypes;
   QlpQRCodeGenLibTypes;
 
 
 type
 type

+ 1 - 1
QRCodeGenLib/src/Packages/FPC/QRCodeGenLib4PascalPackage.lpk

@@ -22,7 +22,7 @@
     <Description Value="QRCodeGenLib4Pascal is a Delphi/FPC compatible library that provides an easy to use interface for generating QR Codes.
     <Description Value="QRCodeGenLib4Pascal is a Delphi/FPC compatible library that provides an easy to use interface for generating QR Codes.
 "/>
 "/>
     <License Value="MIT License"/>
     <License Value="MIT License"/>
-    <Version Major="1" Minor="1"/>
+    <Version Major="1" Minor="2"/>
     <Files Count="18">
     <Files Count="18">
       <Item1>
       <Item1>
         <Filename Value="..\..\QRCodeGen\QlpBitBuffer.pas"/>
         <Filename Value="..\..\QRCodeGen\QlpBitBuffer.pas"/>

+ 85 - 46
QRCodeGenLib/src/QRCodeGen/QlpQrCode.pas

@@ -8,13 +8,14 @@ uses
   Math,
   Math,
   Classes,
   Classes,
   SysUtils,
   SysUtils,
+{$IF DEFINED(VCL)}
+  Vcl.Graphics,
+  Vcl.Imaging.jpeg, // for VCL JPEG Support
+  Vcl.Imaging.pngimage, // for VCL PNG Support
+{$ELSEIF DEFINED(LCL)}
   Graphics,
   Graphics,
-{$IFDEF DELPHI}
-  Imaging.jpeg, // for Delphi JPEG Support
-  Imaging.pngimage, // for Delphi PNG Support
-{$ELSE}
-  Interfaces, // Added so that Lazarus/FPC will Initialize the WidgetSet
-{$ENDIF DELPHI}
+  Interfaces, // Added so that the LCL will Initialize the WidgetSet
+{$IFEND}
   QlpIQrCode,
   QlpIQrCode,
   QlpIQrTemplate,
   QlpIQrTemplate,
   QlpQrTemplate,
   QlpQrTemplate,
@@ -187,6 +188,12 @@ type
 
 
     procedure ValidateImageDimensions(AScale, ABorder: Int32);
     procedure ValidateImageDimensions(AScale, ABorder: Int32);
 
 
+{$IFDEF LCL}
+    function ToBmpImageInternalLCL(AScale, ABorder: Int32): TBitmap;
+{$ENDIF LCL}
+{$IFDEF VCL}
+    function ToBmpImageInternalVCL(AScale, ABorder: Int32): TBitmap;
+{$ENDIF VCL}
     // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
     // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
     // QR Code of the given version number and error correction level, with remainder bits discarded.
     // QR Code of the given version number and error correction level, with remainder bits discarded.
     // This stateless pure function could be implemented as a (40*4)-cell lookup table.
     // This stateless pure function could be implemented as a (40*4)-cell lookup table.
@@ -299,7 +306,6 @@ type
     property ForegroundColor: TColor read GetForegroundColor
     property ForegroundColor: TColor read GetForegroundColor
       write SetForegroundColor;
       write SetForegroundColor;
 
 
-{$IFNDEF _FIXINSIGHT_}
     /// <summary>
     /// <summary>
     /// Returns a bitmap image depicting this QR Code, with the specified
     /// Returns a bitmap image depicting this QR Code, with the specified
     /// module scale and border modules. For example, ToBmpImage(scale=10,
     /// module scale and border modules. For example, ToBmpImage(scale=10,
@@ -327,7 +333,7 @@ type
     /// object.</b>
     /// object.</b>
     /// </remarks>
     /// </remarks>
     function ToBmpImage(AScale, ABorder: Int32): TBitmap;
     function ToBmpImage(AScale, ABorder: Int32): TBitmap;
-{$ENDIF}
+
     /// <summary>
     /// <summary>
     /// Returns a jpeg image depicting this QR Code, with the specified
     /// Returns a jpeg image depicting this QR Code, with the specified
     /// module scale and border modules. For example, ToBmpImage(scale=10,
     /// module scale and border modules. For example, ToBmpImage(scale=10,
@@ -921,43 +927,25 @@ begin
   end;
   end;
 end;
 end;
 
 
-{$IFNDEF _FIXINSIGHT_}
-
-function TQrCode.ToBmpImage(AScale, ABorder: Int32): TBitmap;
-{$IFNDEF FPC}
-type
-  TRGBTriple = record
-    B, G, R: Byte;
-  end;
+{$IFDEF LCL}
 
 
-type
-  PRGBTripleArray = ^TRGBTripleArray;
-  TRGBTripleArray = array [0 .. MaxInt div SizeOf(TRGBTriple) - 1]
-    of TRGBTriple;
-{$ENDIF FPC}
+function TQrCode.ToBmpImageInternalLCL(AScale, ABorder: Int32): TBitmap;
 var
 var
   LColumn, LRow: Int32;
   LColumn, LRow: Int32;
   LDoColor: Boolean;
   LDoColor: Boolean;
   LBrushColor: TColor;
   LBrushColor: TColor;
   LForegroundColor, LBackgroundColor: TColor;
   LForegroundColor, LBackgroundColor: TColor;
-  LScanLine: {$IFNDEF FPC} PRGBTripleArray {$ELSE} PByte {$ENDIF FPC};
-{$IFDEF FPC}
+  LScanLine: PByte;
   LBytesPerPixel, LRedOffset, LGreenOffset, LBlueOffset: Byte;
   LBytesPerPixel, LRedOffset, LGreenOffset, LBlueOffset: Byte;
-{$ENDIF FPC}
 begin
 begin
-  ValidateImageDimensions(AScale, ABorder);
-
   Result := TBitmap.Create;
   Result := TBitmap.Create;
-{$IFNDEF FPC}
-  Result.PixelFormat := pf24bit;
-{$ENDIF FPC}
+
   Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
   Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
     * AScale);
     * AScale);
 
 
   LForegroundColor := FForegroundColor;
   LForegroundColor := FForegroundColor;
   LBackgroundColor := FBackgroundColor;
   LBackgroundColor := FBackgroundColor;
 
 
-{$IFDEF FPC}
   LBytesPerPixel := Result.RawImage.Description.BitsPerPixel shr 3;
   LBytesPerPixel := Result.RawImage.Description.BitsPerPixel shr 3;
   LRedOffset := Result.RawImage.Description.RedShift shr 3;
   LRedOffset := Result.RawImage.Description.RedShift shr 3;
   LGreenOffset := Result.RawImage.Description.GreenShift shr 3;
   LGreenOffset := Result.RawImage.Description.GreenShift shr 3;
@@ -967,16 +955,11 @@ begin
   LGreenOffset := LBytesPerPixel - 1 - LGreenOffset;
   LGreenOffset := LBytesPerPixel - 1 - LGreenOffset;
   LBlueOffset := LBytesPerPixel - 1 - LBlueOffset;
   LBlueOffset := LBytesPerPixel - 1 - LBlueOffset;
 {$ENDIF ENDIAN_LITTLE}
 {$ENDIF ENDIAN_LITTLE}
-{$ENDIF FPC}
   try
   try
-{$IFDEF FPC}
-    // update locking for speedup. only available in FPC
     Result.BeginUpdate(True);
     Result.BeginUpdate(True);
-{$ENDIF FPC}
     for LColumn := 0 to System.Pred(Result.Height) do
     for LColumn := 0 to System.Pred(Result.Height) do
     begin
     begin
-      LScanLine := {$IFDEF FPC} Result.RawImage.GetLineStart(LColumn)
-{$ELSE} Result.ScanLine[LColumn] {$ENDIF FPC};
+      LScanLine := Result.RawImage.GetLineStart(LColumn);
       for LRow := 0 to System.Pred(Result.Width) do
       for LRow := 0 to System.Pred(Result.Width) do
       begin
       begin
         LDoColor := GetModule((LRow div AScale) - ABorder,
         LDoColor := GetModule((LRow div AScale) - ABorder,
@@ -991,26 +974,82 @@ begin
         end;
         end;
         // Slow !!!
         // Slow !!!
         // Result.Canvas.Pixels[LRow, LColumn] := LBrushColor;
         // Result.Canvas.Pixels[LRow, LColumn] := LBrushColor;
-{$IFDEF FPC}
         (LScanLine + LBlueOffset)^ := GetBValue(LBrushColor);
         (LScanLine + LBlueOffset)^ := GetBValue(LBrushColor);
         (LScanLine + LGreenOffset)^ := GetGValue(LBrushColor);
         (LScanLine + LGreenOffset)^ := GetGValue(LBrushColor);
         (LScanLine + LRedOffset)^ := GetRValue(LBrushColor);
         (LScanLine + LRedOffset)^ := GetRValue(LBrushColor);
         System.Inc(LScanLine, LBytesPerPixel);
         System.Inc(LScanLine, LBytesPerPixel);
-{$ELSE}
-        LScanLine^[LRow].B := GetBValue(LBrushColor);
-        LScanLine^[LRow].G := GetGValue(LBrushColor);
-        LScanLine^[LRow].R := GetRValue(LBrushColor);
-{$ENDIF FPC}
       end;
       end;
     end;
     end;
   finally
   finally
-{$IFDEF FPC}
-    // update locking for speedup. only available in FPC
     Result.EndUpdate(false);
     Result.EndUpdate(false);
-{$ENDIF FPC}
   end;
   end;
 end;
 end;
-{$ENDIF}
+{$ENDIF LCL}
+{$IFDEF VCL}
+
+function TQrCode.ToBmpImageInternalVCL(AScale, ABorder: Int32): TBitmap;
+type
+  TRGBTriple = record
+    B, G, R: Byte;
+  end;
+
+type
+  PRGBTripleArray = ^TRGBTripleArray;
+  TRGBTripleArray = array [0 .. MaxInt div SizeOf(TRGBTriple) - 1]
+    of TRGBTriple;
+var
+  LColumn, LRow: Int32;
+  LDoColor: Boolean;
+  LBrushColor: TColor;
+  LForegroundColor, LBackgroundColor: TColor;
+  LScanLine: PRGBTripleArray;
+begin
+  Result := TBitmap.Create;
+  Result.PixelFormat := pf24bit;
+
+  Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
+    * AScale);
+
+  LForegroundColor := FForegroundColor;
+  LBackgroundColor := FBackgroundColor;
+
+  for LColumn := 0 to System.Pred(Result.Height) do
+  begin
+    LScanLine := Result.ScanLine[LColumn];
+    for LRow := 0 to System.Pred(Result.Width) do
+    begin
+      LDoColor := GetModule((LRow div AScale) - ABorder,
+        (LColumn div AScale) - ABorder);
+      if LDoColor then
+      begin
+        LBrushColor := LForegroundColor;
+      end
+      else
+      begin
+        LBrushColor := LBackgroundColor;
+      end;
+      // Slow !!!
+      // Result.Canvas.Pixels[LRow, LColumn] := LBrushColor;
+      LScanLine^[LRow].B := GetBValue(LBrushColor);
+      LScanLine^[LRow].G := GetGValue(LBrushColor);
+      LScanLine^[LRow].R := GetRValue(LBrushColor);
+    end;
+
+  end;
+end;
+{$ENDIF VCL}
+
+function TQrCode.ToBmpImage(AScale, ABorder: Int32): TBitmap;
+begin
+  ValidateImageDimensions(AScale, ABorder);
+{$IF DEFINED(LCL)}
+  Result := ToBmpImageInternalLCL(AScale, ABorder);
+{$ELSEIF DEFINED(VCL)}
+  Result := ToBmpImageInternalVCL(AScale, ABorder);
+{$ELSE}
+{$MESSAGE ERROR 'This UI Framework is not supported at the moment.'}
+{$IFEND}
+end;
 
 
 function TQrCode.ToJpegImage(AScale, ABorder: Int32): TJPEGImage;
 function TQrCode.ToJpegImage(AScale, ABorder: Int32): TJPEGImage;
 var
 var

+ 8 - 0
README.md

@@ -27,6 +27,14 @@ Manual parameters:
     FreePascal 3.0.0 and Above.
     FreePascal 3.0.0 and Above.
     
     
     Delphi XE3 and Above.
     Delphi XE3 and Above.
+    
+**Supported Visual Frameworks**
+ 
+    LCL Framework (Lazarus)
+    
+    VCL Framework (Delphi)
+    
+    FMX (FireMonkey is not yet supported but pull requests are welcome).
 
 
 **Installing the Library.**
 **Installing the Library.**