• unity之二维码


    首先下载插件ZXing,链接在下面。解压后把unity文件夹的文件导入unity中。

    注意:二维码生成目前只能生成256*256的,不然会报所引超出界限错误。

    链接:https://pan.baidu.com/s/1_En2Tc2kdiDw8fvvzJZKYQ
    提取码:vl5k

    代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using ZXing;
    using ZXing.QrCode;
    
    public class QRCodeFactory
    {
        #region 单例
        private static QRCodeFactory instance;
    
        public static QRCodeFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new QRCodeFactory();
                }
                return instance;
            }
        }
    
        #endregion
    
        /// <summary>
        /// 直接返回图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="margin"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Texture2D GetTexture2D(string content, int margin = 0, int width = 256, int height = 256)
        {
            if (string.IsNullOrEmpty(content))
            {
                Debug.Log("错误!数据不能为空!");
                return null;
            }
            Texture2D t2d = new Texture2D(width, height);
            t2d.SetPixels32(GetQRcode(content, margin, width, height));
            t2d.Apply();
            return t2d;
        }
        /// <summary>
        /// 只返回生成二维码的颜色数据
        /// </summary>
        /// <param name="content"></param>
        /// <param name="margin"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Color32[] GetQRcode(string content, int margin = 0, int width = 256, int height = 256)
        {
            if (string.IsNullOrEmpty(content))
            {
                Debug.Log("错误!数据不能为空!");
                return null;
            }
    
            BarcodeWriter bw = new BarcodeWriter  //设置二维码变量类
            {
                Format = BarcodeFormat.QR_CODE,  //设置二维码格式
                Options = new QrCodeEncodingOptions //设置编码格式
                {
                    CharacterSet = "UTF-8",// 设置中文模式
                    Height = height, //设置宽高
                    Width = width,
                    Margin = margin  //设置二维码的边距
                }
            };
            return bw.Write(content);
        }
        public Result CheckQRCode(Texture2D t2d)
        {
            if (t2d == null)
            {
                Debug.Log("错误!图片为空!");
                return null;
            }
            Color32[] colorDatas = t2d.GetPixels32();
            BarcodeReader barcodeRender = new BarcodeReader();
            return barcodeRender.Decode(colorDatas, t2d.width, t2d.height);//将画面中的二维码信息检索出来
        }
    }

    using System.Collections;using System.Collections.Generic;using UnityEngine;using ZXing;using ZXing.QrCode;
    public class QRCodeFactory{    #region 单例    private static QRCodeFactory instance;
        public static QRCodeFactory Instance    {        get        {            if (instance == null)            {                instance = new QRCodeFactory();            }            return instance;        }    }
        #endregion
        /// <summary>    /// 直接返回图片    /// </summary>    /// <param name="content"></param>    /// <param name="margin"></param>    /// <param name="width"></param>    /// <param name="height"></param>    /// <returns></returns>    public Texture2D GetTexture2D(string content, int margin = 0, int width = 256, int height = 256)    {        if (content == "")        {            Debug.Log("错误!数据不能为空字符串!");            return null;        }        Texture2D t2d = new Texture2D(width, height);        t2d.SetPixels32(GetQRcode(content, margin, width, height));        t2d.Apply();        return t2d;    }    /// <summary>    /// 只返回生成二维码的颜色数据    /// </summary>    /// <param name="content"></param>    /// <param name="margin"></param>    /// <param name="width"></param>    /// <param name="height"></param>    /// <returns></returns>    public Color32[] GetQRcode(string content, int margin = 0, int width = 256, int height = 256)    {        if (content == "")        {            Debug.Log("错误!数据不能为空字符串!");            return null;        }
            BarcodeWriter bw = new BarcodeWriter  //设置二维码变量类        {            Format = BarcodeFormat.QR_CODE,  //设置二维码格式            Options = new QrCodeEncodingOptions //设置编码格式            {                CharacterSet = "UTF-8",// 设置中文模式                Height = height, //设置宽高                Width = width,                Margin = margin  //设置二维码的边距            }        };
            return bw.Write(content);    }    public Result CheckQRCode(Texture2D t2d)    {        if (t2d == null)        {            Debug.Log("错误!图片为空!");            return null;        }        Color32[] colorDatas = t2d.GetPixels32();        BarcodeReader barcodeRender = new BarcodeReader();        return barcodeRender.Decode(colorDatas, t2d.width, t2d.height);//将画面中的二维码信息检索出来    }}

  • 相关阅读:
    ZOJ 1002 Fire Net
    Uva 12889 One-Two-Three
    URAL 1881 Long problem statement
    URAL 1880 Psych Up's Eigenvalues
    URAL 1877 Bicycle Codes
    URAL 1876 Centipede's Morning
    URAL 1873. GOV Chronicles
    Uva 839 Not so Mobile
    Uva 679 Dropping Balls
    An ac a day,keep wa away
  • 原文地址:https://www.cnblogs.com/luoyanghao/p/13497885.html
Copyright © 2020-2023  润新知