【问题标题】:How to color QR code using Zxing.net如何使用 Zxing.net 为二维码着色
【发布时间】:2018-05-01 14:41:00
【问题描述】:

我已经使用 ZXing.net 生成了二维码,我想给生成的二维码上色。如何在MVC.net中使用ZXing.net给二维码上色?

代码如下

IBarcodeWriter barcodeWriter = new BarcodeWriter
                {
                    Format = BarcodeFormat.QR_CODE,
                    Options = new QrCodeEncodingOptions
                    {
                        Width = 400,
                        Height = 400
                    }


                };

                var result = barcodeWriter.Write(qrcode);
                var barcodeBitmap = new Bitmap(result);
                #region code for text
                //RectangleF rectf = new RectangleF(0, 0, barcodeBitmap.Width, barcodeBitmap.Height);
                //Graphics g = Graphics.FromImage(barcodeBitmap);

                //g.SmoothingMode = SmoothingMode.AntiAlias;
                //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                //StringFormat format = new StringFormat()
                //{
                //    Alignment = StringAlignment.Center,
                //    LineAlignment = StringAlignment.Center
                //};
                //// Draw the text onto the image
                //g.DrawString("Vaishali", new Font("Tahoma", 8), Brushes.Red, rectf,format);
                #endregion

                #region code for logo
                System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("~/image") + "/logo.png");

                int left = (barcodeBitmap.Width / 2) - (logo.Width / 2);
                int top = (barcodeBitmap.Height / 2) - (logo.Height / 2);

                Graphics g = Graphics.FromImage(barcodeBitmap);

                g.DrawImage(logo, new Point(left, top));
                #endregion
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(barcodePath, FileMode.Create, FileAccess.ReadWrite))
                    {

                        barcodeBitmap.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }

请有人告诉我如何给颜色。

【问题讨论】:

    标签: .net model-view-controller qr-code zxing.net


    【解决方案1】:

    我创建了一个 GitHub Repo “ColorZXing.Net”,基本上你可以生成颜色单调的二维码,或者全彩色的二维码。

    GitHub:https://github.com/HainanZhao/ColorZXing.Net

    【讨论】:

      【解决方案2】:

      好吧,如果您只想要单色(例如蓝色或任何hexadecimal 值),您可以尝试这种方式

        private Bitmap generate() {
          Map<EncodeHintType, Object> hintsMap = new HashMap<>();
          hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
          hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
          hintsMap.put(EncodeHintType.MARGIN, 2);
          int mWidth = 100;
          int mHeight = 100;
          try {
              BitMatrix bitMatrix = new QRCodeWriter().encode(finalText, BarcodeFormat.QR_CODE, mWidth, mHeight, hintsMap);
              int[] pixels = new int[mWidth * mHeight];
              for (int i = 0; i < mHeight; i++) {
                  for (int j = 0; j < mWidth; j++) {
                      if (bitMatrix.get(j, i)) {// True if is is Black
                          pixels[i * mWidth + j] = 0xFFFFFFFF; //White
                      } else {
                          pixels[i * mWidth + j] = 0x282946; //Insert the color here. 
                      }
                  }
              }
              Bitmap bitmap1 = Bitmap.createBitmap(pixels, mWidth, mHeight, Bitmap.Config.ARGB_8888);
              //SaveImage(bitmap1);
              return bitmap1;
          } catch (WriterException e) {
              e.printStackTrace();
          }
          return null;
      }
      

      【讨论】:

      • 这是java。不是 c#
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多