netcore2.0跨平台环境 生成二维码和条形码

环境VS 2017 16.4社区版

在项目的NuGet 管理器上找到ZXing.Net.Bindings.ZKWeb.System.Drawing

安装。

安装之后

安装之后可以看到项目引用了ZXing.Net   ZKWeb.System.Drawing  ZXing.ZKWeb.System.Drawing.dll

代码如下。要注意的是netcore下还没有System.Drawing的微软官方实现,目前是使用的ZKWeb.System.Drawing。

using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using System.Text;
using ZXing;
using ZXing.Common;

namespace EwmTest
{
    /// <summary>
    /// 二维码和条形码
    /// </summary>
    public class CodeHelper
    {
        // 生成二维码
        public static void CreateCodeEwm(string message,string gifFileName, int width=600, int height=600)
        {
            int heig = width;
            if (width > height)
            {
                heig = height;
                width= height;
            }
            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var w = new ZXing.QrCode.QRCodeWriter();
        
            BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 0,
             
            };

            Bitmap b2 = zzb.Write(b);
            b2.Save(gifFileName, ImageFormat.Gif);
            b2.Dispose();
            
        }
        /// <summary>
        /// 读取二维码或者条形码从图片
        /// </summary>
        /// <param name="imgFile"></param>
        /// <returns></returns>
        public static string ReadFromImage(string imgFile)
        {
            
            if (string.IsNullOrWhiteSpace(imgFile))
            {
                return "";
            }
            Image img = Image.FromFile(imgFile);
            Bitmap b = new Bitmap(img);
            
            //该类名称为BarcodeReader,可以读二维码和条形码
            var zzb = new ZXing.ZKWeb.BarcodeReader();
            zzb.Options = new DecodingOptions
            {
                CharacterSet = "UTF-8"
            };
            Result r=zzb.Decode(b);
            string resultText = r.Text;
            b.Dispose();
            img.Dispose();

            return resultText;

        }
        
        //将Bitmap  写为byte[]的方法
        public static byte[] BitmapToArray(Bitmap bmp)
        {
            byte[] byteArray = null;

            using (MemoryStream stream = new MemoryStream())
            {
                
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
                
            return byteArray;
        }
        // 生成条形码
        public static void CreateCodeTxm(string message, string gifFileName, int width, int height)
        {

            if (string.IsNullOrWhiteSpace(message)) {
                return;
            }

            var w = new ZXing.OneD.CodaBarWriter();
            BitMatrix b= w.encode(message, BarcodeFormat.ITF, width, height);
            var zzb=new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 3,
                PureBarcode=true
            };
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Bitmap b2= zzb.Write(b);
            b2.Save(gifFileName, ImageFormat.Gif);
            b2.Dispose();
        }


       
    }
}

代码全面完成。(*^▽^*)

2018.7.18

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值