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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章