Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //var g = new GF.XPolynom(GF.FromExponent(0), GF.FromExponent(0));
            //var eccNum = 2;
            //for (int i = 1; i < eccNum; i++) {
            //	g *= new GF.XPolynom(GF.FromExponent(i), GF.FromExponent(0));
            //}

            //var r = new GF.XPolynom[8 * eccNum];
            //for (int i = 0; i < 8 * eccNum; i++) {
            //	var msg = new GF.XPolynom(eccNum * 2 + 1);
            //	// msg[eccNum * 2] = GF.FromPolynom(0x55);
            //	msg[eccNum * 2 - 1 - i / 8] = GF.FromPolynom(1 << (7 - i % 8));

            //	r[i] = msg % g;
            //	System.Console.WriteLine(Convert.ToString(r[i][0].Polynom, 2).PadLeft(8, '0') + "," + Convert.ToString(r[i][1].Polynom, 2).PadLeft(8, '0'));
            //}
            //System.Console.WriteLine();
            //{
            //	var msg = new GF.XPolynom(eccNum * 2 + 1);
            //	// msg[eccNum * 2] = GF.FromPolynom(0x55);
            //	// msg[eccNum * 2 - 1] = GF.FromPolynom(0b11000000);
            //	//var r0 = msg % g;
            //	//System.Console.WriteLine(Convert.ToString(r0[0].Polynom, 2).PadLeft(8, '0') + "," + Convert.ToString(r0[1].Polynom, 2).PadLeft(8, '0'));

            //	msg[eccNum * 2 - 1] = GF.FromPolynom(0b10000000);
            //	msg[eccNum * 2 - 2] = GF.FromPolynom(0b00000001);
            //	var r0 = msg % g;
            //	System.Console.WriteLine(Convert.ToString(r0[0].Polynom, 2).PadLeft(8, '0') + "," + Convert.ToString(r0[1].Polynom, 2).PadLeft(8, '0'));
            //}



            //byte[] data1 = Encoding.UTF8.GetBytes("17689480023");
            //byte[] data2 = Encoding.UTF8.GetBytes("15059723108");
            //var (qr1, qr2, swap) = QRCodeMagician.CreateObfuscatedQRCode(data1, data2);
            //if (qr1 == null) { System.Console.WriteLine("失败"); return; }
            //Save(@"Z:\qr1.png", qr1, null, 5);
            //Save(@"Z:\qr2.png", qr2, null, 5);
            //Save(@"Z:\qr12.png", qr1, qr2, 5);
            //Save(@"Z:\qr21.png", qr2, qr1, 5);
            //SaveObfuscated(@"Z:\qr.png", qr1, qr2, 5);
            //System.Console.WriteLine("成功");
            //var qr = new QRCode(data1);
            //Save(@"Z:\qr.png", qr, null, 5);

            //var r1 = QRDataEncoder.CalculateECCWords(new byte[] { 1,3,2 }, 2);
            //var r2 = QRDataEncoder.CalculateECCWords(new byte[] { 2 }, 2);
            //var r3 = QRDataEncoder.CalculateECCWords(new byte[] { 1, 2 }, 4);

            // System.IO.File.WriteAllBytes(@"D:\MyDocuments\Desktop\1.bin", new byte[] { 1 });

            //const int N = 5;
            //const int M = 5;
            //var left = new byte[N][];
            //var right = new byte[N][];
            //for (int i = 0; i < N; i++) {
            //	left[i] = new byte[N];
            //	left[i][i] = 1;
            //	right[i] = new byte[M];
            //}
            //Random random = new Random();
            //for (int i = 0; i < N; i++) {
            //	for (int j = 0; j < M; j++) {
            //		right[i][j] = (byte) (random.Next(10) < 2 ? 1 : 0);
            //	}
            //}

            //Print(left, right);
            //System.Console.WriteLine();
            //List<int> indexes;
            //(indexes, right) = BitBlock.GaussianElimination(left, right);
            //Print(left, right);

            //for (int i = 0; i < indexes.Count; i++) {
            //	System.Console.Write(indexes[i] + " ");
            //}
            //System.Console.WriteLine();

            int version = 20;

            byte[] data1   = Encoding.UTF8.GetBytes("0.0");
            var    N       = QRInfo.GetN(version);
            int    padding = 6;
            // byte[] data1 = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
            var pixels = new ImagePixel[N, N];

            for (int x = 0; x < pixels.GetLength(0); x++)
            {
                for (int y = 0; y < pixels.GetLength(1); y++)
                {
                    //if (x > 15) {
                    //	pixels[x, y] = ImagePixel.White;
                    //} else if (x <= 4) {
                    //	pixels[x, y] = ImagePixel.White;
                    //} else {
                    //	pixels[x, y] = ImagePixel.Any;
                    //}
                    //if (x >= 11 || y <= 8) {
                    //	pixels[x, y] = ImagePixel.White;
                    //} else {
                    //	pixels[x, y] = ImagePixel.Any;
                    //}

                    if (y >= padding && y < N - padding && x >= padding && x < N - padding)
                    {
                        pixels[x, y] = y % 2 == 0 ? ImagePixel.White : ImagePixel.Black;
                    }
                    else
                    {
                        pixels[x, y] = ImagePixel.Any | ImagePixel.White;
                    }
                }
            }
            //pixels[0, 9] = ImagePixel.Black;
            //pixels[0, 10] = ImagePixel.Black;
            //pixels[0, 11] = ImagePixel.Black;
            //pixels[0, 12] = ImagePixel.Black;
            //pixels[1, 9] = ImagePixel.Black;
            //pixels[1, 10] = ImagePixel.Black;
            //pixels[1, 11] = ImagePixel.Black;
            //pixels[1, 12] = ImagePixel.Black;
            //pixels[9, 0] = ImagePixel.Any;
            //pixels[10, 0] = ImagePixel.Any;
            //pixels[11, 0] = ImagePixel.Any;
            //pixels[12, 0] = ImagePixel.Any;
            ////pixels[9, 1 ]= ImagePixel.Any;
            //pixels[10, 1] = ImagePixel.Any;
            //pixels[11, 1] = ImagePixel.Any;
            //pixels[12, 1] = ImagePixel.Any;

            int cellSize = 5;

            pixels = QRCodeMagician.GetImagePixel(version, new Bitmap(@"Z:\1.png"), cellSize);
            var qr = QRCodeMagician.ImageArt(DataMode.Byte, version, ECCLevel.L, MaskVersion.Version100, data1, pixels, QRInfo.GetMaxErrorAllowBytes(version, ECCLevel.L) / 2);

            Save(@"Z:\art.png", qr, null, cellSize);

            var qr2 = QRCodeMagician.ImageArt(DataMode.Byte, version, ECCLevel.L, MaskVersion.Version100, data1, pixels);

            Save(@"Z:\art2.png", qr2, null, cellSize);

            //var bits = new BitSet(1000);
            //bits[3] = true;
            //var r1 = GF.XPolynom.RSEncode(bits.ByteArray, 0, bits.ByteArray.Length, 100);
            //bits[3] = false;
            //bits[300] = true;
            //var r2 = GF.XPolynom.RSEncode(bits.ByteArray, 0, bits.ByteArray.Length, 100);
            //bits[3] = true;
            //var r3 = GF.XPolynom.RSEncode(bits.ByteArray, 0, bits.ByteArray.Length, 100);
        }
