C # generates two-dimensional code, trim border

Source: Internet
Author: User

The QR code generated with Google Zxing has a white border and is displayed on the report (using Crystal Report), which makes the QR code too small to scan for recognition due to limited space.

By trimming out the white border, you can maximize the display of the QR code in a limited space.

usingcom.google.zxing;usingCom.google.zxing.common;usingCom.google.zxing.qrcode.decoder;usingJava.awt.image;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacesunway.barcode{/// <summary>    ///     /// </summary>     Public Static classBarcodehelper {/// <summary>        ///         /// </summary>        /// <param name= "info" ></param>         /// <returns></returns>         Public Static stringCreatebarcode (stringinfo) {            stringFilePath =string.             Empty; FilePath= Guid.NewGuid (). ToString () +". PNG"; //            Try{multiformatwriter writer=NewMultiformatwriter (); Hashtable hints=NewHashtable (); Hints. ADD (Encodehinttype.character_set,"Utf-8");//CodingErrorcorrectionlevel level =ErrorCorrectionLevel.H; Hints. ADD (encodehinttype.error_correction, level); //Tolerance Rate//hints.  ADD (encodehinttype.margin, 0); //Two-dimensional code border width, here the document says set 0-4,Bytematrix Bytematrix = Writer.encode (info, Barcodeformat.qr_code, -, -, hints); Bitmap Bitmap=Tobitmap (Bytematrix); //bitmap.            Save (FilePath); }            Catch(Exception) {FilePath=string.                Empty; Throw; }            finally            {            }             //            returnFilePath; }        /// <summary>        ///Convert to Bitmap/// </summary>        /// <param name= "Matrix" ></param>        /// <returns></returns>         Public StaticBitmap Tobitmap (Bytematrix matrix) {intwidth =Matrix.            Width; intHeight =Matrix.            Height; //32-bit ARGB formatBitmap Bmap =NewBitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Color Colorblack= Colortranslator.fromhtml ("0xff000000");//BlackColor Colorwhite = colortranslator.fromhtml ("0xFFFFFFFF");//White//The two-dimensional matrix is converted into a one-dimensional pixel group, which is always lined up horizontally.//color[] pixels = new int[width * height];            BOOLIsfirstblackpoint =false; intStartX =0; intStarty =0; //Looping content matrix, writing white, black dots             for(intx =0; x < width; X + +)            {                 for(inty =0; Y < height; y++)                {                    if(Matrix.get_renamed (x, y)! =-1)//return white point/black point                    {                        if(!isfirstblackpoint) {Isfirstblackpoint=true; StartX= x;//The QR code comes with a white border, the upper-left corner of the border is a black point, and the top-left corner is recorded .Starty =y; }} bmap. SetPixel (x, Y, matrix.get_renamed (x, y)!= -1?colorblack:colorwhite); }            }            #regionJudge and InterceptintPadding_size_min =2; intX1 = StartX-padding_size_min; intY1 = Starty-padding_size_min; if(X1 <0|| Y1 <0)            {                returnBmap; }            Else            {                intW1 = width-x1 *2; intH1 = Height-y1 *2; Bitmap BITMAPQR= Cutimage (Bmap,NewRectangle (x1, y1, W1, H1)); returnBITMAPQR; }            #endregion        }                /// <summary>        ///capture a picture, specify the Intercept area (start position and length/width)/// </summary>        /// <param name= "img" ></param>        /// <param name= "rect" ></param>        /// <returns></returns>        Private StaticBitmap cutimage (Image img, Rectangle rect) {Bitmap B=NewBitmap (rect. Width, Rect.            Height, Pixelformat.format32bppargb); Graphics g=Graphics.fromimage (b); G.drawimage (IMG,0,0, Rect, graphicsunit.pixel);            G.dispose (); returnb; }    }}

C # generates two-dimensional code, trim border

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.