WPF使用Zxing.net生成二维码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
using ZXing.Windows.Compatibility;


namespace Huanzhang.Common.Helpers
{
    public static class QRCodeHelper
    {
        /// 
        /// 生成二维码
        /// 
        /// 二维码内容
        /// 二维码宽度,单位像素
        /// 二维码高度,单位像素
        /// 
        public static BitmapSource? CreateQRCode(string sContent, int width = 300, int height = 300)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = new QrCodeEncodingOptions()
            {
                CharacterSet = "UTF-8",
                Margin = 1,
                DisableECI = true,
                Height = height,
                Width = width,
            };

            var bitmap = writer.Write(sContent);

            return BitmapToImageSource(bitmap);
        }

        /// 
        /// 生成二维码
        /// 
        /// 二维码内容
        /// 二维码宽度,单位像素
        /// 二维码高度,单位像素
        /// 二维码Logo图片路径
        /// 
        public static BitmapSource? CreateQRCode(string sContent, int width, int height, string logoImagPath)
        {
            if (!File.Exists(logoImagPath))
            {
                throw new ArgumentException("二维码Logo图片路径不存在!");
            }
            var logo = System.Drawing.Image.FromFile(logoImagPath);
            //初始化
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary hint = new Dictionary();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hint.Add(EncodeHintType.MARGIN, 1);

            //生成二维码
            BitMatrix bm = writer.encode(sContent, BarcodeFormat.QR_CODE, width, height, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);
            //计算尺寸
            int[] rectangle = bm.getEnclosingRectangle();

            //计算插入Logo的大小位置
            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);

            return BitmapToImageSource(bmpimg);
        }

        private static BitmapImage? BitmapToImageSource(Bitmap bitmap)
        {
            if (bitmap == null) return null;
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                bitmap.Dispose();
                memory.Position = 0;
                BitmapImage bitmapimage = new BitmapImage();
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();

                return bitmapimage;
            }
        }

        public static string LoadImage(string imagePath, ref string sFormat)
        {
            BarcodeReader reader = new BarcodeReader();
            //设置读取的格式(一般为UTF-8)
            reader.Options.CharacterSet = "UTF-8";
            Bitmap p = new Bitmap(imagePath);
            Result result = reader.Decode(p);
            if (result != null)
            {
                sFormat = result.BarcodeFormat.ToString();
                return result.ToString();
            }
            else
                return null;
        }
    }
}

在Nuget引用Zxing.net 包和ZXing.Net.Bindings.Windows.Compatibility包即可。

你可能感兴趣的:(WPF,.Net,.net)