Giter Club home page Giter Club logo

Comments (4)

BobTB avatar BobTB commented on June 19, 2024

I timed it, one QR code takes approx. 35 msec, and the image is approx 1MB big. With 2000 of them to run, I get 70 seconds runtime. I think that getting BW 1 bit image directly will cut this time a lot.

The cloning and changing to B/W image takes 2 msec.

from zxing.net.

micjahn avatar micjahn commented on June 19, 2024

There is no option available but you can write your own renderer. Take a look at the source code of the BitmapRenderer class. Implement your own BitmapRenderer1bpp, change the line
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
and this one
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
of the original BitmapRenderer and the different places of the pixel assignments

pixels[index++] = color.B;
pixels[index++] = color.G;
pixels[index++] = color.R;

After that you can use your custom renderer with the BitmapWriter:

  var x = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;

           var barcodeWriter = new BarcodeWriter 
            {

                Format = barcodeFormat,
                Options = new ZXing.QrCode.QrCodeEncodingOptions
                {
                    CharacterSet = "ISO-8859-2",
                    Width = 600,
                    Height = 600,
                    ErrorCorrection = x,
                    QrVersion = 15
                },
                Renderer = new BitmapRenderer1bpp()
            };

var bitmap = barcodeWriter.Write("ASDASD");

from zxing.net.

BobTB avatar BobTB commented on June 19, 2024

Great. Thank you!

from zxing.net.

lellis1936 avatar lellis1936 commented on June 19, 2024

I had a need for this as well and the class I developed is at bottom. It does not support Options or text content but otherwise seems to work fine. Suggestions for improvement are welcome. Not thoroughly tested and only tested with QR codes. Usage is:

var bcWriter = new BarcodeWriter { Renderer = new BitmapRenderer1bpp() };
BitMatrix bitmatrix = writer.encode(qrText, BarcodeFormat.QR_CODE, width, height, myHints);
Bitmap qrMonoBitmap = bcWriter.Write(bitmatrix);

Too bad there is not native support for monochrome QR because they are vastly smaller and substantially quicker to create. And of course black and white output is usually what's desired, not color.

Here's the custom class:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

using ZXing.Common;
using ZXing.OneD;

namespace ZXing.Rendering
{
    /// <summary>
    /// Renders a <see cref="BitMatrix" /> to a <see cref="Bitmap" /> image
    /// </summary>
    public class BitmapRenderer1bpp : IBarcodeRenderer<Bitmap>
    {
        static BitmapRenderer1bpp()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BitmapRenderer"/> class.
        /// </summary>
        public BitmapRenderer1bpp()
        {
        }

        /// <summary>
        /// Renders the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="format">The format.</param>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        public Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content)
        {
            return Render(matrix, format, content, null);
        }

        /// <summary>
        /// Renders the specified matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="format">The format.</param>
        /// <param name="content">The content.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        virtual public Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
        {
            var width = matrix.Width;
            var height = matrix.Height;

            // create the bitmap and lock the bits because we need the stride
            // which is the width of the image and possible padding bytes
            var bmp = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
            var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
            try
            {
                var pixels = new byte[bmpData.Stride * height];

                // going through the lines of the matrix
                for (int y = 0; y < matrix.Height; y++)
                {
                    // going through the columns of the current line
                    for (var x = 0; x < matrix.Width; x++)
                    {
                        byte bitValue = (byte)(matrix[x, y] ? 0 : 1);
                        if (bitValue == 1)
                        {
                            pixels[y * bmpData.Stride + (x / 8)] |= (byte)(0x80 >> (x % 8));
                        }
                    }
                }

                //Copy the data from the byte array into BitmapData.Scan0
                Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
            }
            finally
            {
                //Unlock the pixels
                bmp.UnlockBits(bmpData);
            }

            return bmp;
        }
    }
}

from zxing.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.