reactjs 使用C#创建QRCode并将其保存为图像

ha5z0ras  于 2023-05-22  发布在  React
关注(0)|答案(1)|浏览(213)

我正在从我们的Web应用程序上的URL接收数据,该URL是使用API响应动态生成的。我必须把它放在一个QRCode中,然后把这个QRCode发送回前端,让我的客户端下载它。我们使用C#作为后端,React.js作为前端。
我试过在C#中使用ZXIng库,但没有成功。我尝试了很多代码,但似乎什么都没有发生。

5lhxktic

5lhxktic1#

请尝试以下代码。它已经过测试,但你可能需要调整它以适应你的使用,这应该不会太困难。

下载/安装NuGet包:ZXing.Net(版本:0.16.8)
增加以下using指令

  • using System.IO;
  • using ZXing;
  • using ZXing.Common;
  • using ZXing.QrCode;
    编码
private Bitmap CreateQrCode(string data)
{
    //specify desired options
    QrCodeEncodingOptions options = new QrCodeEncodingOptions()
    {
        CharacterSet = "UTF-8",
        DisableECI = true,
        Width = 250,
        Height = 250
    };

    //create new instance and set properties
    BarcodeWriter writer = new BarcodeWriter()
    {
        Format = BarcodeFormat.QR_CODE,
        Options = options
    };

    //create QR code and return Bitmap
    return writer.Write(data);
}

private byte[] GetQrCodeBytes(string data, System.Drawing.Imaging.ImageFormat imgFormat)
{
    using (MemoryStream ms = new MemoryStream())
    {
        //create QR code and save to file
        using (Bitmap bmp = CreateQrCode(data))
        {
            //save to MemoryStream
            bmp.Save(ms, imgFormat);
        }

        return ms.ToArray();
    }
}

private string GetTextFromQrCode(byte[] qrcodeBytes)
{
    //specify desired options
    DecodingOptions options = new DecodingOptions()
    {
        CharacterSet = "UTF-8"
    };

    //create new instance and set properties
    BarcodeReader reader = new BarcodeReader() { Options = options };

    using (MemoryStream ms = new MemoryStream(qrcodeBytes))
    {
        using (Bitmap bmp = (Bitmap)Bitmap.FromStream(ms))
        {
            //decode QR code
            Result r = reader.Decode(bmp);

            //return QR code text
            return r.Text;
        }
    }
}

private string GetTextFromQrCode(string filename)
{
    //specify desired options
    DecodingOptions options = new DecodingOptions()
    {
        CharacterSet = "UTF-8"
    };

    //create new instance and set properties
    BarcodeReader reader = new BarcodeReader() { Options = options };

    //read image and convert to Bitmap
    using (Bitmap bmp = (Bitmap)Bitmap.FromFile(filename))
    {
        //decode QR code
        Result r = reader.Decode(bmp);

        //return QR code text
        return r.Text;
    }
}

private void SaveQrCode(string data, string filename, System.Drawing.Imaging.ImageFormat imgFormat)
{
    //create QR code and save to file
    using (Bitmap bmp = CreateQrCode(data))
    {
        bmp.Save(filename, imgFormat);
    }
}

二维码解码(用法一):

string filename = @"C:\Temp\TestQrCode.png"
string qrcodeText = GetTextFromQrCode(filename);

二维码解码(用法二):
注意:下面的代码中,QR码是从文件中读取的,并放置在byte[]中。从文件阅读是为了演示/测试目的。

string filename = @"C:\Temp\TestQrCode.png"
byte[] qrcodeBytes = File.ReadAllBytes(filename);
string qrcodeText = GetTextFromQrCode(qrcodeBytes);

保存二维码(用法一):

string qrcodeText = "This is a test";
string filename = @"C:\Temp\TestQrCode.png"

//get filename extension
string ext = Path.GetExtension(filename);

if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
    SaveQrCode(qrcodeText, filename, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
    SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (ext == ".png")
    SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Png);

保存二维码(用法二):
注意:下面代码中,二维码保存到byte[],然后写入文件。写入文件用于演示/测试目的。

string qrcodeText = "This is a test";
string filename = @"C:\Temp\TestQrCode.png"

//get filename extension
string ext = Path.GetExtension(filename);

byte[] qrcodeBytes = null;

if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
    qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
    qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (ext == ".png")
    qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Png);

//save to file
File.WriteAllBytes(fileName, qrcodeBytes);

资源

相关问题