Пример #1
0
        // 生成二维码
        public static void CreateCode(string message, string gifFileName, int width = 600, int height = 600)
        {
            int heig = width;

            if (width > height)
            {
                heig  = height;
                width = height;
            }
            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }
            string dir = Path.GetDirectoryName(gifFileName);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var w = new ZXing.QrCode.QRCodeWriter();

            BitMatrix b   = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
            var       zzb = new ZXing.ZKWeb.BarcodeWriter();

            zzb.Options = new EncodingOptions()
            {
                Margin = 0,
            };

            Bitmap b2 = zzb.Write(b);

            b2.Save(gifFileName, ImageFormat.Gif);
            b2.Dispose();
        }
Пример #2
0
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var writer = new ZXing.QrCode.QRCodeWriter();
            var matrix = writer.encode(comboBox1.Text, ZXing.BarcodeFormat.QR_CODE, 0, 0);

            int    scale = 10;
            int    www   = 1 + matrix.Dimension + 1;
            Bitmap pic   = new Bitmap(scale * www, scale * www);

            using (var cv = Graphics.FromImage(pic)) {
                cv.Clear(Color.White);
                for (int y = 0; y < matrix.Dimension; y++)
                {
                    for (int x = 0; x < matrix.Dimension; x++)
                    {
                        if (matrix[x, y])
                        {
                            cv.FillRectangle(Brushes.Black,
                                             new RectangleF(scale * (1 + x), scale * (1 + y), scale, scale)
                                             );
                        }
                    }
                }
            }

            pictureBox1.Image = pic;
        }
