public static List <QRCode> ExtractFrom(Image image)
        {
            List <QRCode> output = new List <QRCode>();
            Bitmap        bitmap = new Bitmap(image);

            ZXing.QrCode.QRCodeReader    qr        = new ZXing.QrCode.QRCodeReader();
            ZXing.LuminanceSource        source    = new ZXing.BitmapLuminanceSource(bitmap);
            ZXing.Common.HybridBinarizer hybrid    = new ZXing.Common.HybridBinarizer(source);
            ZXing.BinaryBitmap           binBitmap = new ZXing.BinaryBitmap(hybrid);

            ZXing.Result[] multiresults = new ZXing.Multi.QrCode.QRCodeMultiReader().decodeMultiple(binBitmap);

            if (multiresults != null && multiresults.Length > 0)
            {
                foreach (ZXing.Result result in multiresults)
                {
                    QRCode qrCode = new QRCode(result.Text);
                    qrCode.Points = new PointF[result.ResultPoints.Length];
                    for (int i = 0; i < result.ResultPoints.Length; i++)
                    {
                        qrCode.Points[i] = new PointF(result.ResultPoints[i].X, result.ResultPoints[i].Y);
                    }
                    output.Add(qrCode);
                }
            }

            ZXing.Result singleresult = qr.decode(binBitmap);

            if (singleresult != null)
            {
                QRCode qrCode = new QRCode(singleresult.Text);
                qrCode.Points    = new PointF[4];
                qrCode.Points[0] = new PointF(singleresult.ResultPoints[0].X, singleresult.ResultPoints[0].Y);
                qrCode.Points[1] = new PointF(singleresult.ResultPoints[1].X, singleresult.ResultPoints[1].Y);
                qrCode.Points[2] = new PointF(singleresult.ResultPoints[2].X, singleresult.ResultPoints[2].Y);
                if (singleresult.ResultPoints.Length > 2)
                {
                    qrCode.Points[3] = new PointF(singleresult.ResultPoints[3].X, singleresult.ResultPoints[3].Y);
                }
                output.Add(qrCode);
            }

            return(output);
        }
示例#2
0
        private async void ScanQRCodeItem_Click(object sender, EventArgs e)
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                using (var fullImage = new System.Drawing.Bitmap(screen.Bounds.Width,
                                                                 screen.Bounds.Height))
                {
                    using (var g = System.Drawing.Graphics.FromImage(fullImage))
                    {
                        g.CopyFromScreen(screen.Bounds.X,
                                         screen.Bounds.Y,
                                         0, 0,
                                         fullImage.Size,
                                         System.Drawing.CopyPixelOperation.SourceCopy);
                    }
                    int maxTry = 10;
                    for (int i = 0; i < maxTry; i++)
                    {
                        int marginLeft = (int)((double)fullImage.Width * i / 2.5 / maxTry);
                        int marginTop  = (int)((double)fullImage.Height * i / 2.5 / maxTry);
                        var cropRect   = new System.Drawing.Rectangle(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2);
                        var target     = new System.Drawing.Bitmap(screen.Bounds.Width, screen.Bounds.Height);

                        double imageScale = (double)screen.Bounds.Width / (double)cropRect.Width;
                        using (var g = System.Drawing.Graphics.FromImage(target))
                        {
                            g.DrawImage(fullImage, new System.Drawing.Rectangle(0, 0, target.Width, target.Height),
                                        cropRect,
                                        System.Drawing.GraphicsUnit.Pixel);
                        }
                        var source = new ZXing.BitmapLuminanceSource(target);
                        var bitmap = new ZXing.BinaryBitmap(new ZXing.Common.HybridBinarizer(source));
                        var reader = new ZXing.QrCode.QRCodeReader();
                        var result = reader.decode(bitmap);
                        if (result != null)
                        {
                            var splash = new QRCodeSplashForm();
                            if (result.Text.StartsWith("vmess://", StringComparison.OrdinalIgnoreCase))
                            {
                                var success = await controller.AddServerBySSURL(result.Text);

                                if (success)
                                {
                                    splash.FormClosed += splash_FormClosed;
                                }
                            }
                            else if (result.Text.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || result.Text.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                            {
                                _urlToOpen         = result.Text;
                                splash.FormClosed += openURLFromQRCode;
                            }
                            else
                            {
                                _urlToOpen         = result.Text;
                                splash.FormClosed += copyFromQRCode;
                            }
                            double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
                            foreach (ZXing.ResultPoint point in result.ResultPoints)
                            {
                                minX = Math.Min(minX, point.X);
                                minY = Math.Min(minY, point.Y);
                                maxX = Math.Max(maxX, point.X);
                                maxY = Math.Max(maxY, point.Y);
                            }
                            minX /= imageScale;
                            minY /= imageScale;
                            maxX /= imageScale;
                            maxY /= imageScale;
                            double margin = (maxX - minX) * 0.20f;
                            minX             += -margin + marginLeft;
                            maxX             += margin + marginLeft;
                            minY             += -margin + marginTop;
                            maxY             += margin + marginTop;
                            splash.Location   = new System.Drawing.Point(screen.Bounds.X, screen.Bounds.Y);
                            splash.TargetRect = new System.Drawing.Rectangle((int)minX + screen.Bounds.X, (int)minY + screen.Bounds.Y, (int)maxX - (int)minX, (int)maxY - (int)minY);
                            splash.Size       = new System.Drawing.Size(fullImage.Width, fullImage.Height);
                            splash.Show();
                            return;
                        }
                    }
                }
            }
            MessageBox.Show(I18N.GetString("No QRCode found. Try to zoom in or move it to the center of the screen."));
        }