C# .Net 使用zxing.dll生成二维码,条形码

public static string GetBarcode(string format, string value, int? width, int? height)
        {
            BarcodeWriter writer = null;

            Bitmap bitmap = null;

            string strbase64 = string.Empty;

            writer = new BarcodeWriter();
            if (format == "QR_CODE")
            {
                EncodingOptions options = new QrCodeEncodingOptions
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    Width = width == null ? 500 : Convert.ToInt32(width),
                    Height = height == null ? 500 : Convert.ToInt32(height)
                };
                writer.Options = options;
                writer.Format = BarcodeFormat.QR_CODE;
                bitmap = writer.Write(value);
            }
            else if (format == "CODE_39")
            {
                /*
                        * 1.支持数字,字母,-,$,空格
                        * 2.产生的条码大
                        * 3.常用于许多用途
                        */
                writer.Format = BarcodeFormat.CODE_39;
                MultiFormatWriter mutiWriter = new MultiFormatWriter();
                BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_39, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
                bitmap = new BarcodeWriter().Write(bm);
            }
            else if (format == "CODE_128")
            {
                /*
                        * 1.支持字母或者数字所有字符
                        * 2.包含3个表格更好的数据进行编码
                        */
                writer.Format = BarcodeFormat.CODE_128;
                MultiFormatWriter mutiWriter = new MultiFormatWriter();
                BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_128, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
                bitmap = new BarcodeWriter().Write(bm);
            }
            else if (format == "ITF")
            {
                EncodingOptions options = new QrCodeEncodingOptions
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    Width = width == null ? 500 : Convert.ToInt32(width),
                    Height = height == null ? 150 : Convert.ToInt32(height)
                };
                writer.Options = options;
                writer.Format = BarcodeFormat.ITF;
                bitmap = writer.Write(value);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                strbase64 = Convert.ToBase64String(arr);
            }

            return strbase64;
        }
 
 
//引用文件下载地址
//http://files.cnblogs.com/files/guichi/Newtonsoft.rar

转载于:https://www.cnblogs.com/guichi/p/5423128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值