Пример #3
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="message"></param>
        /// <param name="gifFileName"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static byte[] CreateCodeEwm(string message, int width = 400, int height = 400)
        {
            int heig = width;

            if (width > height)
            {
                heig  = height;
                width = height;
            }
            if (string.IsNullOrWhiteSpace(message))
            {
                return(null);
            }
            var w = new ZXing.QrCode.QRCodeWriter();

            BitMatrix b   = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
            var       zzb = new ZXing.ZKWeb.BarcodeWriter
            {
                Options = new EncodingOptions {
                    Margin = 0
                }
            };
            Bitmap bmp = zzb.Write(b);

            byte[] byteArray = null;
            using (MemoryStream stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
            bmp.Dispose();
            return(byteArray);
        }
        public static Bitmap GetQRCodeImage(string code)
        {
            var writer        = new ZXing.QrCode.QRCodeWriter();
            var encodedString = writer.encode(code, ZXing.BarcodeFormat.QR_CODE, 4, 4);
            var qrCodeWriter  = new ZXing.BarcodeWriter();

            return(qrCodeWriter.Write(encodedString));
        }
Пример #5
0
        private static Image GenerateQRCode(string Content)
        {
            ZXing.QrCode.QRCodeWriter qrc = new ZXing.QrCode.QRCodeWriter();
            ZXing.Common.BitMatrix    bmx = qrc.encode(Content, ZXing.BarcodeFormat.QR_CODE, 100, 100);
            ZXing.BarcodeWriter       bcw = new ZXing.BarcodeWriter();
            Bitmap bmp = bcw.Write(bmx);

            return((Image)bmp);
        }
Пример #6
0
        ////////////////////////////////////////////////////////////////////////

        protected override BitmapOutput RenderE(QR elem, Template tpl, DataMap data)
        {
            var text = elem.data.Parse(data);

            // generate QR

            var writer = new ZXing.QrCode.QRCodeWriter();
            var hints  = new Dictionary <ZXing.EncodeHintType, object>();

            hints[ZXing.EncodeHintType.MARGIN] = elem.margin;
            switch (elem.error)
            {
            case QR.Error.Low: hints[ZXing.EncodeHintType.ERROR_CORRECTION] = ZXing.QrCode.Internal.ErrorCorrectionLevel.L; break;

            case QR.Error.Med: hints[ZXing.EncodeHintType.ERROR_CORRECTION] = ZXing.QrCode.Internal.ErrorCorrectionLevel.M; break;

            case QR.Error.High: hints[ZXing.EncodeHintType.ERROR_CORRECTION] = ZXing.QrCode.Internal.ErrorCorrectionLevel.Q; break;

            case QR.Error.Max: hints[ZXing.EncodeHintType.ERROR_CORRECTION] = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; break;
            }

            if (text == "")
            {
                text = "\0";
            }
            var code = writer.encode(text, ZXing.BarcodeFormat.QR_CODE, 0, 0, hints);
            var qr   = Helper.Create(code.Dimension, code.Dimension);

            for (int y = 0; y < qr.Height; ++y)
            {
                var row = code.getRow(y, null);
                for (int x = 0; x < qr.Width; ++x)
                {
                    var val = row.isRange(x, x + 1, true);
                    qr.SetPixel(x, y, val ? Color.Black : Color.White);
                }
            }

            // generate bitmap

            var size = elem.rect.CalcWH(new Vector(-1, -1));

            if (size.x < 0)
            {
                size.x = size.y;
            }
            else if (size.y < 0)
            {
                size.y = size.x;
            }

            if (size.x < 0)
            {
                size.x = size.y = code.Height * elem.pitch;
            }
            else if (elem.resize == QR.Resize.None) /* do nothing */ } {
Пример #7
0
        private void Encoding()
        {
            text.TextContent = "Generating barcode...";
            var      ft     = Resources.GetFont(Resources.FontResources.small);
            DateTime dt     = DateTime.Now;
            var      writer = new ZXing.QrCode.QRCodeWriter();

            var matrix = writer.encode("Time is " + dt, BarcodeFormat.QR_CODE, 50, 50);
            var bitmap = matrix.ToBitmap(BarcodeFormat.QR_CODE, null);

            Screen.DrawRectangle(Color.White, 0, 0, 0, Screen.Width, Screen.Height, 0, 0, Color.White, 0, 0,
                                 Color.White, Screen.Width, Screen.Height, 255);
            Screen.DrawImage(2, 2, bitmap, 0, 0, 49, 49);
            Screen.DrawText((DateTime.Now - dt).ToString(), ft, Color.Black, 2, 70);
            Screen.Flush();

            Debug.Print((DateTime.Now - dt).ToString());
        }
        public static byte[] QRTextBytes(string data, int width = 64, bool invers = false)
        {
            var writer = new ZXing.QrCode.QRCodeWriter();

            var hints = new Dictionary <ZXing.EncodeHintType, object>
            {
                { ZXing.EncodeHintType.CHARACTER_SET, "ISO-8859-1" },
                { ZXing.EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M }
            };

            var matrix = writer.encode(data, ZXing.BarcodeFormat.QR_CODE, width, width, hints);
            var bytes  = new List <byte>();
            var y      = 0;

            while (y < width)
            {
                for (var x = 0; x < width; x++)
                {
                    if (matrix[x, y] == true && matrix[x, y + 1] == true)
                    {
                        bytes.Add(219);
                    }
                    else if (matrix[x, y] == true && matrix[x, y + 1] == false)
                    {
                        bytes.Add(223);
                    }
                    else if (matrix[x, y] == false && matrix[x, y + 1] == true)
                    {
                        bytes.Add(220);
                    }
                    else
                    {
                        bytes.Add(32);
                    }
                }
                y += 2;
            }

            var result = bytes.ToArray();

            if (invers)
            {
                for (var i = 0; i < result.Length; i++)
                {
                    if (result[i] == 0xDB)
                    {
                        result[i] = 0x20;
                    }
                    else if (result[i] == 0x20)
                    {
                        result[i] = 0xDB;
                    }
                    else if (result[i] == 0xDC)
                    {
                        result[i] = 0xDF;
                    }
                    else if (result[i] == 0xDF)
                    {
                        result[i] = 0xDC;
                    }
                }
            }

            return(result);
        }
Пример #9
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Start);
            lista = LDbConnection.GetActualUserExpo();
            var toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            SetSupportActionBar(toolbar);
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(false);
            SupportActionBar.SetHomeButtonEnabled(true);
            SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
            drawerLayout   = FindViewById <Android.Support.V4.Widget.DrawerLayout>(Resource.Id.drawer_layout);
            navigationView = FindViewById <Android.Support.Design.Widget.NavigationView>(Resource.Id.nav_view);
            navigationView.NavigationItemSelected += HomeNavigationView_NavigationItemSelected;
            var    qrcode = FindViewById <Android.Widget.ImageView>(Resource.Id.Start_qrcode);
            var    writer = new ZXing.QrCode.QRCodeWriter();
            String s      = "";

            if (LDbConnection.getUserType() == "Uczestnik")
            {
                s = "Uczestnik:" + LDbConnection.GetUser().Email;
            }
            else if (LDbConnection.getUserType() == "Wystawca")
            {
                s = "Wystawca:" + LDbConnection.GetCompany().Email;
            }
            ZXing.Common.BitMatrix  bm          = writer.encode(s, ZXing.BarcodeFormat.QR_CODE, 500, 500);
            Android.Graphics.Bitmap ImageBitmap = Android.Graphics.Bitmap.CreateBitmap(500, 500, Config.Argb8888);

            for (int i = 0; i < 500; i++)
            {     //width
                for (int j = 0; j < 500; j++)
                { //height
                    ImageBitmap.SetPixel(i, j, bm[i, j] ? Color.Black : Color.White);
                }
            }

            if (ImageBitmap != null)
            {
                qrcode.SetImageBitmap(ImageBitmap);
            }
            var expo_list = FindViewById <Android.Widget.Spinner>(Resource.Id.Start_targi);

            expo_list.ItemSelected += new System.EventHandler <Android.Widget.AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
            var adapter = new MyExpoSingleListViewAdapter(lista, this);

            expo_list.Adapter = adapter;
            var btn1 = (Button)FindViewById(Resource.Id.Start_join);

            btn1.Visibility = Android.Views.ViewStates.Invisible;
            btn1.Click     += delegate
            {
                var NxtAct = new Android.Content.Intent(this, typeof(UserActivity));
                StartActivity(NxtAct);
            };
            var button = FindViewById <Android.Widget.Button>(Resource.Id.Start_scan);

            button.Click += async delegate
            {
                ZXing.Mobile.MobileBarcodeScanner scanner;
                ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
                scanner = new ZXing.Mobile.MobileBarcodeScanner();
                scanner.UseCustomOverlay = false;

                //We can customize the top and bottom text of the default overlay
                scanner.BottomText = "Poczekaj, aż kod kreskowy będzie automatycznie zeskanowany!";

                //Start scanning
                var result = await scanner.Scan();

                scanner.Cancel();
                if (result == null)
                {
                    scanner.Cancel();
                }
                else
                {
                    scanner.Cancel();
                    string[] scan = result.Text.Split(':');
                    if (scan[0] == "Wystawca")
                    {
                        var NxtAct = new Android.Content.Intent(this, typeof(CompanyActivity));
                        NxtAct.PutExtra("Email", scan[1]);
                        NxtAct.PutExtra("expo_id", lista[select].Id);
                        NxtAct.PutExtra("Search", result.Text);
                        NxtAct.PutExtra("Show", true);

                        StartActivity(NxtAct);
                    }
                    else if (scan[0].Contains("Uczestnik"))
                    {
                        System.Console.WriteLine("Uczestnik");
                        var NxtAct = new Android.Content.Intent(this, typeof(UserActivity));
                        NxtAct.PutExtra("Email", scan[1]);
                        NxtAct.PutExtra("expo_id", lista[select].Id);
                        NxtAct.PutExtra("Search", result.Text);
                        NxtAct.PutExtra("Show", true);

                        StartActivity(NxtAct);
                    }
                }
            };
            if (lista == null)
            {
                button.Visibility = Android.Views.ViewStates.Invisible;
                btn1.Visibility   = Android.Views.ViewStates.Visible;
            }
        }
