Skip to content

Commit 31267cb

Browse files
committed
Adjust Scan screen qrcode
1 parent c23379b commit 31267cb

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

v2rayN/v2rayN/Common/QRCodeHelper.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
using QRCoder;
22
using QRCoder.Xaml;
3+
using System.Drawing;
4+
using System.Windows.Interop;
5+
using System.Windows;
36
using System.Windows.Media;
7+
using ZXing.Common;
8+
using ZXing.QrCode;
9+
using ZXing.Windows.Compatibility;
10+
using ZXing;
411

512
namespace v2rayN
613
{
@@ -28,5 +35,70 @@ public class QRCodeHelper
2835
return null;
2936
}
3037
}
38+
39+
public static string ScanScreen(float dpiX, float dpiY)
40+
{
41+
try
42+
{
43+
var left = (int)(SystemParameters.WorkArea.Left);
44+
var top = (int)(SystemParameters.WorkArea.Top);
45+
var width = (int)(SystemParameters.WorkArea.Width / dpiX);
46+
var height = (int)(SystemParameters.WorkArea.Height / dpiY);
47+
48+
using Bitmap fullImage = new Bitmap(width, height);
49+
using (Graphics g = Graphics.FromImage(fullImage))
50+
{
51+
g.CopyFromScreen(left, top, 0, 0, fullImage.Size, CopyPixelOperation.SourceCopy);
52+
}
53+
int maxTry = 10;
54+
for (int i = 0; i < maxTry; i++)
55+
{
56+
int marginLeft = (int)((double)fullImage.Width * i / 2.5 / maxTry);
57+
int marginTop = (int)((double)fullImage.Height * i / 2.5 / maxTry);
58+
Rectangle cropRect = new(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2);
59+
Bitmap target = new(width, height);
60+
61+
double imageScale = (double)width / (double)cropRect.Width;
62+
using (Graphics g = Graphics.FromImage(target))
63+
{
64+
g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
65+
cropRect,
66+
GraphicsUnit.Pixel);
67+
}
68+
69+
BitmapLuminanceSource source = new(target);
70+
QRCodeReader reader = new();
71+
72+
BinaryBitmap bitmap = new(new HybridBinarizer(source));
73+
var result = reader.decode(bitmap);
74+
if (result != null)
75+
{
76+
return result.Text;
77+
}
78+
else
79+
{
80+
BinaryBitmap bitmap2 = new(new HybridBinarizer(source.invert()));
81+
var result2 = reader.decode(bitmap2);
82+
if (result2 != null)
83+
{
84+
return result2.Text;
85+
}
86+
}
87+
}
88+
}
89+
catch (Exception ex)
90+
{
91+
Logging.SaveLog(ex.Message, ex);
92+
}
93+
return string.Empty;
94+
}
95+
96+
public static Tuple<float, float> GetDpiXY(Window window)
97+
{
98+
IntPtr hWnd = new WindowInteropHelper(window).EnsureHandle();
99+
Graphics g = Graphics.FromHwnd(hWnd);
100+
101+
return new(96 / g.DpiX, 96 / g.DpiY);
102+
}
31103
}
32104
}

v2rayN/v2rayN/Common/Utils.cs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
using System.Windows.Interop;
1919
using System.Windows.Media;
2020
using System.Windows.Media.Imaging;
21-
using ZXing;
22-
using ZXing.Common;
23-
using ZXing.QrCode;
24-
using ZXing.Windows.Compatibility;
2521

2622
namespace v2rayN
2723
{
@@ -950,66 +946,6 @@ public static string GetFontsPath(string filename = "")
950946

951947
#endregion TempPath
952948

953-
#region scan screen
954-
955-
public static string ScanScreen(float dpiX, float dpiY)
956-
{
957-
try
958-
{
959-
var left = (int)(SystemParameters.WorkArea.Left);
960-
var top = (int)(SystemParameters.WorkArea.Top);
961-
var width = (int)(SystemParameters.WorkArea.Width / dpiX);
962-
var height = (int)(SystemParameters.WorkArea.Height / dpiY);
963-
964-
using Bitmap fullImage = new Bitmap(width, height);
965-
using (Graphics g = Graphics.FromImage(fullImage))
966-
{
967-
g.CopyFromScreen(left, top, 0, 0, fullImage.Size, CopyPixelOperation.SourceCopy);
968-
}
969-
int maxTry = 10;
970-
for (int i = 0; i < maxTry; i++)
971-
{
972-
int marginLeft = (int)((double)fullImage.Width * i / 2.5 / maxTry);
973-
int marginTop = (int)((double)fullImage.Height * i / 2.5 / maxTry);
974-
Rectangle cropRect = new(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2);
975-
Bitmap target = new(width, height);
976-
977-
double imageScale = (double)width / (double)cropRect.Width;
978-
using (Graphics g = Graphics.FromImage(target))
979-
{
980-
g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
981-
cropRect,
982-
GraphicsUnit.Pixel);
983-
}
984-
985-
BitmapLuminanceSource source = new(target);
986-
BinaryBitmap bitmap = new(new HybridBinarizer(source));
987-
QRCodeReader reader = new();
988-
Result result = reader.decode(bitmap);
989-
if (result != null)
990-
{
991-
string ret = result.Text;
992-
return ret;
993-
}
994-
}
995-
}
996-
catch (Exception ex)
997-
{
998-
Logging.SaveLog(ex.Message, ex);
999-
}
1000-
return string.Empty;
1001-
}
1002-
1003-
public static Tuple<float, float> GetDpiXY(Window window)
1004-
{
1005-
IntPtr hWnd = new WindowInteropHelper(window).EnsureHandle();
1006-
Graphics g = Graphics.FromHwnd(hWnd);
1007-
1008-
return new(96 / g.DpiX, 96 / g.DpiY);
1009-
}
1010-
1011-
#endregion scan screen
1012-
1013949
#region 开机自动启动等
1014950

1015951
/// <summary>

v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,10 +1026,10 @@ public async Task ScanScreenTaskAsync()
10261026
{
10271027
ShowHideWindow(false);
10281028

1029-
var dpiXY = Utils.GetDpiXY(Application.Current.MainWindow);
1029+
var dpiXY = QRCodeHelper.GetDpiXY(Application.Current.MainWindow);
10301030
string result = await Task.Run(() =>
10311031
{
1032-
return Utils.ScanScreen(dpiXY.Item1, dpiXY.Item2);
1032+
return QRCodeHelper.ScanScreen(dpiXY.Item1, dpiXY.Item2);
10331033
});
10341034

10351035
ShowHideWindow(true);

0 commit comments

Comments
 (0)