示例#1
0
        private void menuScreenQRCodeScan_Click(object sender, EventArgs e)
        {
            Thread.Sleep(100);
            foreach (Screen screen in Screen.AllScreens)
            {
                Point screen_size = Utils.GetScreenPhysicalSize();
                using (Bitmap fullImage = new Bitmap(screen_size.X,
                                                     screen_size.Y))
                {
                    using (Graphics g = Graphics.FromImage(fullImage))
                    {
                        g.CopyFromScreen(screen.Bounds.X,
                                         screen.Bounds.Y,
                                         0, 0,
                                         fullImage.Size,
                                         CopyPixelOperation.SourceCopy);
                    }
                    for (int i = 0; i < 100; i++)
                    {
                        double    stretch;
                        Rectangle cropRect = Scan.GetScanRect(fullImage.Width, fullImage.Height, i, out stretch);
                        if (cropRect.Width == 0)
                        {
                            break;
                        }

                        string    url;
                        Rectangle rect;
                        if (stretch == 1 ? Scan.ScanQRCode(screen, fullImage, cropRect, out url, out rect) : Scan.ScanQRCodeStretch(screen, fullImage, cropRect, stretch, out url, out rect))
                        {
                            QRCodeSplashForm splash = new QRCodeSplashForm();

                            splash.FormClosed += splash_FormClosed;


                            splash.Location = new Point(screen.Bounds.X, screen.Bounds.Y);
                            double dpi = Screen.PrimaryScreen.Bounds.Width / (double)screen_size.X;
                            splash.TargetRect = new Rectangle(
                                (int)(rect.Left * dpi + screen.Bounds.X),
                                (int)(rect.Top * dpi + screen.Bounds.Y),
                                (int)(rect.Width * dpi),
                                (int)(rect.Height * dpi));
                            splash.Size = new Size(fullImage.Width, fullImage.Height);

                            List <VmessItem> vmessItems = V2rayConfigHandler.ImportFromStrConfig(out string msg, url);
                            if (vmessItems != null && vmessItems.Count > 0)
                            {
                                foreach (VmessItem vmessItem in vmessItems)
                                {
                                    if (ConfigHandler.AddServer(ref config, vmessItem, -1) != 0)
                                    {
                                        break;
                                    }
                                }
                                splash.Show();
                                //刷新
                                RefreshServers();
                                LoadV2ray();
                            }
                            else
                            {
                                splash.Show();
                                UI.Show(msg);
                            }

                            //扫到一个二维码即退出
                            break;
                        }
                    }
                }
            }
        }
示例#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."));
        }