1.NuGet 安装ZXing.Net
安装System.Drawing.Common,6.0.0>=版本只支持windows运行,如果是.net core跨平台项目建议安装6.0.0以下版本要不会报错”此平台不支持System.Drawing.Common。“(也可引用别的三方库)
2.解析代码
public static string Base64ToQRCode(string base64String)
{
byte[] arr = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
BarcodeReader reader = new BarcodeReader
{
//不加这个部分二维码可能会解析失败。
AutoRotate = true, // 尝试自动旋转图像
Options = new DecodingOptions
{
TryHarder = true, // 尝试更多的解码方法
PossibleFormats = new BarcodeFormat[] { BarcodeFormat.QR_CODE } // 限制为QR码
}
};
Result result = reader.Decode(bmp);
if (result!=null)
{
return result.Text;
}
return "";
}