DelphiZXingQRCode生成二维码不可解码及方案实现受阻咨询
Hey there, I’ve run into similar headaches with DelphiZXingQRCode before, so let’s break down how to fix both the occasional decoding failures and the specific high-error-correction-level problem you’re facing.
First: Fixing the High Error Correction Level Decoding Bug
The root issue here is often that the original DelphiZXingQRCode implementation mishandles the bit values for error correction levels higher than Low. Here’s the complete, missing code you need to get this working:
Step 1: Update the TErrorCorrectionLevel Class
The original class likely lacks the fOrdinal field and proper GetBits method to map levels to their correct ZXing bit values. Replace or extend the class like this:
type TErrorCorrectionLevel = class private fOrdinal: Integer; fBits: Integer; public constructor Create(AOrdinal, ABits: Integer); function GetBits: Integer; class property Low: TErrorCorrectionLevel read GetLow; class property Medium: TErrorCorrectionLevel read GetMedium; class property Quartile: TErrorCorrectionLevel read GetQuartile; class property High: TErrorCorrectionLevel read GetHigh; end; var _Low, _Medium, _Quartile, _High: TErrorCorrectionLevel; constructor TErrorCorrectionLevel.Create(AOrdinal, ABits: Integer); begin inherited Create; fOrdinal := AOrdinal; fBits := ABits; end; function TErrorCorrectionLevel.GetBits: Integer; begin Result := fBits; end; class function TErrorCorrectionLevel.GetLow: TErrorCorrectionLevel; begin if not Assigned(_Low) then _Low := TErrorCorrectionLevel.Create(0, 0); // Low = 0 bits Result := _Low; end; class function TErrorCorrectionLevel.GetMedium: TErrorCorrectionLevel; begin if not Assigned(_Medium) then _Medium := TErrorCorrectionLevel.Create(1, 1); // Medium = 1 bit Result := _Medium; end; class function TErrorCorrectionLevel.GetQuartile: TErrorCorrectionLevel; begin if not Assigned(_Quartile) then _Quartile := TErrorCorrectionLevel.Create(2, 3); // Quartile = 3 bits Result := _Quartile; end; class function TErrorCorrectionLevel.GetHigh: TErrorCorrectionLevel; begin if not Assigned(_High) then _High := TErrorCorrectionLevel.Create(3, 2); // High = 2 bits Result := _High; end;
Step 2: Implement the GenerateQRCode Method
This method ties the corrected error correction levels into the ZXing generation logic. Here’s a complete implementation:
function GenerateQRCode(const AText: string; AErrorCorrection: TErrorCorrectionLevel; const AWidth, AHeight: Integer): TBitmap; var Writer: TQRCodeWriter; Matrix: TBitMatrix; Renderer: TBitmapRenderer; EncodedText: TBytes; begin // Ensure text is UTF-8 encoded (critical for non-ASCII characters) EncodedText := TEncoding.UTF8.GetBytes(AText); Writer := TQRCodeWriter.Create; try // Pass the corrected bit value for error correction Writer.ErrorCorrection := AErrorCorrection.GetBits; // Encode the UTF-8 text to avoid encoding mismatches Matrix := Writer.Encode(TEncoding.UTF8.GetString(EncodedText), TBarcodeFormat.QR_CODE, AWidth, AHeight); try Renderer := TBitmapRenderer.Create; try Result := Renderer.Render(Matrix, TBarcodeFormat.QR_CODE, '', nil); finally Renderer.Free; end; finally Matrix.Free; end; finally Writer.Free; end; end;
Additional Fixes for General Decoding Failures
If you’re still seeing occasional decoding issues even after fixing the error correction level, check these common culprits:
- Size Matters: Higher error correction levels require more space. If your QR code is too small for the text length + correction level, ZXing may generate a malformed code. Calculate the minimum required size based on your input text and correction level (ZXing has built-in logic for this, but ensuring you set a sufficiently large width/height helps).
- Encoding Consistency: Always use UTF-8 for text encoding when generating and decoding. Mismatched encodings (e.g., ANSI vs UTF-8) can corrupt the data.
- Update the Library: Make sure you’re using the latest version of DelphiZXingQRCode—older versions may have unpatched bugs related to error correction or edge-case text inputs.
- Test with ZXing Decoders: If third-party scanners fail, test with the official ZXing decoder (either the Delphi port or the Java/Android version). If ZXing can decode it but other tools can’t, the issue is with the scanner, not your generated code.
内容的提问来源于stack exchange,提问作者Peter Meier

