示例#1
0
 /// <summary>
 /// Draws the bitmap using the given pixel data.
 /// </summary>
 /// <param name="pixelData">The pixel data.</param>
 /// <param name="bitmap">The bitmap.</param>
 public static void DrawBitmap(ZXing.Rendering.PixelData pixelData, System.Drawing.Bitmap bitmap)
 {
     System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
     try
     {
         // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
         System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
     }
     finally
     {
         bitmap.UnlockBits(bitmapData);
     }
 }
示例#2
0
 /// <summary>
 /// Gets the pixel data for the given QR atributes.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="margin">The margin.</param>
 /// <returns></returns>
 public static ZXing.Rendering.PixelData GetPixelData(string text, int width, int height, int margin = 0)
 {
     ZXing.BarcodeWriterPixelData qrCodeWriter = new ZXing.BarcodeWriterPixelData
     {
         Format  = ZXing.BarcodeFormat.QR_CODE,
         Options = new QrCodeEncodingOptions
         {
             Height          = height,
             Width           = width,
             Margin          = margin,
             ErrorCorrection = ErrorCorrectionLevel.L
         }
     };
     ZXing.Rendering.PixelData pixelData = qrCodeWriter.Write(text);
     return(pixelData);
 }
示例#3
0
        public async Task <IActionResult> GetQrCode([FromRoute] string text, [FromRoute] int?size = 100, [FromRoute] int?margin = 5)
        {
            //TODO move to the QuickCore assembly and import using Aplication Parts

            //Render the QR image
            ZXing.Rendering.PixelData pixelData = QuickCore.Renderer.GetPixelData(text, size.Value, size.Value, margin.Value);

            // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
            // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
                using (MemoryStream ms = new MemoryStream())
                {
                    QuickCore.Renderer.DrawBitmap(pixelData, bitmap);

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    return(File(ms.GetBuffer(), "image/png"));
                }
        }
示例#4
0
        /// <summary>
        /// Invokes the creation asynchronously.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
        {
            string qrCode = context.Request.Query["qrcode"].FirstOrDefault();

            if (qrCode != null)
            {
                //Get the QR code attributes
                string text = context.Request.Query["content"].FirstOrDefault();

                //Get sizes (specific width and height override size)
                int size;
                if (!int.TryParse(context.Request.Query["size"].FirstOrDefault(), out size))
                {
                    size = 250; //Default
                }

                int margin;
                if (!int.TryParse(context.Request.Query["margin"].FirstOrDefault(), out margin))
                {
                    margin = 5; //Default
                }


                //Render the QR image
                context.Response.ContentType = "image/png";
                ZXing.Rendering.PixelData pixelData = Renderer.GetPixelData(text, size, size, margin);

                // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
                // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
                using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
                    using (MemoryStream ms = new MemoryStream())
                    {
                        Renderer.DrawBitmap(pixelData, bitmap);

                        // save to stream as PNG
                        bitmap.Save(context.Response.Body, System.Drawing.Imaging.ImageFormat.Png);
                    }
            }
            else
            {
                // Call the next delegate/middleware in the pipeline
                await _next(context);
            }
        }
示例#5
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var qrCodeWriter = new ZXing.BarcodeWriterPixelData
            {
                Format  = ZXing.BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = QR_DIMENTION,
                    Width  = QR_DIMENTION,
                    Margin = QR_MARGIN
                }
            };

            ZXing.Rendering.PixelData pixelData = qrCodeWriter.Write(QrUrl);
            // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
            // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
            using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
                using (var ms = new MemoryStream())
                {
                    BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    // save to stream as PNG
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    output.TagName = "img";
                    output.Attributes.Clear();
                    output.Attributes.Add("width", QR_DIMENTION);
                    output.Attributes.Add("height", QR_DIMENTION);
                    output.Attributes.Add("alt", QrUrl);
                    output.Attributes.Add("src", string.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
                }
        }
示例#6
0
        private byte[] GetBarcode(string input, int?width = 100, int?height = 100, BarcodeFormat format = BarcodeFormat.CODE_128)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData
            {
                Format  = format,
                Options = new ZXing.Common.EncodingOptions
                {
                    PureBarcode = true,
                    Margin      = 0,
                    Width       = width.Value,
                    Height      = height.Value
                }
            };

            //return Encoding.UTF8.GetBytes(barcodeWriter.Encode(input).ToString());
            ZXing.Rendering.PixelData pixelData = writer.Write(input);
            using (Bitmap bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    // PNG or JPEG or whatever you want
                    bitmap.SetResolution(70F, 70F);
                    bitmap.Save(ms, ImageFormat.Png);
                    return(ms.ToArray());
                }
            }
        }
示例#7
0
        /// <summary>
        /// Synchronously executes the <see cref="T:Microsoft.AspNetCore.Razor.TagHelpers.TagHelper" /> with the given <paramref name="context" /> and
        /// <paramref name="output" />.
        /// </summary>
        /// <param name="context">Contains information associated with the current HTML tag.</param>
        /// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            string text = context.AllAttributes["content"].Value.ToString();
            string alt  = context.AllAttributes["alt"].Value.ToString();

            //Get size
            int size;

            if (!int.TryParse(context.AllAttributes["size"]?.Value.ToString(), out size))
            {
                size = 250; //Default
            }
            int margin;

            if (!int.TryParse(context.AllAttributes["margin"]?.Value.ToString(), out margin))
            {
                margin = 5; //Default
            }

            ZXing.Rendering.PixelData pixelData = Renderer.GetPixelData(text, size, size, margin);

            // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
            // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
            using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
                using (MemoryStream ms = new MemoryStream())
                {
                    Renderer.DrawBitmap(pixelData, bitmap);

                    // save to stream as PNG
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    output.TagName = "img";
                    output.Attributes.Clear();
                    output.Attributes.Add("width", size);
                    output.Attributes.Add("height", size);
                    output.Attributes.Add("alt", alt);
                    output.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
                }
        }
示例#8
0
        public async Task <IActionResult> GetQrCode([FromRoute] string url)
        {
            //TODO move to the QuickCore assembly and import using Application Parts
            var qrPostfix  = url.Substring(url.IndexOf("qrcode"));
            var qrSegments = qrPostfix.Split('/');

            int size;

            if (!int.TryParse(qrSegments.Skip(1).FirstOrDefault(), out size))
            {
                size = 100;
            }
            int margin;

            if (!int.TryParse(qrSegments.Skip(2).FirstOrDefault(), out margin))
            {
                margin = 5;
            }

            //Get the target URL and use as text
            string targetPath = url.Substring(0, url.IndexOf("qrcode"));
            string text       = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/{targetPath}{HttpContext.Request.QueryString}";

            //Render the QR image
            ZXing.Rendering.PixelData pixelData = QuickCore.Renderer.GetPixelData(text, size, size, margin);

            // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
            // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
                using (MemoryStream ms = new MemoryStream()) {
                    QuickCore.Renderer.DrawBitmap(pixelData, bitmap);

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    return(File(ms.GetBuffer(), "image/png"));
                }
        }