.NET Core使用SkiaSharp快速生成二維碼( 真正跨平臺方案)
前言
在.NET 6之前我們一直是使用QRCoder來生成二維碼(QRCoder是一個非常強大的生成二維碼的組件,用到了System.Drawing.Common 包),然后從.NET 6開始,當為非 Windows 操作系統(tǒng)編譯引用代碼時,平臺分析器會發(fā)出編譯時警告。異常如下:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception.
---- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
Stack Trace:
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(42,0): at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(25,0): at System.Drawing.Bitmap..ctor(String filename)
/_/src/libraries/System.Resources.ResourceManager/tests/ResourceManagerTests.cs(270,0): at System.Resources.Tests.ResourceManagerTests.EnglishImageResourceData()+MoveNext()
/_/src/libraries/System.Linq/src/System/Linq/Select.cs(136,0): at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
----- Inner Stack Trace -----
/_/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs(31,0): at System.Drawing.LibraryResolver.EnsureRegistered()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs(65,0): at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs(27,0): at System.Drawing.SafeNativeMethods.Gdip..cctor()
報錯原因
由于 System.Drawing.Common 被設計為 Windows 技術的精簡包裝器,因此其跨平臺實現(xiàn)欠佳。
libgdiplus 是本機端 System.Drawing.Common 跨平臺實現(xiàn)的主要提供程序。libgdiplus 實際上是對 System.Drawing.Common 所依賴的 Windows 部分的重新實現(xiàn)。該實現(xiàn)使 libgdiplus 成為一個重要的組件。它大約有 30,000 行 C 代碼,大部分未經測試,而且缺少很多功能。libgdiplus 還具有許多用于圖像處理和文本呈現(xiàn)的外部依賴項,例如 cairo、pango 和其他本機庫。這些依賴項使得維護和交付組件更具挑戰(zhàn)性。自從包含 Mono 跨平臺實現(xiàn)以來,我們已將許多從未得到修復的問題重定向到 libgdiplus。相比之下,我們采用的其他外部依賴項,例如 icu 或 openssl,都是高質量的庫。使 libgdiplus 的功能集和質量與 .NET 堆棧的其余部分相媲美是不可行的。
官方建議
在非 Windows 環(huán)境中,推薦使用 SkiaSharp 和 ImageSharp 等庫來完成跨平臺的圖像處理操作。這些庫提供了更全面和穩(wěn)定的功能支持,適用于大多數圖形操作需求。而 System.Drawing.Common 將主要繼續(xù)在 Windows 窗體和 GDI+ 的上下文中進行演化。
SkiaSharp介紹
SkiaSharp是由Mono團隊維護的開源項目,它是基于Google的Skia圖形庫的.NET跨平臺綁定,提供了強大的2D圖形繪制和處理功能,適用于多個平臺,包括Windows、macOS、Linux、iOS和Android,可用于創(chuàng)建高性能的、可移植的2D圖形應用程序。
SkiaSharp 提供了跨平臺綁定,適用于以下平臺:
- .NET Standard 1.3
- .NET Core
- .NET 6
- Tizen
- Android
- iOS
- tvOS
- macOS
- Mac Catalyst
- WinUI 3 (Windows App SDK / Uno Platform)
- Windows Classic Desktop (Windows.Forms / WPF)
- Web Assembly (WASM)
- Uno Platform (iOS / macOS / Android / WebAssembly)
這些平臺可以使用 SkiaSharp 來進行跨平臺的圖形渲染和繪制操作。
使用SkiaSharp快速生成二維碼
1、安裝SkiaSharp.QrCode庫
使用SkiaSharp生成二維碼的QR碼生成器(無需使用System.Drawing)。
圖片
2、生成二維碼圖片
/// <summary>
/// 生成二維碼圖片
/// </summary>
public static void GenerateQRCode()
{
var qrCodeContent = "追逐時光者";
using (var generator = new QRCodeGenerator())
{
// 創(chuàng)建二維碼(并設置糾錯能力最高級)
var createQrCode = generator.CreateQrCode(qrCodeContent, ECCLevel.H);
var skImageInfo = new SKImageInfo(300, 300);
// 創(chuàng)建SkiaSharp畫布
using (var surface = SKSurface.Create(skImageInfo))
{
var canvas = surface.Canvas;
// 渲染二維碼到畫布
canvas.Render(createQrCode, skImageInfo.Width, skImageInfo.Height);
using (var image = surface.Snapshot())// 獲取畫布快照
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))// 編碼畫布快照為PNG格式的數據
using (var stream = File.OpenWrite(@"MyQRCode.png"))
{
data.SaveTo(stream);// 將數據保存到文件流中,生成二維碼圖片
}
}
}
}
二維碼的糾錯能力通常分為四個級別
- 在生成二維碼時,糾錯能力(Error Correction Capability)指的是QR碼中包含的冗余信息,用于修復可能出現(xiàn)的損壞或錯誤。QR碼可以通過添加冗余信息來提高其容錯性,即使在一定程度上受到損壞或噪聲的干擾,仍然可以正確地解碼和還原二維碼的內容。
- QR碼的糾錯能力通常分為四個級別,從低到高依次為:L、M、Q、H。其中,糾錯能力最低的級別為L,最高的級別為H。糾錯能力越高,二維碼中包含的冗余信息就越多,因此可以更好地恢復丟失或損壞的數據。
namespace SkiaSharp.QrCode
{
public enum ECCLevel
{
L,
M,
Q,
H
}
}
3、查看生成效果
圖片
參考文章
- https://learn.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only
- QRCoder:https://github.com/codebude/QRCoder