Recently work needs a need, that is, a service address to generate two-dimensional code, can be used to scan code sharing, online find the next method is more, I here PO call ZXing.dll Library to generate two-dimensional code method it. First, a brief introduction to the Zxing Library, Zxing Library is an open source Java class Library, can be used to generate and parse a variety of formats 1d/2d barcode;zxing follow Apache License 2.0, just tools, is no charge.
Zxing Library:http://zxingnet.codeplex.com/
Click to download , unzip the zip package after downloading:
Add the zxing to the engineering application, the following is the core code:
1 /// <summary>2 ///generate two-dimensional code images3 /// </summary>4 /// <param name= "strmessage" >a string to generate a two-dimensional code</param>5 /// <param name= "width" >Two-dimensional code image width</param>6 /// <param name= "height" >Two-dimensional code image height</param>7 /// <returns></returns>8 PrivateBitmap getqrcodebyzxingnet (String strmessage,int32 width,int32 height)9 {TenBitmap result =NULL; One Try A { -Barcodewriter Barcodewriter =NewBarcodewriter (); -Barcodewriter.format =Barcodeformat.qr_code; theBARCODEWRITER.OPTIONS.HINTS.ADD (Encodehinttype.character_set,"UTF-8"); - barCodeWriter.Options.Hints.Add (encodehinttype.error_correction, ZXing.QrCode.Internal.ErrorCorrection Level.H); -BarCodeWriter.Options.Height =height; -BarCodeWriter.Options.Width =width; +BarCodeWriter.Options.Margin =0; -ZXing.Common.BitMatrix BM =Barcodewriter.encode (strmessage); +result =Barcodewriter.write (BM); A } at Catch(Exception ex) - { - //Abnormal output - } - returnresult; -}
Look at the effect of creating a QR code:
At the same time, Zxing Library also supports the decoding of QR code images.
The core code for decoding is as follows:
/// <summary> ///decode QR Code/// </summary> /// <param name= "Barcodebitmap" >QR code image to decode</param> /// <returns>Sweep Code Results</returns> Private stringDecodeqrcode (Bitmap barcodebitmap) {Barcodereader reader=NewBarcodereader (); Reader. Options.characterset="UTF-8"; varresult =Reader. Decode (BARCODEBITMAP); return(Result = =NULL) ?NULL: Result. Text; }
Look at the effect:
Just looking for a QR code:
The sweep code results are as follows:
C # uses the ZXing.dll library to generate two-dimensional code