Unity 生成二维码

Posted clhxxlcj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 生成二维码相关的知识,希望对你有一定的参考价值。

public void CaptureScreen(Camera c, Rect r)
    
        //捕抓摄像机图像并转换成字符数组
        RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);
        c.targetTexture = rt;
        c.Render();

        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(r, 0, 0);
        screenShot.Apply();

        c.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);

        byte[] bytes = screenShot.EncodeToPNG();
        string url = "http://cus.szqicnt.com/whzq/put.php";//要上传到的地址

        WWWForm form = new WWWForm();
        form.AddField("name", "test.png");
        form.AddBinaryData("file", bytes);//把图片流上传

        WWW www = new WWW(url, form);
        StartCoroutine(PostData(www));//启动子线程
        Destroy(screenShot);//销毁
    

IEnumerator PostData(WWW www)
    
        yield return www;
        string returnStr = www.text.Trim();
        Debug.Log(returnStr);
        if(returnStr == "1")
        
            Debug.Log("图片文件容量超过php.ini限制");
        


        Texture2D qr = ZXingUtil.instance.QrCreat(returnStr);
        if(qr!=null && this.QRCode!=null)
        
            this.QRCode.texture = qr;
        
    


using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using UnityEngine;
using ZXing;
using ZXing.Common;

public class ZXingUtil : MonoBehaviour 

    private int _width = 256;
    private int _height = 256;
    private Texture2D _texure2d;
    private bool _success;
    public static ZXingUtil instance;
    EncodingOptions options = null;
    BarcodeWriter writer = null;

    void Awake()
    
        instance = this;
    

    void Start()
    
        _texure2d = new Texture2D(_width, _height);

        options = new EncodingOptions
        
            Width = _width,
            Height = _height
        ;
        writer = new BarcodeWriter();
        writer.Format = BarcodeFormat.QR_CODE;
        writer.Options = options;
    
    /// <summary>
    /// 传入条形码内容 返回条码图,条形码须为偶数位
    /// </summary>
    /// <param name="barcode"></param>
    /// <returns></returns>
    public Texture2D QrCreat(string barcode)
    
        try
        
            Bitmap bitmap = writer.Write(barcode);

            Image image = writer.Write(barcode);
            //将PNG / JPG图像字节数组加载到纹理中
            _texure2d.LoadImage(ImageToByte(image));
            return _texure2d;
        
        catch (Exception e)
        
            Debug.LogError("z xing error:"+e);
            return null;
        
    

    public Texture2D QrCreat(string barcode, Vector2 imageVec)
    
        _texure2d = new Texture2D((int)imageVec.x, (int)imageVec.y);
        Bitmap bitmap = writer.Write(barcode);
        _texure2d.LoadImage(ImageToByte(bitmap));
        return _texure2d;
    

    //将image类型转化为byte[]类型
    private byte[] ImageToByte(Image img)
    
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    

  

以上是关于Unity 生成二维码的主要内容,如果未能解决你的问题,请参考以下文章

Unity怎么制作中心带logo的二维码

[Unity] 二维洞穴地图随机生成

Unity插件系列之二维码

pbootcms对接微信扫码登录代码核心片段和步骤(前后端)

[Unity3D]自制UnityForAndroid二维码扫描插件

zxing.unity.dll下载之后的位置找到UnityDemo/Assets下