This project has moved. For the latest updates, please go here.

How can I change "BitMatrix encode" to "ByteMatrix encode"?

Aug 11, 2013 at 8:36 AM
I want to made a solid Color QRCode.
I finded a method of possible. but this method needs ByteMatrix of encode.
How can I change "BitMatrix encode" to "ByteMatrix encode" in "QRCodeWriter.cs" of "namespace ZXing.QrCode" ?
thanks.
Coordinator
Aug 11, 2013 at 8:18 PM
First a little hint: you can currently change the foreground and background color for qr and other barcodes. No need for a custom method.

If that is not what you want then I would propose that you post your method here. I will change it to use a BitMatrix instead of ByteMatrix.
That is easier as the opposite way.
Aug 12, 2013 at 12:52 AM
Edited Aug 12, 2013 at 12:55 AM
Thanks for your reply.
My code:
QRCodeWriter qrCode = new QRCodeWriter();

                ByteMatrix imgBitmap = qrCode.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
                var gColor = Color.Blue;

                sbyte[][] img = imgBitmap.Array;
                LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(0, 0, width, height), foregroundColor, gColor, LinearGradientMode.Vertical);

                Bitmap bmp = new Bitmap(width, height);
                Graphics gg = Graphics.FromImage(bmp);
                gg.Clear(backgroundColor);

                for (int i = 0; i < img.Length - 1; i++)
                {
                    for (int j = 0; j < img.Length - 1; j++)
                    {
                        if (img[j][i] == 0)
                        {
                            gg.FillRectangle(lb, i, j, 1, 1);
                        }
                        else
                        {                            
                            gg.FillRectangle(backgroundColor, i, j, 1, 1);
                        }

                    }
                }
And at the last I want to made a QRCode,as shown below.
Image or Image
Is it possible to use this project?
thanks
Coordinator
Aug 12, 2013 at 8:15 PM
You have to put the code of your method into a custom renderer class like the following:
using System.Drawing;
using System.Drawing.Drawing2D;

using ZXing.Common;
using ZXing.Rendering;

namespace ZXing.Rendering
{
   public class CustomBitmapRenderer : BitmapRenderer
   {
      public Color BackgroundGradientColor { get; set; }
      public Color ForegroundGradientColor { get; set; }

      public override Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
      {
         int width = matrix.Width;
         int height = matrix.Height;

         var backgroundBrush = new LinearGradientBrush(
            new Rectangle(0, 0, width, height), BackgroundGradientColor, Background, LinearGradientMode.Vertical);
         var foregroundBrush = new LinearGradientBrush(
            new Rectangle(0, 0, width, height), ForegroundGradientColor, Foreground, LinearGradientMode.ForwardDiagonal);

         var bmp = new Bitmap(width, height);
         var gg = Graphics.FromImage(bmp);
         gg.Clear(Background);

         for (int x = 0; x < width - 1; x++)
         {
            for (int y = 0; y < height - 1; y++)
            {
               if (matrix[x, y])
               {
                  gg.FillRectangle(foregroundBrush, x, y, 1, 1);
               }
               else
               {
                  gg.FillRectangle(backgroundBrush, x, y, 1, 1);
               }
            }
         }

         return bmp;
      }
   }
}
I modified it a little bit and added two more properties for a better customizing of the color gradient.
Add the class to your sample project. Now you can use the custom renderer with the BarcodeWriter class.
A small sample:
var content = "Sample text for encoding";
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Options = new EncodingOptions
   {
      Height = 400,
      Width = 400
   },
   Renderer = new CustomBitmapRenderer
      {
         Background = Color.Aqua,
         BackgroundGradientColor = Color.HotPink,
         Foreground = Color.DarkBlue,
         ForegroundGradientColor = Color.DarkCyan
      }
};
var bitmap = writer.Write(content);
Now you can modify the "Render" method of the class "CustomBitmapRenderer" to fit your needs.
Aug 13, 2013 at 3:56 AM
Thanks very much.
and I have a question too.
how can get the position detection patterns and change the color in this project?
Coordinator
Aug 13, 2013 at 5:04 PM
Take the CustomBitmapRenderer class and modify the Render method to fit your needs.
There is no built-in way to do such things. I think you can reuse the Detector class of the qr codes decoder to find the patterns.
The encoder doesn't put out the information.
Sorry.
Aug 14, 2013 at 12:36 AM
Edited Aug 14, 2013 at 12:36 AM
Thanks,I will try it.
Jul 11, 2014 at 3:40 AM
micjahn wrote:
First a little hint: you can currently change the foreground and background color for qr and other barcodes. No need for a custom method.

If that is not what you want then I would propose that you post your method here. I will change it to use a BitMatrix instead of ByteMatrix.
That is easier as the opposite way.
Hi, I saw you post in other thread to construct a codewriter like this
var writer = new BarcodeWriter
{
  Format = BarcodeFormat.QR_CODE,
  Options = new QrCodeEncodingOptions {Width = 25, Height = 25}
};

my question is "you can currently change the foreground and background color for qr and other barcodes", how to set the color of background or foreground?
thanks~
Jul 11, 2014 at 6:03 AM
I found it.
      writer.Renderer = new ZXing.Rendering.WriteableBitmapRenderer()
        {
            Foreground = System.Windows.Media.Color.FromArgb(255, 0, 0, 255) // blue
        };