c#二维码生成附带文字

发布一下 0 0

c#生成二维码使用ZXing库,ZXing可以生成一维码和二维码,并附带文字。

并且可以设置二维码边距,


1.二维码生成

  public static Bitmap CreateQRCode(string asset)        {            EncodingOptions options = new QrCodeEncodingOptions            {                DisableECI = true,                CharacterSet = "UTF-8",                Width = 100,                           Height = 100            };           // options.Margin = 0;            BarcodeWriter writer = new BarcodeWriter();            writer.Format = BarcodeFormat.QR_CODE;            writer.Options = options;            //var matrix = writer.Encode(asset);            //writer.Options = new EncodingOptions();            //return writer.Write(DeleteWhite(matrix, 2));            return writer.Write(asset);        }

2.二维码添加文字

根据二维码生成的图片,在下发添加文字

  SaveFileDialog saveFileDialog = new SaveFileDialog();                    saveFileDialog.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpeg)|*.jpg;*.jpeg|ALL Files(*.*)|*.*";                    if (saveFileDialog.ShowDialog() == DialogResult.OK)                    {                        //String url = "http://cooperate.bjkalf.net/static/trace/index.html";                        String url = "https://www.baidu.com/";                        Bitmap bt = CreateQRCode(url);                        var pic = OverlayImage(bt);                        var Picture = OverlayText(pic, "请扫描二维码", 90);                        Picture.Save(saveFileDialog.FileName);                        Picture.Dispose();                        //bt.Save(saveFileDialog.FileName);                        //bt.Dispose();                        MessageBox.Show("保存成功");                    }
 protected Image OverlayImage(Bitmap lowerPic)        {            Bitmap upperPic = new Bitmap(lowerPic.Width, lowerPic.Height);            var upperImg = BitmapToImg(upperPic);            var lowerImg = BitmapToImg(lowerPic);            using (Graphics g = Graphics.FromImage(upperImg))            {                g.Clear(Color.White);                g.DrawImage(lowerImg, new Rectangle(0,                                                 0,                                                 lowerImg.Width,                                                 lowerImg.Height),                        0, 0, lowerImg.Width, lowerImg.Height, GraphicsUnit.Pixel);            }            return upperImg;        }        /// 图片上嵌入文字        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        protected Bitmap OverlayText(Image image, string name, int y)        {            using (Graphics g = Graphics.FromImage(image))            {                g.DrawImage(image, 100, -10, image.Width, image.Height);                using (Font f = new Font("宋体", 6))                {                    StringFormat format = new StringFormat();                    format.Alignment = StringAlignment.Center; //居中                    using (Brush b = new SolidBrush(Color.Black))                    {                        g.DrawString(name, f, b, 50, y, format);                    }                }            }            return ImgToBitmap(image);        }        private Image BitmapToImg(Bitmap Bitmap)        {            try            {                Bitmap map = new Bitmap(Bitmap);                Image img = map;                return img;            }            catch (System.Exception)            {                throw;            }        }        private  Bitmap ImgToBitmap(Image image)        {            try            {                Bitmap img = new Bitmap(image);                return img;            }            catch (Exception)            {                throw;            }        }

3.二维码边距无白色

zxing默认生成的二维码周围是有白色的边距,如果想让二维码填充整张图片,并且无边距,需要进行处理,处理方法如下

  private static BitMatrix DeleteWhite(BitMatrix matrix, int margin)        {            int[] rec = matrix.getEnclosingRectangle();            int resWidth = rec[2];            int resHeight = rec[3];            var resMatrix = new BitMatrix(resWidth + margin * 2, resHeight + margin * 2);            resMatrix.clear();            for (int i = 0; i < resWidth; i++)                for (int j = 0; j < resHeight; j++)                {                    if (matrix[rec[0] + i, rec[1] + j])                        resMatrix[margin + i, margin + j] = true;                }            return resMatrix;        }
  EncodingOptions options = new QrCodeEncodingOptions            {                DisableECI = true,                CharacterSet = "UTF-8",                Width = 100,                           Height = 100            };           // options.Margin = 0;            BarcodeWriter writer = new BarcodeWriter();            writer.Format = BarcodeFormat.QR_CODE;            writer.Options = options;            var matrix = writer.Encode(asset);            writer.Options = new EncodingOptions();            return writer.Write(DeleteWhite(matrix, 2));

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/207381.html