This object renders a QR Code as a BitMatrix 2D array of greyscale values. [email protected] (Daniel Switkin)
Inheritance: Writer
Beispiel #1
1
        public static byte[] GeneratePngBytes(string text, int width, int height)
        {
            var hints = new Dictionary<EncodeHintType, object>
            {
                [EncodeHintType.MARGIN] = 0
            };

            var qr = new QRCodeWriter();
            var matrix = qr.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

            var writer = new BarcodeWriter {Options = {Margin = 0}};

            using (var bitmap = writer.Write(matrix))
            {
                bitmap.MakeTransparent(Color.White);

                var ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Png);

                return ms.ToArray();
            }
        }
Beispiel #2
1
 public BitMatrix GetQR(string message)
 {
     QRCodeWriter qrCode = new QRCodeWriter();
     BitMatrix imgBitmap = qrCode.encode(message, ZXing.BarcodeFormat.QR_CODE, 350, 350);
     return imgBitmap;
 }
    public async static Task<Uri> ToQrDataUri(this ISdp sdp, int width, int height)
    {
      var qrCodeWriter = new QRCodeWriter();
      var bitMatrix = qrCodeWriter.encode(sdp.ToString(), ZXing.BarcodeFormat.QR_CODE, width, height);

      using (var canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), 500, 500, 96))
      {
        using (var drawingSession = canvasRenderTarget.CreateDrawingSession())
        {
          for (var y = 0; y < height; y++)
          {
            for (var x = 0; x < width; x++)
            {
              drawingSession.DrawRectangle(x, y, 1, 1, bitMatrix.get(x, y) ? Color.FromArgb(0, 0, 0, 0) : Color.FromArgb(255, 255, 255, 255));
            }
          }
        }

        using (var inMemoryRandomAccessStream = new InMemoryRandomAccessStream())
        {
          await canvasRenderTarget.SaveAsync(inMemoryRandomAccessStream, CanvasBitmapFileFormat.Png);
          inMemoryRandomAccessStream.Seek(0);
          var buffer = new byte[inMemoryRandomAccessStream.Size];
          await inMemoryRandomAccessStream.ReadAsync(buffer.AsBuffer(), (uint)inMemoryRandomAccessStream.Size, InputStreamOptions.None);
          return new Uri($"data:image/png;base64,{Convert.ToBase64String(buffer)}");
        }
      }
    }
Beispiel #4
0
    public void CreateCode(string code)
    {
        int QRwidth = 256; // 圖形寬
        int QRheight = 256; //圖形高
        QRCodeWriter writer=new QRCodeWriter();
        BitMatrix bm = writer.encode(code, BarcodeFormat.QR_CODE, QRwidth, QRheight);
        Texture2D t2d=new Texture2D(QRwidth,QRheight);
        r.material.mainTexture=t2d;

        int width = bm.Width;
        int height = bm.Height;

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                //bm.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("Red") : ColorTranslator.FromHtml("Yellow");
                if(bm[x, y]){
                    t2d.SetPixel(x, y,Color.black);
                }else{
                    t2d.SetPixel(x, y,Color.white);
                }
            }
        }
        t2d.Apply();
    }
Beispiel #5
0
        /// <summary>
        ///         Trả về file name (path) của ảnh tạo ra bởi ZXing library (temp file)
        /// </summary>
        /// <param name="Text">Văn bản cần sinh mã QR</param>
        /// <param name="ImageSize">Kích thước của ảnh QR. Tối đa là 500 px</param>
        /// <param name="Correction">Mức độ chịu lỗi</param>
        /// <param name="Margin">Số điểm ảnh trắng để làm biên </param>
        /// <returns></returns>
        static string GetQRCodeLocalFileNameByZXing(string Text, int ImageSize = 500, CorrectionLevel Correction = CorrectionLevel.High, int Margin = 0)
        {
            QRCodeWriter qr = new ZXing.QrCode.QRCodeWriter(); //QRCode as a BitMatrix 2D array

            Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>();

            hint.Add(EncodeHintType.MARGIN, Margin); // margin of the QRCode image
            hint.Add(EncodeHintType.ERROR_CORRECTION, Correction);

            var matrix = qr.encode(Text, BarcodeFormat.QR_CODE, ImageSize, ImageSize, hint); // encode QRCode matrix from source text

            ZXing.BarcodeWriter w = new ZXing.BarcodeWriter();
            Bitmap img            = w.Write(matrix);                    // QRCode Bitmap image
            string tempFile       = Path.GetTempFileName();             //create a temp file to save QRCode image

            img.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png); //save QRCode image to temp file


            //old one - use clipboard
            //MemoryStream ms = new MemoryStream();
            //img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //save QRCode image to memory stream
            //System.Drawing.Image i = System.Drawing.Image.FromStream(ms); // create image in Image form
            //Clipboard.SetDataObject(i);// set image to clipboard



            return(tempFile);
        }
