C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(二)


1.使用ZXint.Net生成帶logo的二維碼

/// <summary>
/// 生成帶Logo的二維碼
/// </summary>
/// <param name="text"></param>
static void Generate3(string text)
{
    //Logo 圖片
    Bitmap logo = new Bitmap(@"H:\桌面\截圖\102.jpg");
    //構造二維碼寫碼器
    MultiFormatWriter writer = new MultiFormatWriter();
    Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
    hint.Add(EncodeHintType.CHARACTER_SET,"UTF-8");
    hint.Add(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);

    //生成二維碼 
    BitMatrix bm = writer.encode(text,BarcodeFormat.QR_CODE,300,300,hint);
    BarcodeWriter barcodeWriter = new BarcodeWriter();
    Bitmap map = barcodeWriter.Write(bm);


    //獲取二維碼實際尺寸(去掉二維碼兩邊空白后的實際尺寸)
    int[] rectangle = bm.getEnclosingRectangle();

    //計算插入圖片的大小和位置
    int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
    int middleH = Math.Min((int)(rectangle[3]/3.5),logo.Height);
    int middleL = (map.Width - middleW) / 2;
    int middleT = (map.Height - middleH) / 2;

    //將img轉換成bmp格式,否則后面無法創建Graphics對象
    Bitmap bmpimg = new Bitmap(map.Width,map.Height,PixelFormat.Format32bppArgb);
    using (Graphics g=Graphics.FromImage(bmpimg))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.DrawImage(map,0,0);
    }
    //將二維碼插入圖片
    Graphics myGraphic = Graphics.FromImage(bmpimg);
    //白底
    myGraphic.FillRectangle(Brushes.White,middleL,middleT,middleW,middleH);
    myGraphic.DrawImage(logo,middleL,middleT,middleW,middleH);

    //保存成圖片
    bmpimg.Save(@"H:\桌面\截圖\generate3.png",ImageFormat.Png);
}

調用:

//生成帶圖片的二維碼
Generate3("https://www.baidu.com/");
Generate3("ionic是一個強大的混合式/hybrid HTML5移動開發框架,特點是使用標准的HTML、CSS和JavaScript,開發跨平台的應用 ,只需要幾步就可以快速創建您的Ionic應用,創建應用從這里開始");

更多:

C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(一)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM