SoFunction
Updated on 2025-03-09

Summary of the method of generating QR codes

This article summarizes the methods of generating QR codes. Share it for your reference, as follows:

Share an example of C# generating QR code, directly quote the class to generate QR code, and friends in need will refer to it.

Method 1. Directly refer to the class and generate a QR code.

Code example:

 encoder = new QRCodeEncoder();
 = QRCodeEncoder.ENCODE_MODE.BYTE;//Coding method (Note: BYTE can support Chinese, ALPHA_NUMERIC scanned all numbers) = 4;//size = 0;// Version (Note: Setting to 0 mainly prevents errors when the encoded string is too long) = QRCodeEncoder.ERROR_CORRECTION.M;
String qrdata = "QR code information";
 bp = ((), ("GB2312"));
Image image = bp;
Object oMissing = ;
 = bp;

Save QR code picture:

Code example:

SaveFileDialog sf = new SaveFileDialog();
 = "Select a save file location";
 = "Save the picture(*.jpg) |*.jpg|All files(*.*) |*.*";
//Set the default file type display order = 1;
//Save the dialog box to remember the directory you opened last time = true;
if (() == )
{
  Image im = this.;
  //Get file path  string localFilePath = ();
  if ( != "")
  {
    string fileNameExt = (("\\") + 1);//Get file name without path    // newFileName = fileNameExt+("yyyyMMdd");//Add time after the file name    string FilePath = (0, (".")); //Get file path, with file name, without suffix    string fn = ;
    (FilePath +"-"+ ("yyyyMMdd") + ".jpg");
  }
}
//Analyze QR code information// QRCodeDecoder decoder = new QRCodeDecoder();
// String decodedString = (new QRCodeBitmapImage(new Bitmap()));
//this. = decodedString;

Method 2. Reference the ZXing class library.

ZXing is an open source Java class library for parsing 1D/2D barcodes in multiple formats. The goal is to be able to decode QR encoding, Data Matrix, UPC 1D barcode. At the same time, it also provides class libraries such as cpp, ActionScript, android, iPhone, rim, j2me, j2se, jruby, C#, etc. The main function of the zxing class library is decoding, which is currently a relatively strong decoding capability among open source class library (commercial also says that, but it is indeed very worthwhile for the licensing fees of tens of thousands of class library).

Download the corresponding code in Google Code

1. Download the latest package of zxing

Go to zxing's homepage:/p/zxing/

Find the CSharp folder in it, open and compile it in vs. Copy and paste the debug under obj into the bin file directory in your project.
Right-click to add project reference. References to the project and you can use it where you need it.

There are two UTF-8 problems in the source code, which will cause garbled Chinese (modified before compilation.dll)

One: in the class

internal const  DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

Here, change ISO-8859-1 to UTF-8

Second: Members of the class

private const UTF8 = "UTF8";

UTF8 should be changed to UTF-8

Code example:

using ;
using ;
using ;
using ByteMatrix = ;
using EAN13Writer = .EAN13Writer;
using EAN8Writer = .EAN8Writer;
using MultiFormatWriter = ;

method:

string content = "QR code information";
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
 = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
 = "Save the picture(*.png) |*.png|All files(*.*) |*.*";
 = "*.png|*.png";
 = true;
if (() == )
{
if ( != "")
{
  writeToFile(byteMatrix, , );
}
}

Analyze the QR code:

Code example:

if (this.() != )
{
return;
}
Image img = (this.);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch ( ioe)
{
(());
return;
}
if (bmap == null)
{
("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, , );
 bitmap1 = new (new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
(());
return;
}
();
public static void writeToFile(ByteMatrix matrix,  format, string file)
{
    Bitmap bmap = toBitmap(matrix);
    (file, format);
}
public static Bitmap toBitmap(ByteMatrix matrix)
{
    int width = ;
    int height = ;
    Bitmap bmap = new Bitmap(width, height, .Format32bppArgb);
    for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
  (x, y, matrix.get_Renamed(x, y) != -1 ? ("0xFF000000") : ("0xFFFFFFFF"));
}
}
return bmap;
}

PS: Here we recommend a very powerful QR code online generation tool for everyone to use for free:

Online QR code generation tool (enhanced version):
http://tools./transcoding/jb51qrcode

For more information about relevant content, please check out the topic of this site:Summary of string operation techniques》、《Summary of operating XML skills》、《Summary of file operation skills》、《Ajax tips summary topic"and"Summary of cache operation skills》。

I hope this article will be helpful to everyone's programming.