Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #26 from LeoChen98/dev
Browse files Browse the repository at this point in the history
[2.4.0.21]
  • Loading branch information
LeoChen98 authored May 8, 2020
2 parents c784048 + cd1751f commit 95fde76
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
2 changes: 1 addition & 1 deletion BiliAccount.TestProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static void Main(string[] args)

ByQRCode.QrCodeStatus_Changed += ByQRCode_QrCodeStatus_Changed;
ByQRCode.QrCodeRefresh += ByQRCode_QrCodeRefresh;
ByQRCode.LoginByQrCode().Save("tmp.jpg");
ByQRCode.LoginByQrCode("#FF66CCFF", "#00000000", true).Save("tmp.png");

//string token = Console.ReadLine();
//Console.WriteLine(BiliAccount.Linq.ByPassword.IsTokenAvailable(token));
Expand Down
6 changes: 3 additions & 3 deletions BiliAccount/BiliAccount.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>BiliAccount</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>BiliAccount</PackageId>
<Version>2.3.6.20</Version>
<Version>2.4.0.21</Version>
<Authors>LeoChen</Authors>
<Company>zhangbudademao.com</Company>
<Product>BiliAccount</Product>
Expand All @@ -19,8 +19,8 @@
<PackageTags>bilibili bililive bililogin biliaccount</PackageTags>
<PackageIcon>favicon_4.png</PackageIcon>
<PackageReleaseNotes>
fixes:
1. 修复.net core 3.x下二维码登录返回值转换错误的问题。
adds:
1. 新增二维码图像自定义可选参数。(close #25)
</PackageReleaseNotes>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
30 changes: 25 additions & 5 deletions BiliAccount/Core/ByQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ public static void CancelLogin()
}

/// <summary>
/// 获取登陆二维码
/// 获取二维码
/// </summary>
public static Bitmap GetQrcode()
/// <param name="Foreground">前景颜色</param>
/// <param name="Background">背景颜色</param>
/// <param name="IsBorderVisable">是否使用边框</param>
/// <returns>二维码位图</returns>
public static Bitmap GetQrcode(Color Foreground,Color Background,bool IsBorderVisable)
{
Bitmap qrCodeImage = null;
re:
Expand All @@ -74,10 +78,26 @@ public static Bitmap GetQrcode()
QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
QRCode qrcode = new QRCode(qrCodeData);
//生成二维码位图
qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 0, 6, false);
qrCodeImage = qrcode.GetGraphic(5, Foreground, Background, null, 0, 6, IsBorderVisable);

qrCodeImage.MakeTransparent();

if (Background.A != 0)
{
for (int x = 0; x < qrCodeImage.Width; x++)
{
for (int y = 0; y < qrCodeImage.Height; y++)
{
if (qrCodeImage.GetPixel(x, y).ToArgb() == 0)
{
qrCodeImage.SetPixel(x, y, Background);
}
}
}
}

Monitor = new Timer(MonitorCallback, obj.data.oauthKey, 1000, 1000);
Refresher = new Timer(RefresherCallback, null, 180000, Timeout.Infinite);
Refresher = new Timer(RefresherCallback, new List<object>{ Foreground, Background, IsBorderVisable }, 180000, Timeout.Infinite);
}
}
else goto re;
Expand Down Expand Up @@ -181,7 +201,7 @@ private static void MonitorCallback(object o)
/// <param name="state"></param>
private static void RefresherCallback(object state)
{
Linq.ByQRCode.RaiseQrCodeRefresh(GetQrcode());
Linq.ByQRCode.RaiseQrCodeRefresh(GetQrcode((Color)((List<object>)state)[0], (Color)((List<object>)state)[1], (bool)((List<object>)state)[2]));
}

#endregion Private Methods
Expand Down
24 changes: 24 additions & 0 deletions BiliAccount/Exceptions/InvalidColorValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace BiliAccount.Exceptions
{
/// <summary>
/// 传入了错误的颜色值
/// </summary>
public class InvalidColorValue:Exception
{
/// <summary>
/// 错误的属性名
/// </summary>
public string PropertyName { get; private set; }

/// <summary>
/// 以指定的属性名初始化<see cref="InvalidColorValue"/>
/// </summary>
/// <param name="name">属性名</param>
public InvalidColorValue(string name) : base($"传入了错误的颜色值!{name}")
{
PropertyName = name;
}
}
}
35 changes: 31 additions & 4 deletions BiliAccount/Linq/ByQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Text.RegularExpressions;

#if NETFRAMEWORK

Expand Down Expand Up @@ -120,12 +121,38 @@ public static ImageSource GetQrCodeImageSource(Bitmap qrCodeImage)
#endif

/// <summary>
/// 用二维码登录
/// 获取二维码
/// </summary>
/// <returns>二维码图片实例</returns>
public static Bitmap LoginByQrCode()
/// <param name="strForeground">前景颜色</param>
/// <param name="strBackground">背景颜色</param>
/// <param name="IsBorderVisable">是否使用边框</param>
/// <returns>二维码位图</returns>
/// <exception cref="Exceptions.InvalidColorValue">传入了错误的颜色值</exception>
public static Bitmap LoginByQrCode(string strForeground = "#FF000000", string strBackground = "#FFFFFFFF", bool IsBorderVisable = false)
{
return Core.ByQRCode.GetQrcode();
Regex reg = new Regex("#[0-9A-Fa-f]{6,8}");
if (reg.IsMatch(strForeground) && reg.IsMatch(strBackground))
return LoginByQrCode(ColorTranslator.FromHtml(strForeground), ColorTranslator.FromHtml(strBackground), IsBorderVisable);
else if (!reg.IsMatch(strForeground))
throw new Exceptions.InvalidColorValue("strForeground");
else
throw new Exceptions.InvalidColorValue("strBackground");
}

/// <summary>
/// 获取二维码
/// </summary>
/// <param name="Foreground">前景颜色</param>
/// <param name="Background">背景颜色</param>
/// <param name="IsBorderVisable">是否使用边框</param>
/// <returns>二维码位图</returns>
/// <exception cref="Exceptions.InvalidColorValue">传入了错误的颜色值</exception>
public static Bitmap LoginByQrCode(System.Drawing.Color Foreground, System.Drawing.Color Background, bool IsBorderVisable = false)
{
if(Foreground != Background)
return Core.ByQRCode.GetQrcode(Foreground, Background, IsBorderVisable);
else
throw new Exceptions.InvalidColorValue("strForeground and strBackground can not be same!");
}

#endregion Public Methods
Expand Down

0 comments on commit 95fde76

Please sign in to comment.