• 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应用,创建应用从这里开始");
  • 相关阅读:
    字符串与字典常用命令
    Python学习之路:字符串常用操作
    Python学习之路:购物车实例
    面试题2017
    c#语法学习
    结构化设计模式-桥接模式
    结构型设计模式-适配器模式
    .Net Cache
    设计模式的六大原则
    uml类图关系
  • 原文地址:https://www.cnblogs.com/zxtceq/p/10121389.html
Copyright © 2020-2023  润新知