Beispiel #1
0
        // Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
        // vertical and horizontal orders respectively.
        private static int applyMaskPenaltyRule1Internal(ByteMatrix matrix, bool isHorizontal)
        {
            int penalty = 0;
            int len     = matrix.Width - 1;

            for (int i = 1; i <= len; i++)
            {
                int bit = isHorizontal ? matrix.get_Renamed(i, len) : matrix.get_Renamed(len, i);
                if (bit == 1)
                {
                    penalty++;
                }
            }
            return(penalty);
        }
Beispiel #2
0
        internal override void Draw2DBarcode(IGraphicsRenderer g, float kx, float ky)
        {
            Brush        light = Brushes.White;
            Brush        dark  = new SolidBrush(Color);
            GraphicsPath path  = new GraphicsPath();

            g.FillRectangle(light, 0, 0, matrix.Width * PixelSize * kx, matrix.Height * PixelSize * kx);

            for (int y = 0; y < matrix.Height; y++)
            {
                for (int x = 0; x < matrix.Width; x++)
                {
                    if (matrix.get_Renamed(x, y) == 0)
                    {
                        path.AddRectangle(new RectangleF(
                                              x * PixelSize * kx,
                                              y * PixelSize * ky,
                                              PixelSize * kx,
                                              PixelSize * ky
                                              ));
                    }
                }
            }
            if (path.PointCount > 0)
            {
                g.FillPath(dark, path);
            }
        }
Beispiel #3
0
        internal override void Draw2DBarcode(IGraphicsRenderer g, float kx, float ky)
        {
            Brush        light = Brushes.White;
            Brush        dark  = new SolidBrush(Color);
            GraphicsPath path  = new GraphicsPath();

            for (int y = 0; y < matrix.Height; y++)
            {
                for (int x = 0; x < matrix.Width; x++)
                {
                    if (matrix.get_Renamed(x, y) == 0)
                    {
                        g.PathAddRectangle(path, new RectangleF(
                                               x * PixelSize * kx,
                                               y * PixelSize * ky,
                                               PixelSize * kx,
                                               PixelSize * ky
                                               ));
                    }
                }
            }
            if (path.PointCount > 0)
            {
                g.FillPath(dark, path);
                if (text.StartsWith("SPC"))
                {
                    ErrorCorrection = QRCodeErrorCorrection.M;
                }
            }

            dark.Dispose();
            path.Dispose();
        }
Beispiel #4
0
        internal override void Draw2DBarcode(IGraphics g, float kx, float ky)
        {
            Brush dark = new SolidBrush(Color);

            for (int y = 0; y < matrix.Height; y++)
            {
                for (int x = 0; x < matrix.Width; x++)
                {
                    if (matrix.get_Renamed(x, y) == 0)
                    {
                        g.FillRectangle(dark, new RectangleF(
                                            x * PixelSize * kx,
                                            y * PixelSize * ky,
                                            PixelSize * kx,
                                            PixelSize * ky
                                            ));
                    }
                }
            }
            if (text.StartsWith("SPC"))
            {
                ErrorCorrection = QRCodeErrorCorrection.M;
            }

            dark.Dispose();
        }