Exemplo n.º 2
0
        unsafe static void Main(string[] args)
        {
            int version  = 5;
            int cellSize = 6;

            byte[] data1    = Encoding.UTF8.GetBytes("BAKA");
            var    N        = QRInfo.GetN(version);
            var    maxError = QRInfo.GetMaxErrorAllowBytes(version, ECCLevel.L);
            var    template = new Bitmap(@"Z:\2.png");
            var    pixels   = QRCodeMagician.GetImagePixel(version, template, cellSize, cellSize / 3);
            var    qr       = QRCodeMagician.ImageArt(DataMode.Alphanumeric, version, ECCLevel.L, MaskVersion.Version100, data1, pixels, 4);
            var    bitmap   = new Bitmap((N + 2) * cellSize, (N + 2) * cellSize);
            // var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
            var fixedBlackBrush = Brushes.Black;
            var fixedWhiteBrush = Brushes.White;
            var blackBrush      = Brushes.Black;
            var whiteBrush      = Brushes.White;
            var halftone        = cellSize / 3;

            using (var g = Graphics.FromImage(bitmap)) {
                g.Clear(Color.White);
                g.DrawImage(new Bitmap(@"Z:\1.png"), new Rectangle(cellSize, cellSize, N * cellSize, N * cellSize));
                for (int x = 0; x < N; x++)
                {
                    for (int y = 0; y < N; y++)
                    {
                        var drawX = x + 1;
                        var drawY = y + 1;

                        if (qr[x, y].Type == QRValueType.TimingPatterns)
                        {
                            continue;
                        }


                        //if (qr[x, y].Type == QRValueType.Fixed || qr[x, y].Type == QRValueType.TimingPatterns || qr[x, y].Type == QRValueType.Format || qr[x, y].Type == QRValueType.Version) {
                        if (qr[x, y].Type == QRValueType.Fixed || qr[x, y].Type == QRValueType.TimingPatterns)
                        {
                            if (qr[x, y].Value)
                            {
                                g.FillRectangle(fixedBlackBrush, new Rectangle(drawX * cellSize, drawY * cellSize, cellSize, cellSize));
                            }
                            else
                            {
                                // g.FillRectangle(fixedWhiteBrush, new Rectangle(drawX * cellSize, drawY * cellSize, cellSize, cellSize));
                            }
                            //if (qr[x, y].Value) {
                            //	g.FillRectangle(blackBrush, new Rectangle(drawX * cellSize + halftone, drawY * cellSize + halftone, halftone, halftone));
                            //} else {
                            //	g.FillRectangle(whiteBrush, new Rectangle(drawX * cellSize + halftone, drawY * cellSize + halftone, halftone, halftone));
                            //}
                        }
                        else if (!pixels[x, y].HasFlag(ImagePixel.Stable) || qr[x, y].Value != pixels[x, y].HasFlag(ImagePixel.Black))
                        {
                            if (qr[x, y].Value)
                            {
                                g.FillRectangle(blackBrush, new Rectangle(drawX * cellSize + halftone, drawY * cellSize + halftone, halftone, halftone));
                            }
                            else
                            {
                                g.FillRectangle(whiteBrush, new Rectangle(drawX * cellSize + halftone, drawY * cellSize + halftone, halftone, halftone));
                            }
                            //if (qr[x, y].Value) {
                            //	g.FillRectangle(fixedBlackBrush, new Rectangle(drawX * cellSize, drawY * cellSize, cellSize, cellSize));
                            //} else {
                            //	g.FillRectangle(fixedWhiteBrush, new Rectangle(drawX * cellSize, drawY * cellSize, cellSize, cellSize));
                            //}
                        }
                    }
                }
            }
            bitmap.Save(@"Z:\out.png");
        }
