Summary of ASP. NET method for generating two dimensional code

  • 2021-07-24 10:50:04
  • OfStack

This paper summarizes the method of generating 2-dimensional codes by ASP. NET. Share it for your reference, as follows:

Share a case of c # generated 2-D code code, directly reference ThoughtWorks. QRCode. dll class generated 2-D code, friends in need of reference.

Method 1. Generate 2-D code by directly referring to ThoughtWorks. QRCode. dll classes.

Code example:


ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;// Coding method ( Note: BYTE Can support Chinese, ALPHA_NUMERIC All the scans are numbers )
encoder.QRCodeScale = 4;// Size 
encoder.QRCodeVersion = 0;// Version ( Note: Set to 0 Mainly to prevent errors when the encoded string is too long )
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
String qrdata = "2 Dimension code information ";
System.Drawing.Bitmap bp = encoder.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
Image image = bp;
Object oMissing = System.Reflection.Missing.Value;
pictureBox1.Image = bp;

Save 2D code picture:

Code example:


SaveFileDialog sf = new SaveFileDialog();
sf.Title = " Select where to save the file ";
sf.Filter = " Save Picture (*.jpg) |*.jpg| All files (*.*) |*.*";
// Set the default file type display order 
sf.FilterIndex = 1;
// Does the Save dialog remember the last directory opened 
sf.RestoreDirectory = true;
if (sf.ShowDialog() == DialogResult.OK)
{
  Image im = this.pictureBox1.Image;
  // Get the file path 
  string localFilePath = sf.FileName.ToString();
  if (sf.FileName != "")
  {
    string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);// Get the file name without path 
    // newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd") ;// Add time to the file name 
    string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(".")); // Get the file path with the file name , Without suffix 
    string fn = sf.FileName;
    pictureBox1.Image.Save(FilePath +"-"+ DateTime.Now.ToString("yyyyMMdd") + ".jpg");
  }
}
// Analyse 2 Dimension code information 
// QRCodeDecoder decoder = new QRCodeDecoder();
// String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));
//this.label3.Text = 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 encoded, Data Matrix, UPC 1D barcode. At the same time, it also provides class libraries in cpp, ActionScript, android, iPhone, rim, j2me, j2se, jruby, C #, etc. The function of zxing class library is mainly decoding, which is relatively strong in decoding ability among open source class libraries at present (another commercial one, but it is really worth the license fee of thousands of class libraries at every turn).

Download the corresponding code at Google code

1. Download the latest package for zxing

Go to the home page of zxing: http://code.google.com/p/zxing/

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

There are two problems with UTF-8 in the source code, which will lead to garbled Chinese (modified before compiling. dll)

Of its 1: com. google. zxing. qrcode. encoder. encoder


internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

Here, replace ISO-8859-1 with UTF-8

Its 2: Members of the com. google. zxing. qrcode. decoder. DecodedBitStreamParser class

private const System.String UTF8 = "UTF8";

Replace UTF8 with UTF-8

Code example:


using com.google.zxing.qrcode;
using com.google.zxing;
using com.google.zxing.common;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
using EAN13Writer = com.google.zxing.oned.EAN13Writer;
using EAN8Writer = com.google.zxing.oned.EAN8Writer;
using MultiFormatWriter = com.google.zxing.MultiFormatWriter;

Methods:


string content = "2 Dimension code information ";
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = " Save Picture (*.png) |*.png| All files (*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
  writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
}
}

Parsing 2-D code:

Code example:


if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Image img = Image.FromFile(this.openFileDialog1.FileName);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}
MessageBox.Show(result.Text);
public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
    Bitmap bmap = toBitmap(matrix);
    bmap.Save(file, format);
}
public static Bitmap toBitmap(ByteMatrix matrix)
{
    int width = matrix.Width;
    int height = matrix.Height;
    Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
  bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bmap;
}

PS: Here, we recommend a very powerful online generation tool for 2D codes, which is free for everyone to use:

Online 2-D code generation tool (enhanced version):
http://tools.ofstack.com/transcoding/jb51qrcode

For more readers interested in asp. net, please check out the topics on this site: "asp. net String Operation Skills Summary", "asp. net Operation XML Skills Summary", "asp. net File Operation Skills Summary", "asp. net ajax Skills Summary" and "asp. net Cache Operation Skills Summary".

I hope this article is helpful to everyone's asp. net programming.


Related articles: