|
- using Microsoft.Win32;
- using NLog;
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
-
- namespace Shadowsocks.WPF.Behaviors
- {
- public static class Utilities
- {
- private static Logger logger = LogManager.GetCurrentClassLogger();
-
- private static string _tempPath = null;
-
- // return path to store temporary files
- public static string GetTempPath()
- {
- if (_tempPath == null)
- {
- bool isPortableMode = Configuration.Load().portableMode;
- try
- {
- if (isPortableMode)
- {
- _tempPath = Directory.CreateDirectory("ss_win_temp").FullName;
- // don't use "/", it will fail when we call explorer /select xxx/ss_win_temp\xxx.log
- }
- else
- {
- _tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Program.ExecutablePath.GetHashCode())).FullName;
- }
- }
- catch (Exception e)
- {
- logger.Error(e);
- throw;
- }
- }
- return _tempPath;
- }
-
- // return a full path with filename combined which pointed to the temporary directory
- public static string GetTempPath(string filename) => Path.Combine(GetTempPath(), filename);
-
- public static string ScanQRCodeFromScreen()
- {
- foreach (Screen screen in Screen.AllScreens)
- {
- using (Bitmap fullImage = new Bitmap(screen.Bounds.Width,
- screen.Bounds.Height))
- {
- using (Graphics g = Graphics.FromImage(fullImage))
- {
- g.CopyFromScreen(screen.Bounds.X,
- screen.Bounds.Y,
- 0, 0,
- fullImage.Size,
- 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);
- Rectangle cropRect = new Rectangle(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2);
- Bitmap target = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
-
- double imageScale = (double)screen.Bounds.Width / (double)cropRect.Width;
- using (Graphics g = Graphics.FromImage(target))
- {
- g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
- cropRect,
- GraphicsUnit.Pixel);
- }
- var source = new BitmapLuminanceSource(target);
- var bitmap = new BinaryBitmap(new HybridBinarizer(source));
- QRCodeReader reader = new QRCodeReader();
- var result = reader.decode(bitmap);
- if (result != null)
- return result.Text;
- }
- }
- }
- return null;
- }
-
- public static void OpenInBrowser(string url)
- {
- try
- {
- Process.Start(url);
- }
- catch
- {
- // hack because of this: https://github.com/dotnet/corefx/issues/10361
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- Process.Start(new ProcessStartInfo(url)
- {
- UseShellExecute = true,
- Verb = "open"
- });
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- {
- Process.Start("xdg-open", url);
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- Process.Start("open", url);
- }
- else
- {
- throw;
- }
- }
- }
- }
- }
|