工作中使用到了,就随笔记下了。希望可以帮助有需要的同学们。

/// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap Generate1(string text, int width, int height)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                Margin = 1//设置二维码的边距,单位不是固定像素
            };

writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

/// <summary>
        /// 生成一维条形码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap Generate2(string text, int width, int height)
        {
            BarcodeWriter writer = new BarcodeWriter();
            //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_39;
            EncodingOptions options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

/// <summary>
        /// 生成带Logo的二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static Bitmap Generate3(string text, int width, int height)
        {
            //Logo 图片
            string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png";
            Bitmap logo = new Bitmap(logoPath);
            //构造二维码写码器
            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);
            //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边

//生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 30, height + 30, hint);
            bm = deleteWhite(bm);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);

//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
            int[] rectangle = bm.getEnclosingRectangle();

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

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, width, height);
                //白底将二维码插入图片
                g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
                g.DrawImage(logo, middleL, middleT, middleW, middleH);
            }
            return bmpimg;
        }

/// <summary>
        /// 删除默认对应的空白
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        private static BitMatrix deleteWhite(BitMatrix matrix)
        {
            int[] rec = matrix.getEnclosingRectangle();
            int resWidth = rec[2] + 1;
            int resHeight = rec[3] + 1;

BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
            resMatrix.clear();
            for (int i = 0; i < resWidth; i++)
            {
                for (int j = 0; j < resHeight; j++)
                {
                    if (matrix[i + rec[0], j + rec[1]])
                        resMatrix[i, j] = true;
                }
            }
            return resMatrix;
        }

.NET ZXING 生成带logo的二维码和普通二维码及条型码相关推荐

  1. 使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小

    使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小  * 可选是否带logo,可选是否保存二维码图片:结果返回base64编码的图片数据字符串  * 页面显示:< ...

  2. java关于Zxing 生成带Logo 二维码图片失真问题

    java关于Zxing 生成带Logo 二维码图片失真问题 问题点 logo本身是高清图片,但是Zxing生成的二维码中,logo像素失真,感觉被严重压缩一样. 排查问题 是Graphics2D 绘制 ...

  3. zxing生成带logo的二维码

    倒Zxing依赖 implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.1' 代码段 import android.graphics.Bitma ...

  4. Android Studio 生成二维码、生成带logo的二维码

    1.生成二维码: 2.生成logo的二维码: 一.引入依赖 首先在libs文件目录下放进jar包zxing.jar,要下载zxing.jar就点击链接:下载zxing.jar(记得点击"Cd ...

  5. jQuery生成带Logo的二维码

    用zxing生成二维码并解析:https://blog.csdn.net/qq_41879385/article/details/81320723 用QR Code生成和解析二维码文章地址:https ...

  6. 实现扫描二维码和生成带logo的二维码

    欢迎来到风的博客 今天讲的是如何引用google的zxing库实现扫描二维码和生成带logo的二维码,源码库可以从github上下载[https://github.com/zxing/zxing];在 ...

  7. iOS 生成带 logo 的二维码,区域截屏保存至相册(小功能二连发 (一))

    原文链接:http://www.jianshu.com/p/36e9f012ef39 生成带 logo 的二维码 区域截屏相关 -- 由3033分享 开篇 最近项目需要搞了几个相对独立的小功能,今天有 ...

  8. asp.net 后台生成二维码及生成带logo的二维码

    asp.net 后台生成二维码及生成带logo的二维码,此处将生成二维码和带Logo的二维码写在一起的,需要自己区分一下. 直接上代码 using System; using System.Colle ...

  9. vue 生成带logo的二维码 qrcode-vue 支持下载图片 实例详解

    最近的项目上有个需求,生成带logo的二维码,网上大部分都是基于JQ插件jQuery.qrcode.对于vue项目并不适用,翻阅了大量资料后,我找到了qrcode-vue这款基于vue的生成二维码的插 ...

最新文章

  1. 应对5G网络需求,G.metro技术逐步走向成熟和应用
  2. Maven最佳实践:版本管理
  3. bzoj 2436: [Noi2011]Noi嘉年华
  4. 命令行选项解析函数:getopt()
  5. 【POJ - 2337】Catenyms(欧拉图相关,欧拉通路输出路径,tricks)
  6. 使用faketime修改docker内的时间,解决date: cannot set date: Operation not permitted问题
  7. jQuery版三级联动案例
  8. ssm架构 开源项目_如何为您的开源项目选择正确的品牌架构
  9. git 提交代码命令_提交代码:git push 命令的四种形式
  10. 最简单的视频编码器:编译(libx264,libx265,libvpx)
  11. python中help函数_Python help()函数
  12. C# WinForm 使用FlowLayoutPanel控件做为导航菜单按钮的容器
  13. leetcode刷题日记- 重复叠加字符串匹配
  14. Java对接ChinaPay提现(公私钥方式)
  15. javascript将数字转换成大写
  16. 前端布局篇之文字居中显示
  17. 【React自制全家桶】九、Redux入手
  18. hr标签---中心线:设置颜色
  19. 使用Dockerfile集成python3 docker基础镜像
  20. Oracle 存储过程 in、out、in out 参数的使用方法

热门文章

  1. js php计算器编程,js 计算器实现
  2. sanic教程-快速开始安装
  3. 二维数组根据每个数组的元素排序(奥运奖牌排序)的几种方法
  4. LAMP兄弟连38期激情开班
  5. mbp连接wifi没弹出认证页面
  6. 论MathType中空格的正确的输入方法
  7. 程序员专属10张壁纸
  8. PHP笔记-laravel中使用jquery及jquery.niceScroll.js
  9. Android USB识别开发
  10. 西门子S7-200 PLC 与信捷DS2伺服通讯程序,实际应用于编码器同步控制案例,即将编码器直接连接到伺服驱动器上