Beispiel #5
0
        public Bitmap toBitmap(ByteMatrix matrix)
        {
            int    width  = matrix.Width;
            int    height = matrix.Height;
            Bitmap bmap   = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return(bmap);
        }
    public Sprite CreateQRCode(string context, Texture2D gameLogo = null, int offsetX = 0, int offsetY = 0)
    {
        QRCodeWriter qrwriter = new QRCodeWriter();
        Hashtable    table    = new Hashtable();

        table[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.Q;
        ByteMatrix matrix = qrwriter.encode(context, com.google.zxing.BarcodeFormat.QR_CODE, 440, 440, table);

        Texture2D pic        = new Texture2D(440, 440, TextureFormat.RGBA32, false);
        Color     whitecolor = new Color(238 / 255f, 245 / 255f, 212 / 255f);

        for (int x = 0; x < 440; x++)
        {
            for (int y = 0; y < 440; y++)
            {
                pic.SetPixel(x, y, matrix.get_Renamed(y, x) == 0 ? Color.black : whitecolor);
            }
        }

        if (gameLogo != null)
        {
            for (int x = 0; x < gameLogo.width; x++)
            {
                for (int y = 0; y < gameLogo.height; y++)
                {
                    pic.SetPixel(x + offsetX, y + offsetY, gameLogo.GetPixel(x, y));
                }
            }
        }

        pic.Apply(false);

        Sprite sp = Sprite.Create(pic, new Rect(0, 0, pic.width, pic.height), new Vector2(0.5f, 0.5f));

        return(sp);
    }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="contents"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="hints"></param>
        /// <returns></returns>
        public BitMatrix encode(System.String contents, BarcodeFormat format, int width, int height, IDictionary <EncodeHintType, object> hints)
        {
            //contents = "12345";
            //width = 17;
            //height = 17;
            //hints = new Dictionary<EncodeHintType, object>();
            //hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            //I should probably throw an exception here.
            if (!hints.ContainsKey(EncodeHintType.MICROQRCODE_VERSION))
            {
                hints.Add(EncodeHintType.MICROQRCODE_VERSION, 4);
            }

            if (contents == null || contents.Length == 0)
            {
                throw new System.ArgumentException("Found empty contents");
            }

            if (format != BarcodeFormat.MICRO_QR_CODE)
            {
                throw new System.ArgumentException("Can only encode MICRO_QR_CODE, but got " + format);
            }

            if (width < 0 || height < 0)
            {
                throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
            }


            //hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");

            var errorCorrectionLevel = ErrorCorrectionLevel.L;
            int cVersionNum          = -1;

            if (hints != null)
            {
                if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
                {
                    var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
                    if (requestedECLevel != null)
                    {
                        errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
                        if (errorCorrectionLevel == null)
                        {
                            switch (requestedECLevel.ToString().ToUpper())
                            {
                            case "L":
                                errorCorrectionLevel = ErrorCorrectionLevel.L;
                                break;

                            case "M":
                                errorCorrectionLevel = ErrorCorrectionLevel.M;
                                break;

                            case "Q":
                                errorCorrectionLevel = ErrorCorrectionLevel.Q;
                                break;

                            default:
                                errorCorrectionLevel = ErrorCorrectionLevel.L;
                                break;
                            }
                        }
                    }
                }
                if (hints.ContainsKey(EncodeHintType.MICROQRCODE_VERSION))
                {
                    int versionNum = (int)hints[EncodeHintType.MICROQRCODE_VERSION];
                    if (versionNum >= 1 && versionNum <= 4)
                    {
                        cVersionNum = versionNum;
                    }
                }
            }

            MicroQRCode code = new MicroQRCode();

            //Step 1 Encode the Data.
            Encoder.encode(contents, errorCorrectionLevel, hints, code, cVersionNum);

            //Step 2 Render the ByteMatrix using the original code.
            ByteMatrix microQrCodeByteMatrix = renderByteMatrix(code, width, height);

            //Step 3 Convert the Byte Matrix to a QrCode.ByteMatrix (swapping 1 for 0 and 0 for 1)
            ZXing.QrCode.Internal.ByteMatrix qrCodeByteMatrix = new QrCode.Internal.ByteMatrix(width, height);
            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    // Get the byte value and perform byye swap (swapping 1 for 0 and 0 for 1)
                    byte value = (byte)microQrCodeByteMatrix.get_Renamed(x, y);
                    if (value == 0)
                    {
                        qrCodeByteMatrix.set(x, y, 1);
                    }
                    else
                    {
                        qrCodeByteMatrix.set(x, y, 0);
                    }
                }
            }

            //Step 4 render the result and return a BitMatrix
            BitMatrix bitMatrix2 = renderResult(qrCodeByteMatrix, code, width, height, QUIET_ZONE_SIZE);

            return(bitMatrix2);
        }