Beispiel #6
0
        public static Android.Graphics.Bitmap Create(string hostName, int port, string connectionPassword)
        {
            StringBuilder qrString = new StringBuilder(DataSchemeWithSuffix);
            qrString.Append(hostName);
            qrString.Append("/");
            qrString.Append(port);
            qrString.Append("/");
            qrString.Append(connectionPassword);

            BitMatrix qrMatrix = new QRCodeWriter().encode(qrString.ToString(), ZXing.BarcodeFormat.QR_CODE, 800, 800);

            return new BitmapRenderer().Render(qrMatrix, ZXing.BarcodeFormat.QR_CODE, string.Empty);
        }
Beispiel #7
0
        /// <summary>
        ///         Trả về file name (path) của ảnh tạo ra bởi ZXing library (temp file)
        /// </summary>
        /// <param name="Text">Văn bản cần sinh mã QR</param>
        /// <param name="ImageSize">Kích thước của ảnh QR. Tối đa là 500 px</param>
        /// <param name="Correction">Mức độ chịu lỗi</param>
        /// <param name="Margin">Số điểm ảnh trắng để làm biên </param>
        /// <returns></returns>
        static string GetQRCodeLocalFileNameByZXing(string Text, int ImageSize = 500, CorrectionLevel Correction = CorrectionLevel.High, int Margin = 0)
        {
            QRCodeWriter qr = new ZXing.QrCode.QRCodeWriter(); //QRCode as a BitMatrix 2D array

            Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>();

            hint.Add(EncodeHintType.MARGIN, Margin); // margin of the QRCode image
            hint.Add(EncodeHintType.ERROR_CORRECTION, Correction);

            var matrix = qr.encode(Text, BarcodeFormat.QR_CODE, ImageSize, ImageSize, hint); // encode QRCode matrix from source text

            ZXing.BarcodeWriter w = new ZXing.BarcodeWriter();
            Bitmap img            = w.Write(matrix);                    // QRCode Bitmap image
            string tempFile       = Path.GetTempFileName();             //create a temp file to save QRCode image

            img.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png); //save QRCode image to temp file

            return(tempFile);
        }
    /// <summary>
    /// Draw qr code on enable.
    /// </summary>
    void OnEnable()
    {
        _using = GameObject.FindObjectOfType <UsingCon>();
        ZXing.QrCode.QRCodeWriter QRWriter = new ZXing.QrCode.QRCodeWriter();
        BitMatrix encoded = QRWriter.encode(_using.profile.passCode, ZXing.BarcodeFormat.QR_CODE, 512, 512);
        Texture2D tex     = new Texture2D(512, 512, TextureFormat.RGBA32, false);

        Color[] pixels = tex.GetPixels();
        int     k      = 0;

        for (int j = 0; j < 512; j++)
        {
            ZXing.Common.BitArray row = new ZXing.Common.BitArray(512);
            row = encoded.getRow(j, null);
            int[] intRow = row.Array;
            for (int i = intRow.Length - 1; i >= 0; i--)
            {
                int thirtyTwoPixels = intRow[i];
                for (int b = 31; b >= 0; b--)
                {
                    int pixel = ((thirtyTwoPixels >> b) & 1);
                    if (pixel == 0)
                    {
                        pixels[k] = Color.white;
                    }
                    else
                    {
                        pixels[k] = Color.black;
                    }
                    k++;
                }
            }
        }

        print("PassCode for draw QR : " + _using.profile.passCode);

        tex.SetPixels(pixels);
        tex.Apply();
        qr_output.material.mainTexture = tex;
    }
    public static ImageSource ToQrImageSource(this ISdp sdp, int width, int height)
    {
      var qrCodeWriter = new QRCodeWriter();
      var bitMatrix = qrCodeWriter.encode(sdp.ToString(), ZXing.BarcodeFormat.QR_CODE, width, height);

      var canvasImageSource = new CanvasImageSource(CanvasDevice.GetSharedDevice(), width, height, 96);
      using (var drawingSession = canvasImageSource.CreateDrawingSession(Color.FromArgb(255, 255, 255, 255)))
      {
        for (var y = 0; y < height; y++)
        {
          for (var x = 0; x < width; x++)
          {
            if (bitMatrix.get(x, y))
            {
              drawingSession.DrawRectangle(x, y, 1, 1, Color.FromArgb(255, 0, 0, 0));
            }
          }
        }
      }

      return canvasImageSource;
    }
Beispiel #10
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.qr_generator_activity);
            initGUI ();
            btn_gen.Click += delegate {
                if(!inputText.Text.Equals("")){
                    try{
                    writer = new QRCodeWriter();
                    matrix = writer.encode(inputText.Text, BarcodeFormat.QR_CODE, size, size);

                    int height = matrix.Height;
                    int width = matrix.Width;
                    int[] pixels = new int[width * height];
                    for (int y = 0; y < height; y++)
                    {
                        int offset = y * width;
                        for (int x = 0; x < width; x++)
                        {
                            // pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000
                            // : 0xFFFFFFFF;
                            pixels[offset + x] = matrix[x,y] ? Android.Graphics.Color.Black : Android.Graphics.Color.White;
                        }
                    }
                    Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
                    bitmap.SetPixels(pixels, 0, width, 0, 0, width, height);
                    qrCodeView.SetImageBitmap(bitmap);
                    }catch(Exception e){
                        Console.WriteLine(e.StackTrace);
                    }

                }else{
                    Toast.MakeText(this, "Nothing to encode!", ToastLength.Long).Show();
                }
            };
        }