Пример #10
0
        /// <summary>
        /// 生成二维码(320*320)
        /// </summary>
        /// <param name="text">文本内容</param>
        /// <param name="format">保存格式</param>
        /// <param name="logoImgae">Logo图片(缩放到真实二维码区域尺寸的1/6)</param>
        /// <param name="keepWhiteBorderPixelVal">白边处理(负值表示不做处理,最大值不超过真实二维码区域的1/10)</param>
        /// <returns></returns>
        public static byte[] QRCoder(string text, SkiaSharp.SKEncodedImageFormat format, byte[] logoImgae = null, int keepWhiteBorderPixelVal = -1)
        {
            byte[] reval        = null;
            int    width        = 320;
            int    height       = 320;
            var    qRCodeWriter = new ZXing.QrCode.QRCodeWriter();
            var    hints        = new Dictionary <ZXing.EncodeHintType, object>();

            hints.Add(ZXing.EncodeHintType.CHARACTER_SET, "utf-8");
            hints.Add(ZXing.EncodeHintType.QR_VERSION, 8);
            hints.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.Q);
            var bitMatrix = qRCodeWriter.encode(text, ZXing.BarcodeFormat.QR_CODE, width, height, hints);
            var w         = bitMatrix.Width;
            var h         = bitMatrix.Height;
            var sKBitmap  = new SkiaSharp.SKBitmap(w, h);

            int blackStartPointX = 0;
            int blackStartPointY = 0;
            int blackEndPointX   = w;
            int blackEndPointY   = h;

            #region --绘制二维码(同时获取真实的二维码区域起绘点和结束点的坐标)--
            var sKCanvas     = new SkiaSharp.SKCanvas(sKBitmap);
            var sKColorBlack = SkiaSharp.SKColor.Parse("000000");
            var sKColorWihte = SkiaSharp.SKColor.Parse("ffffff");
            sKCanvas.Clear(sKColorWihte);
            bool blackStartPointIsNotWriteDown = true;
            for (var y = 0; y < h; y++)
            {
                for (var x = 0; x < w; x++)
                {
                    var flag = bitMatrix[x, y];
                    if (flag)
                    {
                        if (blackStartPointIsNotWriteDown)
                        {
                            blackStartPointX = x;
                            blackStartPointY = y;
                            blackStartPointIsNotWriteDown = false;
                        }
                        blackEndPointX = x;
                        blackEndPointY = y;
                        sKCanvas.DrawPoint(x, y, sKColorBlack);
                    }
                    else
                    {
                        //sKCanvas.DrawPoint(x, y, sKColorWihte);//不用绘制(背景是白色的)
                    }
                }
            }
            sKCanvas.Dispose();
            #endregion

            int qrcodeRealWidth  = blackEndPointX - blackStartPointX;
            int qrcodeRealHeight = blackEndPointY - blackStartPointY;

            #region -- 处理白边 --
            if (keepWhiteBorderPixelVal > -1)//指定了边框宽度
            {
                var borderMaxWidth = (int)Math.Floor((double)qrcodeRealWidth / 10);
                if (keepWhiteBorderPixelVal > borderMaxWidth)
                {
                    keepWhiteBorderPixelVal = borderMaxWidth;
                }
                var nQrcodeRealWidth  = width - keepWhiteBorderPixelVal - keepWhiteBorderPixelVal;
                var nQrcodeRealHeight = height - keepWhiteBorderPixelVal - keepWhiteBorderPixelVal;

                var sKBitmap2 = new SkiaSharp.SKBitmap(width, height);
                var sKCanvas2 = new SkiaSharp.SKCanvas(sKBitmap2);
                sKCanvas2.Clear(sKColorWihte);
                //二维码绘制到临时画布上时无需抗锯齿等处理(避免文件增大)
                sKCanvas2.DrawBitmap(
                    sKBitmap,
                    new SkiaSharp.SKRect
                {
                    Location = new SkiaSharp.SKPoint {
                        X = blackStartPointX, Y = blackStartPointY
                    },
                    Size = new SkiaSharp.SKSize {
                        Height = qrcodeRealHeight, Width = qrcodeRealWidth
                    }
                },
                    new SkiaSharp.SKRect
                {
                    Location = new SkiaSharp.SKPoint {
                        X = keepWhiteBorderPixelVal, Y = keepWhiteBorderPixelVal
                    },
                    Size = new SkiaSharp.SKSize {
                        Width = nQrcodeRealWidth, Height = nQrcodeRealHeight
                    }
                });

                blackStartPointX = keepWhiteBorderPixelVal;
                blackStartPointY = keepWhiteBorderPixelVal;
                qrcodeRealWidth  = nQrcodeRealWidth;
                qrcodeRealHeight = nQrcodeRealHeight;

                sKCanvas2.Dispose();
                sKBitmap.Dispose();
                sKBitmap = sKBitmap2;
            }
            #endregion

            #region -- 绘制LOGO --
            if (logoImgae != null && logoImgae.Length > 0)
            {
                SkiaSharp.SKBitmap sKBitmapLogo = SkiaSharp.SKBitmap.Decode(logoImgae);
                if (!sKBitmapLogo.IsEmpty)
                {
                    var sKPaint2 = new SkiaSharp.SKPaint
                    {
                        FilterQuality = SkiaSharp.SKFilterQuality.None,
                        IsAntialias   = true
                    };
                    var logoTargetMaxWidth  = (int)Math.Floor((double)qrcodeRealWidth / 6);
                    var logoTargetMaxHeight = (int)Math.Floor((double)qrcodeRealHeight / 6);
                    var qrcodeCenterX       = (int)Math.Floor((double)qrcodeRealWidth / 2);
                    var qrcodeCenterY       = (int)Math.Floor((double)qrcodeRealHeight / 2);
                    var logoResultWidth     = sKBitmapLogo.Width;
                    var logoResultHeight    = sKBitmapLogo.Height;
                    if (logoResultWidth > logoTargetMaxWidth)
                    {
                        var r = (double)logoTargetMaxWidth / logoResultWidth;
                        logoResultWidth  = logoTargetMaxWidth;
                        logoResultHeight = (int)Math.Floor(logoResultHeight * r);
                    }
                    if (logoResultHeight > logoTargetMaxHeight)
                    {
                        var r = (double)logoTargetMaxHeight / logoResultHeight;
                        logoResultHeight = logoTargetMaxHeight;
                        logoResultWidth  = (int)Math.Floor(logoResultWidth * r);
                    }
                    var pointX = qrcodeCenterX - (int)Math.Floor((double)logoResultWidth / 2) + blackStartPointX;
                    var pointY = qrcodeCenterY - (int)Math.Floor((double)logoResultHeight / 2) + blackStartPointY;

                    var sKCanvas3 = new SkiaSharp.SKCanvas(sKBitmap);
                    var sKPaint   = new SkiaSharp.SKPaint
                    {
                        FilterQuality = SkiaSharp.SKFilterQuality.Medium,
                        IsAntialias   = true
                    };
                    sKCanvas3.DrawBitmap(
                        sKBitmapLogo,
                        new SkiaSharp.SKRect
                    {
                        Location = new SkiaSharp.SKPoint {
                            X = 0, Y = 0
                        },
                        Size = new SkiaSharp.SKSize {
                            Height = sKBitmapLogo.Height, Width = sKBitmapLogo.Width
                        }
                    },
                        new SkiaSharp.SKRect
                    {
                        Location = new SkiaSharp.SKPoint {
                            X = pointX, Y = pointY
                        },
                        Size = new SkiaSharp.SKSize {
                            Height = logoResultHeight, Width = logoResultWidth
                        }
                    }, sKPaint);
                    sKCanvas3.Dispose();
                    sKPaint.Dispose();
                    sKBitmapLogo.Dispose();
                }
                else
                {
                    sKBitmapLogo.Dispose();
                }
            }
            #endregion

            SkiaSharp.SKImage sKImage = SkiaSharp.SKImage.FromBitmap(sKBitmap);
            sKBitmap.Dispose();
            var data = sKImage.Encode(format, 75);
            sKImage.Dispose();
            reval = data.ToArray();
            data.Dispose();

            return(reval);
        }