生成二维码

作者:追风剑情 发布于:2019-10-29 16:26 分类:Unity3d

一、下载 ThoughtWorks.QRCode.dll放到Unity工程的Plugins目录下
1111.png
百度网盘下载 提取码 s786
PS: System.ComponentModel.dll和System.Drawing.dll在Unity的安装目录下就可以找到。

二、封装一个生成二维码的辅助类

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using UnityEngine;
using ThoughtWorks.QRCode.Codec;

/// <summary>
/// 生成二维码辅助类 (仅考虑了Windows平台)
/// 需要下载 ThoughtWorks.QRCode.dll
/// </summary>
public sealed class QRCodeHelper
{
    /// <summary>
    /// 创建二维码Texture (Unity接口)
    /// </summary>
    /// <param name="content"></param>
    /// <param name="savePath"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static Texture2D CreateQRcodeTexture(string content, string savePath, string fileName = "qrcode")
    {
        Bitmap bitmap = CreateQRCode(content);
        int width = bitmap.Width;
        int height = bitmap.Height;
        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, ImageFormat.Png);
        byte[] bytes = ms.ToArray();

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
        tex.LoadImage(bytes);
        tex.Apply();

        return tex;
    }

    /// <summary>
    /// 创建并保存二维码图片
    /// </summary>
    /// <param name="content"></param>
    /// <param name="savePath"></param>
    /// <param name="fileName"></param>
    public static void CreateQRcode(string content, string savePath, string fileName="qrcode")
    {
        Bitmap bitmap = CreateQRCode(content);
        SaveQRCode(bitmap, savePath, fileName);
    }

    /// <summary>
    /// 创建二维码 (C#接口)
    /// </summary>
    /// <param name="content">内容</param>
    /// <returns></returns>
    public static Bitmap CreateQRCode(string content)
    {
        try
        {
            QRCodeEncoder qrEncoder = new QRCodeEncoder();
            //二维码类型
            qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //二维码尺寸
            qrEncoder.QRCodeScale = 4;
            //二维码版本
            qrEncoder.QRCodeVersion = 7;
            //二维码容错程度
            qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //字体与背景颜色
            qrEncoder.QRCodeBackgroundColor = System.Drawing.Color.White;
            qrEncoder.QRCodeForegroundColor = System.Drawing.Color.Black;
            //UTF-8编码类型
            Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8);

            return qrcode;
        }
        catch (Exception)
        {
            return null;
        }
    }

    /// <summary>
    /// 保存二维码 (C#接口)
    /// </summary>
    /// <param name="QRCode">二维码图片</param>
    /// <param name="SavePath">保存路径</param>
    /// <param name="QRCodeName">图片名称</param>
    public static void SaveQRCode(Bitmap QRCode, string SavePath, string QRCodeName)
    {
        if (!Directory.Exists(SavePath))
        {
            Directory.CreateDirectory(SavePath);
        }
        QRCode.Save(Path.Combine(SavePath, QRCodeName + ".png"), ImageFormat.Png);

        QRCode.Dispose();
    }
}

测试效果
222.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号