unity 生成二维码,识别图片中的二维码


在识别和生成二维码这个ZXing的插件很好用



首先是识别图片中的二维码,也可以打开摄像头来扫描二维码,我想这里就写识别图片中的二维码

//把加载到的图片传进来

 public string GetFishID(Texture2D tex)
    {
        string sr = Decode(tex.GetPixels32(), tex.width, tex.height);
        return sr;
    }
    private BarcodeReader mReader= new BarcodeReader();

//识别二维码中的信息
    public string Decode(Color32[] colors, int width, int height)
    {
        var result = mReader.Decode(colors, width, height);
        if (result != null)
        {
            return result.Text;
        }
        else
        {
            Decode(tex.GetPixels32(), tex.width, tex.height);
        }
        return null;
    }

 

 

下面写一下如何生成二维码

public void ShowQRCode(string s_str, int s_width, int s_height)
    {
        //定义Texture2D并且填充
        Texture2D tTexture = new Texture2D(s_width, s_height);

        //绘制相对应的贴图纹理
        tTexture.SetPixels32(GeneQRCode(s_str, s_width, s_height));

        tTexture.Apply();

        //赋值贴图
        m_QRCode.texture = tTexture;
    }

    /// <summary>
    /// 返回对应颜色数组
    /// </summary>
    /// <param name="s_formatStr">扫码信息</param>
    /// <param name="s_width">码宽</param>
    /// <param name="s_height">码高</param>
    Color32[] GeneQRCode(string s_formatStr, int s_width, int s_height)
    {
        //设置中文编码格式,否则中文不支持
        QrCodeEncodingOptions tOptions = new QrCodeEncodingOptions();
        tOptions.CharacterSet = "UTF-8";
        //设置宽高
        tOptions.Width = s_width;
        tOptions.Height = s_height;
        //设置二维码距离边缘的空白距离
        tOptions.Margin = 3;

        //重置申请写二维码变量类       (参数为:码格式(二维码、条形码...)    编码格式(支持的编码格式)    )
        m_barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = tOptions };

        //将咱们需要隐藏在码后面的信息赋值上
        return m_barcodeWriter.Write(s_formatStr);
    }

 

 

 

Zxing 插件的链接    https://download.csdn.net/download/baozaodedianxiaoer/12689401

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值