Exemplo n.º 3
0
        private static void NewMethod1()
        {
            Random    random  = new Random();
            const int Version = 6;
            var       data    = Encoding.UTF8.GetBytes("0.0");
            int       N       = QRInfo.GetN(Version);

            ImagePixel[,] pixels = new ImagePixel[N, N];
            const int range = 7;

            for (int y = 0; y < N; y++)
            {
                for (int x = 0; x < N; x++)
                {
                    if (y < N / 2 && random.NextDouble() < (N / 2 - y) / (double)range)
                    {
                        pixels[x, y] = ImagePixel.Black;
                    }
                    else if (y > N / 2 && random.NextDouble() < (y - N / 2) / (double)range)
                    {
                        pixels[x, y] = ImagePixel.White;
                    }
                    else if (y < N / 2)
                    {
                        pixels[x, y] = ImagePixel.Black | ImagePixel.Any;
                    }
                    else
                    {
                        pixels[x, y] = ImagePixel.White | ImagePixel.Any;
                    }
                }
            }
            var qrcode = QRCodeMagician.ImageArt(DataMode.Alphanumeric, Version, ECCLevel.L, MaskVersion.Version010, data, pixels);


            for (int y = 0; y < N; y++)
            {
                for (int x = 0; x < N; x++)
                {
                    void SetValue(bool value)
                    {
                        switch (qrcode[x, y].Type)
                        {
                        case QRValueType.Data:
                        case QRValueType.DataPadding:
                        case QRValueType.Ecc:
                        case QRValueType.TimingPatterns:
                        case QRValueType.FixedPoint:
                        case QRValueType.Padding:
                            qrcode[x, y].Value = value;
                            break;
                        }
                    }

                    if (y < N / 2 - range)
                    {
                        SetValue(true);
                    }
                    else if (y > N / 2 + range)
                    {
                        SetValue(false);
                    }
                    else if (qrcode[x, y].Type == QRValueType.TimingPatterns)
                    {
                        SetValue(y < N / 2);
                    }
                }
            }
            Save(@"Z:\out.png", qrcode, null, 6);
        }